Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
By mikeker on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.0.x
Issue links:
Description:
The format_xml_elements
function has been removed and replaced with Twig templates or, in the case of tests, inline XML. This function was primarily used to generate RSS feeds.
Module developers that use format_xml_elements
in their code will need to replace it as such:
Before:
$xml = format_xml_elements(array(array(
'key' => 'test_tag',
'value' => 'some value goes here',
)));
After:
Implement hook_theme
to define a new Twig template to render the XML:
function test_theme() {
$items = array(
'xml_element' => array(
'variables' => array(
'tag' => NULL,
'value' => NULL,
'attributes' => NULL,
),
),
);
return $items;
}
Optionally add a preprcoess function to prepare the variables:
function test_preprocess_xml_element(&$variables) {
if (!empty($variables['attributes'])) {
$variables['attributes'] = new Attribute($variables['attributes']);
}
}
Add a Twig template (in this case in templates/xml-element.html.twig
):
{#
/**
* @file
* Default theme implementation for an XML element.
*
* Available variables:
* - tag: The XML element name.
* - value: (optional) The value for this XML element
* - attributes: (optional) XML attributes for the tag element.
*
* @ingroup themeable
*/
#}
<{{ tag }}{{ attributes|default -}}
{% if value|default -%}
>{{ value }}</{{ tag }}>
{% else -%}
{{ ' />' }}
{% endif %}
Finally:
$render_array = array(
'#theme' => 'xml_element',
'#tag' => 'test_tag',
'#attributes' => ['attr1' => 42, 'attr2' => 'on'],
'#value' => 'foo',
);
$xml = \Drupal::service('renderer')->render($render_array);
Impacts:
Module developers
Themers