6

Here below is the code loading data present in the field collection.
$field_collection = entity_load('field_collection_item', $ids = FALSE);

In each field collection, there are many fields, i would like to loop through each fields is there a way to do it? Any help?

  • What do you want to with them? You catch an array of objects in $field_collection and can loop through the array with php foreach. – Neograph734 Nov 26 '12 at 14:21
8

I'm current "fighting" width this module, too. I don't know if it is the correct/easiest way, but it works (for me).

First you have to load the ids of the field collection fields of the node:

$fc_fields = field_get_items('node', $node, 'field_image_teaser');

// Extract the field collection item ids
$ids = array();
foreach ($fc_fields as $fc_field) {
    $ids[] = $fc_field['value'];
}

I found the code above at Accessing a field collection (a related question).

To iterate over all fields you can use the following code:

//Get the entities
$collections = field_collection_item_load_multiple($ids);

//Loop over the array - if you have more than one entity
for ($i = 1; $i <= sizeof($collections); $i++) {
    $collection = $collections [$i];

    //Iterate over all member vars of the FieldCollectionItemEntity object
    foreach($collection as $key => $value) {
        //Look for fields - there are some other class vars, too.
        if (strpos ($key, 'field') !== false) {
            //Search for textfields 
            if (isset($value['und']) && is_array($value ['und']) && isset($value['und'][0]['value'])) {
                print "Field ".$key." ".$value['und'][0]['value'];
            }
        }
    }                   
} 
2
$fc_fields = field_info_instances('field_collection_item', $field_name);

This will probably help.

  • For further reference: This is the correct way to get the fields you use for your loop. You might have fields either not beginning with "field_" from custom modules or properties starting with "field_". – schlicki Mar 28 '17 at 8:33
0

The second iteration need to be in the below way. Because of the following reasons,

  • The result of field_collection_item_load_multiple will not be 0 based array.
  • For images the url will not be in the index ['und'][0]['value']

    //Get the entities
    $collections = field_collection_item_load_multiple($ids);
    $loadedValues;
    //Loop over the array - if you have more than one entity
    $i = 0;
    foreach ($collections as $collection) {
    
        //Iterate over all member vars of the FieldCollectionItemEntity object
        foreach($collection as $key => $value) {
            //Look for fields - there are some other class vars, too.
            if (strpos ($key, 'field') !== false) {
                //Search for textfields 
                if (is_array($value) && isset($value['und']) && is_array($value ['und']) && (isset($value['und'][0]['value'])||isset($value['und'][0]['uri']))) {
                    // print "Field ".$key." ".$value['und'][0]['value'];
                    if(isset($value['und'][0]['value']))
                    {
                        $loadedValues[$i][$key] = $value['und'][0]['value'];
                    }
                    else if($value['und'][0]['uri'])
                    {
                        $loadedValues[$i][$key] = $value['und'][0]['uri'];
                    }
                }
            }
        }
        $i++;
    }
    
0

That code by @AlxVallejo will give you the field collection structure (an array of field definitions). You can use that to loop through your entity after that. So, developing on is suggestion:

$fc_fields = field_info_instances('field_collection_item', $field_name);
$field_list = array_keys($fc_fields);

foreach ($field_list as $key) {
  // access your current field using $field_collection_entity->{$key} 
}

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.