Drupal - How do I load a field collection from a node?

Doh! I should have looked in the field collection module:

entity_load('field_collection_item', array($item_id))

There is also a wrapper function that does the same trick:

field_collection_item_load($id, $reset = FALSE)

or as per comment, use:

field_collection_item_load_multiple($ids = array(), $conditions = array(), $reset = FALSE)

to load multiple collections (for example if you have an Add more buttons).


field_collection_field_get_entity() should be used in order to load the correct revision.

Example usage:

$node = node_load(1);
$items = field_get_items('node', $node, 'field_fc');
foreach ($items as $item) {
 $fc = field_collection_field_get_entity($item);
 // Do something.
}

Tags:

Entities

7