On this page
How to customize the structure of the XML file
My client wanted to export an XML file, and Views Data Export will output a valid XML file. But it is structured according to how the view renders the data. In our case the client wanted the XML file to be formatted in a particular way and we didn't see any easy way to get Views to render the data in the format that the client wanted. This page shows how we were able to do that.
If you look at the module, you'll see that right away the init() function begins to load some code that "themes" the output. And in the theme directory we can see various files that appear to be code that "themes" some output. That is the key to getting custom XML output. In our case, the theming code was theme/views-data-export-xml-body.tpl.php and our solution was overriding that code with our own.
Here is the custom module we developed to do that overriding:
Our custom_views_data_export_xml module
custom_views_data_export_xml.info:
name = Custom Views Data Export XML
description = Change format of XML export from Views Data Export module.
package = Custom Modules
core = 7.x
version = "7.x-1.0"
core = "7.x"custom_views_data_export_xml.module
<?php
function custom_views_data_export_xml_theme($existing, $type, $theme, $path) {
return array (
'views_data_export_xml_body__holycom_strings__views_data_export_2' => array (
'variables' => array('view' => NULL, 'options' => NULL, 'rows' => NULL, 'title' => NULL),
'template' => 'views-data-export-xml-body--holycom-strings--views-data-export-2' ,
'base hook' => 'views_data_export_xml_body',
'path' => drupal_get_path('module', 'custom_views_data_export_xml'),
),
);
}
views-data-export-xml-body--holycom-strings--views-data-export-2.tpl.php
<?php
// print_r($themed_rows);
// exit();
/*
* Make sure field_products has "display multiple items on same line" enabled.
*/
$rows_temp = array();
foreach ($themed_rows as $count => $row){
$rows_temp[$row['field_term_id']][] = $row;
}
?>
<?php foreach ($rows_temp as $id => $rows): ?>
<?php $ids_row = $rows; $rw = array_shift($ids_row); ?>
<string id="<?php print trim(strip_tags($rw['field_term_id'])); ?>">
<?php
$products_array = explode(',',$rw['field_products']);
?>
<product>
<?php foreach ($products_array as $product_row): ?>
<type><?php print trim(strip_tags($product_row)); ?></type>
<?php endforeach; ?>
</product>
<comment><?php print trim(strip_tags($rw['body'])); ?></comment>
<translations>
<?php foreach ($rows as $count => $row): ?>
<language name="<?php print trim(strip_tags($row['field_translation_languge']));?>">
<value><?php print trim(strip_tags($row['field_term_translated'])); ?></value>
<date_last_changed><?php print trim(strip_tags($row['field_translated_term_date'])); ?></date_last_changed>
</language>
<?php endforeach; ?>
</translations>
</string>
<?php endforeach; ?>
Some things to know
What this script does
Compare the theme/views-data-export-xls-body.tpl.php script that is in the Views Data Export module with the views-data-export-xml-body--holycom-strings--views-data-export-2.tpl.php script that is shown above. They both begin by stepping through the $themed_rows array. That is the content that will be themed.
How do you know what is the content of the $themed_rows array? Look at the first two lines of the .tpl.php file:
// print_r($themed_rows);
// exit();
If you uncomment the lines then you will cause the export to crash, but you'll also get a peek at what this script is getting fed as input. That will help in developing your own script.
Naming your .tpl.php file
Note that ours is named: views-data-export-xml-body--holycom-strings--views-data-export-2.tpl.php
Know that "holycom-strings" is the name of the view and "views-data-export-2" is the name of the display in that view.
How to handle fields with multiple values
We have a multi-value text field with a machine name of field_products. The key to theming that field is to set configure the view so that the "display multiple items on same line" is enabled. This will cause those values to print on the same line, being separated with a comma. Our script then explodes that field into an array and then prints an XML element for each element in the array.
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion