73

I have a node that has a few field collections on it.

When I look at node_load() output, I see:

[field_rich_text_group] => Array
        (
            [und] => Array
                (
                    [0] => Array
                        (
                            [value] => 6
                        )

                    [1] => Array
                        (
                            [value] => 7
                        )

                    [2] => Array
                        (
                            [value] => 8
                        )

                )

        )

What API function can I use to actually load the field data contained in the collection that has an entity_id of 6, 7, 8, etc?

89

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

entity_load('field_collection_item', array($item_id))
  • 1
    I am looking for similar solution. How do you write the output and placed them into rendered array? Thanks – swan Nov 9 '11 at 9:11
  • I wrote a bunch of custom functions to build the layout for these items. Sadly they return HTML, not renderable arrays. It would be nice to use renderable arrays, but I'm not completely clear on the point (I'm sure there is a good reason to do it, I just haven't found an explanation that resonates). – Justin Nov 11 '11 at 22:42
  • Thanks. Field colection is indeed useful to ease up forms, and output, but when it comes to custom queries, I seem to have to dive into the nested arrays, even using entity_metadata_wrapper. – swan Nov 12 '11 at 6:36
  • Oh yah, I wrote wrappers that just turn the dense arrays into the what I need. – Justin Nov 12 '11 at 18:07
  • entity_load() returns a set of nodes which are specific to "entity" and along with them we get the user defined fields too. How do we differentiate the user fields from the fields of an entity? Any clues? – Raj Pawan Gumdal Sep 19 '13 at 10:35
39

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

  • 5
    As per @vasiliy-grotov 's answer above you can also use field_collection_item_load_multiple($ids = array(), $conditions = array(), $reset = FALSE), to load multiple collections (for example if you have an "Add more" btn). – mediaashley Apr 30 '13 at 16:43
21

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.
}
  • 1
    how to get the date fields assigned in the field collection? I used the following thing.$item = field_collection_field_get_entity($itemid);print $item->field_course_starts_on['und'][0]['safe_value']; .But no values are printing for it. – harikris Apr 22 '15 at 8:24
  • Thanks for this. I was getting constant Warning: array_flip(): Can only flip STRING and INTEGER values! with ...item_load() and this solved the issue. – Naidim Jun 29 '18 at 14:19
11

You can use entity_metadata_wrapper from the Entity API module:

$node_wrapper = entity_metadata_wrapper('node', $node);
$field_collection = $node_wrapper->field_rich_text_group[0]->value();
dpm($field_collection);

You can loop all the collections as well:

$node_wrapper = entity_metadata_wrapper('node', $node);
foreach ($node_wrapper->field_rich_text_group as $field_collection_wrapper) {
  $field_collection = $field_collection_wrapper->value();
  dpm($field_collection);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.