1

I have my site set up to import CSVs and manual entry and exporting the key information into an XML feed/File using views data export. I have a problem when I need to add a group of fields. The user can add as many of these groups of fields as they need and they should appear in the output in a single row as nested items.

When I use field collections the output produces invalid XML or is stripped of all wrappers so that it is just plain text. I have tried custom template files in my theme but views data export strips out the tags.

Desired output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<file_information xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<asset_data>
    <upn>GBHIS3473610HD30P001</upn>
    <title>My Title</title>
    <version>Sunday 26th January</version>
    <duration>00:00:30:00</duration>
    <tc_in>10:00:00:00</tc_in>
    <tc_out>10:00:30:00</tc_out>
    <aspect_ratio>16:9</aspect_ratio>
    <segment>
        <sequence>1</sequence>
        <tc_in>10.00.00.00</tc_in>
        <tc_out>10.00.30.00</tc_out>
    </segment>
    <segment>
        <sequence>2</sequence>
        <tc_in>DMC</tc_in>
        <tc_out>DMC</tc_out>
    </segment>
    <segment>
        <sequence>3</sequence>
        <tc_in>DMC</tc_in>
        <tc_out>DMC</tc_out>
    </segment>
    <segment>
        <sequence>4</sequence>
        <tc_in>DMC</tc_in>
        <tc_out>DMC</tc_out>
    </segment>
</asset_data>
</file_information>

Actual output (rest of XML is fine)

<segment>
  <sequence>
    <section class="field field-name-field-sequence field-type-number-integer field-label-above view-mode-full">
     <h2 class="field-label">sequence:&amp;nbsp;</h2>
     <div class="field-items">
     <div class="field-item even">1</div>
     </div>
    </section>
  </sequence>
  <tc_in>
  <section class="field field-name-field-tc-in field-type-hms-field field-label-above view-mode-full">
     <h2 class="field-label">tc_in:&amp;nbsp;</h2>
     <div class="field-items">
       <div class="field-item even">
         <span class="hms hms-format-h-mm-ss">0:00:00</span>
       </div>
     </div>
   </section>
  </tc_in>
  <tc_out>
    <section class="field field-name-field-tc-out field-type-hms-field field-label-above view-mode-full">
    <h2 class="field-label">tc_out:&amp;nbsp;</h2>
    <div class="field-items">
     <div class="field-item even">
       <span class="hms hms-format-h-mm-ss">0:00:30</span>
     </div>
    </div>
   </section>
  </tc_out> 
  <sequence>
    <section class="field field-name-field-sequence field-type-number-integer field-label-above view-mode-full">
     <h2 class="field-label">sequence:&amp;nbsp;</h2>
     <div class="field-items">
       <div class="field-item even">2</div>
     </div>
    </section>
   </sequence>
   <tc_in>
     <section class="field field-name-field-tc-in field-type-hms-field field-label-above view-mode-full">
     <h2 class="field-label">tc_in:&amp;nbsp;</h2>
     <div class="field-items">
      <div class="field-item even">
        <span class="hms hms-format-h-mm-ss">0:00:00</span>
      </div>
     </div>
    </section>
   </tc_in>
   <tc_out>
     <section class="field field-name-field-tc-out field-type-hms-field field-label-above view-mode-full">
     <h2 class="field-label">tc_out:&amp;nbsp;</h2>
     <div class="field-items">
      <div class="field-item even">
        <span class="hms hms-format-h-mm-ss">0:00:20</span>
      </div>
     </div>
    </section>
  </tc_out>
 </segment>

So how can I customise the output or should I use something other than field collections?

contents of my field-collection-item.tpl.php

<sequence><?php print render($content['field_sequence']); ?></sequence>
<tc_in><?php print render($content['field_tc_in']); ?></tc_in>
<tc_out><?php print render($content['field_tc_out']); ?></tc_out>

contents of my views-data-export-xml-body.tpl.php

<?php
   $item_item = 'asset_data';
?>
<?php foreach ($themed_rows as $count => $row): ?>
  <<?php print $item_item; ?>>
<?php foreach ($row as $field => $content): ?>
  <?php if($content != NULL){ ?>
    <?php $content = str_replace("&lt;","<",$content); 
     $content = str_replace("&gt;",">",$content); ?>
     <<?php print $xml_tag[$field]; ?>><?php print_r $content; ?></<?php print $xml_tag[$field]; ?>>
    <?php } ?>
 <?php endforeach; ?>
   </<?php print $item_item; ?>>
 <?php endforeach; ?>
  • Looks like you haven't cleaned tpl for rows and fields. – Mołot Dec 10 '13 at 13:38
  • As far as I know, Field Collection's Views integration is poor, to say the least. @user24312 You might have better luck in the module issue queue. – Beebee Dec 10 '13 at 14:42
  • 1
    @Mołot was right - I had cleaned fields tpl file but had a typo in the filename so it wasn't picking it up. Initial test looks good. – Keypad Dec 10 '13 at 14:59
  • @user24312 feel free to post that as self-answer (if 8h limit for self-answers was lifted already). – Mołot Dec 10 '13 at 15:09
  • G'dam typos always get you! – Beebee Dec 10 '13 at 15:26
1

As suggested by @Molot I needed to clean fields.tpl.php which I had done but had a typo in the name which is why I was puzzled.

In addition to this I needed to add some logic into views-data-export-xml-body.tpl.php to wrap each set of field items seperately.

(used str_replace to find the closing tag of the last element and insert the closing and opening wrapper tag)

$content = str_replace("/tc_out> <sequence>","/tc_out> </segment>
    <segment> <sequence>",$content);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.