I'm moving from D6 to D7 theming and am running into a couple of speed bumps. Can someone clear up how to access field collection field values from the node.tpl?

When I dsm the node, my (unlimited) field_collection_xxx's values are integers, which I assume relate to the entities that field collection creates. Is there a way to load these entities in my node.tpl so I can run the fields through a foreach statement (preferred, since I'm creating a ul), or should I theme the field_collection_item.tpl? If the latter, how would I specify template inheritance for each field collection type?

Thanks for your time.

Comments

RobW’s picture

Status: Closed (fixed) » Active

OK, I looked at the tpls and docs a little closer and answered my own questions. If other themers are wondering, you can use entity_load(). From dsm-ing the $content variable it seems like you could also drill deep down to get the values, but that seems messy. The standard would probably be to work with the content variable in field_collection_item.tpl.php, but I didn't want to do that in this case.

This example prints out a clean ul of contributors optionally linked to websites. The unlimited field collection is named 'field_collection_contributors', and contains two fields, 'field_contributor' and 'field_contrib_link'. This has been edited to give a more generic example.

// check if field collection is not empty. If content exists, do some stuff.
if (!empty($content['your_field_collection'])) {

  //Your custom wrapper goes here
  print '<div class="collectin-it">';

  //Grab each entity uri (which is basically a field collection item), and load it through entity_load(). The entity type is field_collection_item.
  foreach ($content['your_field_collection']['#items'] as $entity_uri) {
    $a_field_collection_item = entity_load('field_collection_item', $entity_uri);

    foreach ($a_field_collection_item as $a_field_collection_item_object ) {

      //Now you have a variable with the entity object loaded in it, and you can do stuff. 
     print render($a_field_collection_item_object->field_1);
     print render($a_field_collection_item_object->field_2);
     print render($a_field_collection_item_object->field_3);
    }

  }

  //close your custom wrapper.
  print '</div>' . "\n"; 
}

Hope this helps anyone with the same question.

[Edited to represent a more general example]

RobW’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

remaye’s picture

In #2, there is two different variables : $a_field_collection_collection and $a_field_collection_item
Shouldn't they be the same ?
And if not, where does $a_field_collection_item come from ?

RobW’s picture

Status: Active » Closed (fixed)

@remaye, I think you're right. Edited for future visitors.

Although I haven't had a chance to work with them, the word in other threads is that an entity wrapper is a more robust way to deal with field collection field values. Might want to check that out:

http://drupal.org/node/1166238#comment-4580004