Posted by sijuwi on February 6, 2012 at 10:45am
5 followers
Jump to:
| Project: | Field collection |
| Version: | 7.x-1.0-beta3 |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Issue Summary
I'm using:
<?php
$field_collection = entity_load('field_collection_item', array($node->field_my_collection_field['und'][0]['value']));
print $field_collection[1]->field_my_field_1['und'][0]['value'];
print $field_collection[1]->field_my_field_2['und'][0]['value'];
?>to output my field collection in node.tpl.php
But I need to loop through the array, so that i can theme each field with a collection over multiple field collections.
Can anyone set me on the right path to achieve this?
Comments
#1
I think what you need is
<?php$t = count($node->field_lima_variant_coll['und']);
for ($i = 0; $i <= $t-1; $i++) {
$field_collection = entity_load('field_collection_item', array($node->field_lima_variant_coll['und'][$i]['value']));
print $field_collection[$i+1]->field_lima_variant_text['und'][0]['value'];
print ($field_collection[$i+1]->field_lima_variant_pdf['und'][0]['url']);
unset($field_collection);
echo '<br /><br />';
}
?>
Thanks for your ticket, that helped me a lot, hope this helps you!
#2
I've found the problem why the field collection is not present in the $content / $node var in the node template. Annoying as hell, but it has to do with "display fields" in the content type. If you don't take care of that tab it's very easy to miss, but in my situation the field was not displayed. Once I've made it display, you can access the vars (field collection) as an array.
#3
This is not very themer-friendly, plus it adds logic to the templates, which isn't recommended.
Wouldn't it be possible to add
$field_*variables to the node.tpl.php automatically, like CCK did ? That would be far more logical. The data is linked (so to speek) to the node anyway.I'm not very familiar with the Entity API, but I could have a look at it if necessary.
Thanks for the module btw. Bummer this didn't made it to Drupal core. It's such a cool feature :-).