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.

// 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

  • 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. – Christopher Jan 15 '16 at 2:09

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

// 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

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

// To save the node after modifying
  • 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" – user2137454 Jul 13 '14 at 12:05
  • Ok, as it seems, the field field_top is a multi-value field. I updated my answer accordingly. – Елин Й. Jul 13 '14 at 12:23
  • It's ok with your update ! ReeeaaalllyyyYYY thanks guy from french country! – user2137454 Jul 13 '14 at 12:31
  • Glad that it works :) – Елин Й. Jul 13 '14 at 12:35

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.