1

I have a content type with a field collection. This field collection has, as fields, other field collections. It works well, it groups the content for a node perfectly, but when trying to extract the data programmatically, I cannot access the values introduced. How would be the method. I tried with this piece of code, but it throws an error. Also not sure if adequate to my case (nested field collections):

$node = node_load($node->nid);
$node_wrapper = entity_metadata_wrapper('node', $node);

foreach ($node_wrapper->field_rich_text_group as $field_collection_wrapper) {
            $field_collection = $field_collection_wrapper->value();
                print_r($field_collection);
}
1

Try this :

$node = node_load($node->nid);
$node_wrapper = entity_metadata_wrapper('node', $node);

foreach ($node_wrapper->field_rich_text_group->value() as $value) {
  $field_collection_wrapper = entity_metadata_wrapper('field_collection_item', $value);
  $field_collection_myfield = $field_collection_wrapper->field_myfield->value();
  print_r($field_collection_myfield);
}

Field Collection is an entity, so if you want to access its field you have to pass the field collection in entity_metadata_wrapper. Then you can access the value of your fields in field collection like you do with your $node_wrapper

  • I have tried, but still an error and does not return the expected values. I am going to start from the beginning, with a single level field collection, and so on... I'll be back to report. Thanks anyway, surely you are right but I am missing something. – Cesar Oct 29 '15 at 7:13
0

I have managed to get the value by direct access.

$node = node_load($node->nid);
$node_wrapper = entity_metadata_wrapper('node', $node);
echo $node_wrapper->field_rich_text_group->field_meta_datos->field_meta_title->value();

I guess that the foreach would be needed if I have multivalued data. Please someone explains to me why this solution works and pbonnefoi does not print anything and throws error. Adapting from his code,

$node = node_load($node->nid);
$node_wrapper = entity_metadata_wrapper('node', $node);
foreach ($node_wrapper->field_rich_text_group->value() as $value) {

$field_collection_wrapper = entity_metadata_wrapper('field_collection_item', $value);

 $field_collection_myfield = entity_metadata_wrapper("field_collection_item", $field_collection_wrapper->field_meta_datos->value());

print $field_collection_myfield->field_meta_title->value();

Print OK but throws this error, probably because in the loop there is something it does not locate:

`// EntityMetadataWrapperException: Unknown data property field_meta_datos. en EntityStructureWrapper->getPropertyInfo() (line 335 of /home/public_html/sites/all/modules/entity/includes/entity.wrapper.inc).`
  • What is meta_datos ? Is it a field collection ? – pbonnefoi Oct 29 '15 at 8:59
  • Yes. What is even worst; Now I am not able to reproduce the structured jerarchy above. I have come to the final strcuture: ` $node_wrapper->field_seo->field_seo_com->field_seo_general->field_meta_title->value() just for having seo boxes for several domains. Instead of two levels I have 4 levels and cannot get it working with less. I am not understanding the nested field collection. – Cesar Oct 29 '15 at 11:12
  • Are your field collection set to multiple ? In that case you have to use nested foreach. – pbonnefoi Oct 29 '15 at 11:20
  • No. it is multiple levels, not multiple values. I do these steps: In the content type, I create field_seo_general (embedded) and field_seo (hidden) just to get them available. In the field_seo which is the last level, I create the field field_meta_title. In field_seo_general I create field__com and field__us, which are field collection. Then, editing these two, I assign existing field, which is the type field_seo. So I can access via echo $node_wrapper->field_seo_general->field__com->field_seo->field_meta_title->value(); . There are no multiple values. I am doing right? It is so convoluted. – Cesar Oct 29 '15 at 11:40
  • Well I have to say I have trouble understanding your problem. For me you only use field collection if you need multiple instance of a group of field ? – pbonnefoi Oct 29 '15 at 14:06

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.