Loading field_collection values in templates

Field collection is a great module to have some sort of a bundle of fields in a form. Thus you can build a form field with mulitple values that itself has more than one fields attached. Maybe more about this module in another blogpost.

If you want to load the values of a field collection entity (just if you need it in an template for example) you can use entity_metadata_wrapper():

<?php
$wrapper = entity_metadata_wrapper('node', $node);
// In case of a non-multiple collection and an example text field, you could get the text via
print $wrapper->field_collection->field_example->value();
// In case of a multiple collection and an example text field, you could get the text of the first one:
print $wrapper->field_collection[0]->field_example->value();
// and so one..
?>

 
Or you load the value with entity_load():

<?php
$field_collection = entity_load('field_collection_item', array(FIELD COLLECTION ITEM ID));
?>

 

Links:

http://drupal.org/project/field_collection
http://api.drupal.org/api/drupal/includes--common.inc/function/entity_lo...
http://drupalcontrib.org/api/drupal/contributions--entity--entity.module...





Powered by Drupal7