1

I've been researching this for the last few days and I cant find a clear explanation of this issue im facing. I'm receiving an error when a node has an empty collection with no values.

The collection field: field_window_shade
The fields within the collection: field_question, field_answer

Here is my code that is only looping thru the collection array within my node.tpl:

            $wrapper  = entity_metadata_wrapper('node', $node);
            $formtype = field_get_items('node', $node, 'field_window_shade');

            foreach($formtype as $itemid) { 
                 $item = field_collection_field_get_entity($itemid);
                 print $item->field_question['und'][0]['value'];
                 print $item->field_answer['und'][0]['value'];
            }

I am after something like this:

            $wrapper  = entity_metadata_wrapper('node', $node);
            $formtype = field_get_items('node', $node, 'field_window_shade');

            if (isset($wrapper->field_question)) {
              foreach($formtype as $itemid) { 
                 $item = field_collection_field_get_entity($itemid);
                 print $item->field_question['und'][0]['value'];
                 print $item->field_answer['und'][0]['value'];
              }
            }

I've seen this question asked about 5 different times with 10 different solutions and I can't seem to reach a solution here.

Thank you !

1

Did you try field_collection_item in entity_metadata_wrapper? I mean this

$wrapper  = entity_metadata_wrapper('node', $node);
$raw_collection = $wrapper->field_window_shade->value();
$fwrapper = entity_metadata_wrapper('field_collection_item', $raw_collection);

Now use $fwrapper to extract the items (This code is not completely accurate a few modification may be needed)

Visit Extracting a specific data part of a Field Collection Item for more details. Also Programmatically create and update field collection with Entity API and Understanding Field Collection Values can be helpful.

  • You welcome. But your answer which is marked as accepted is not similar to my solution – M a m a D Aug 6 '15 at 10:19
0

I've solved this by using the correct field format (filter) type.

here: $item->field_answer->value->raw(); error is now gone

     <?php
            $wrapper         = entity_metadata_wrapper('node', $node);
            $raw_collection = $wrapper->field_window_shade->value();

            foreach ($wrapper->field_window_shade as $item): 
                print "<div class='faq collection-item'><h3 class='toggle'><p>" . $item->field_question->value() . "</p></h3>";
                print "<div class='faq-content'><div class='faq-content-inner'><p>" . $item->field_answer->value->raw() . "</p></div></div></div>";
            endforeach; 
        ?>

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.