7

I want to get (and to set after) the value of a field collection in a field collection (multidimensional field content).

This is my code :

// The node is a custom content.

// I get the list of first collections fields.
$temp = field_collection_get($node,'field_top');
// Shows the structure you can see below in the screenshot.
dpm($temp->value()[0]);

// Now, I want to get the value of field_website_informations.
// I've tried several ways but I couldn't find the right way.
// The following line returns an error.
$testage = entity_metadata_wrapper($temp->value()[0], 'field_website_informations');

Is it possible to do with the Entity API?

The screenshot :

The screenshot

1
  • Do yourself a favour @user2137454 by learning how to set-up and use a php debugger like xdebug. You'll never look back to dpm() ever again, particularly in Drupal with its heavy reliance on multi-dimensional arrays to pass around data. Commented Jan 15, 2016 at 2:09

1 Answer 1

16

Yes it is possible. Let's say you have a content type with a field collection named field_collection_parent, which has a field collection in it named field_collection_child, which has a field in it named field_child.

// First you need to wrap the node with entity_metadata_wrapper
$node_wrapper = entity_metadata_wrapper('node', $node);

// To get the value of field_child:
$value = $node_wrapper
           ->field_collection_parent
           ->field_collection_child
           ->field_child
           ->value();

// UPDATE: If a field accepts multiple values, treat it like an array
// The following line would get the first value of field_collection_child in the 
// first field_collection_parent.
$value = $node_wrapper
           ->field_collection_parent[0]
           ->field_collection_child[0]
           ->value();

// To set/modify the value of field_child
$node_wrapper
  ->field_collection_parent
  ->field_collection_child
  ->field_child
  ->set('Some new value');

// To save the node after modifying
$node_wrapper->save();
5
  • Thanks for help =) When I try : $value = $temp->field_top->field_website_informations->value(); I obtain this error : Undefined property: EntityListWrapper::$field_website_informations I don't understand... My custom content contain a field collection named "field_top". In the admin panel for field collection, I can see the "field top" who contain the sub-collection field "field_website_informations" Commented Jul 13, 2014 at 12:05
  • Ok, as it seems, the field field_top is a multi-value field. I updated my answer accordingly.
    – Elin Y.
    Commented Jul 13, 2014 at 12:23
  • It's ok with your update ! ReeeaaalllyyyYYY thanks guy from french country! Commented Jul 13, 2014 at 12:31
  • Glad that it works :)
    – Elin Y.
    Commented Jul 13, 2014 at 12:35
  • please see my solution: drupal.stackexchange.com/questions/144466/…
    – pinueve
    Commented Oct 23, 2021 at 1:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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