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

Nehbur’s picture

I think what you need is

		$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!

Nehbur’s picture

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.

wadmiraal’s picture

Category: support » feature

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 :-).

wadmiraal’s picture

Issue summary: View changes

typo

jmuzz’s picture

Issue summary: View changes

A better solution than putting the logic in the template may be to use hook_entity_view or to create a custom field in display suite.

I don't think field collections should add all of the nested field collection data to a host entity by default. It's not necessary to render the host since to it the field collection is just a field to be rendered with its own functions. It might make sense to provide a function that would expand the field collections in an entity.

omrmankar’s picture

Essay way to render multiple field collection field without any error and notice.

$t = count($node->field_collection_name_collection['und']);
for ($i = 0; $i <= $t-1; $i++) {
  $field_collection = entity_load('field_collection_item', array($node->field_collection_name_collection['und'][$i]['value']));
     foreach ($field_collection as $value) {
          print  "<h1>".$value->field_collection_name_title['und'][0]['value']. "</h1>";
          print  $value->field_collection_name_body['und'][0]['value'];
      }
}