0

I have a field collection called field_sections that has inside it it 2 fields:

field_section_title (just plain text field) field_menu_item(field collection with 4 fields)

field_menu_item has 4 fields

 - field_item_title 
 - field_menu_image
 - field_item_price
 - field_item_description

I would like to build an array sections, and those sections are objects that contain menu_items, which are objects that contain menu_items.

I have tried something so far, but unfortunately I can only get the sections and not the nested menu_items and their values. For some reason in their place, only a number is contained instead of the actual field value.

How can I build those arrays?

ps:my apologies for the basic question, but I'm just now getting my hands dirty with PHP and this further complicates things for me.

This is what I did so far:

//prepare array
$sections;
$sectionsCounter = 0;

$node = node_load(8);
//Load sections field collection
$items = field_get_items('node', $node, 'field_section');
foreach ($items as $item) {

   $sectionItems;
   $sectionItemsCounter = 0;
   $fc = field_collection_field_get_entity($item);
   $sectionTitle = $fc->field_section_title["und"][0]["value"];
   //push item into array now
   $sections[$sectionsCounter]["sectionTitle"] = $sectionTitle;
   $sectionsCounter++;

}

I have only managed to get the title of each section so far and push it into an array as an object with property title


UPDATE:

Well ,it seems I somehow got it to work, but I'm worried this is not the best solution. Can someone please have a look and tell me if I'm doing this the correct way?

//Load sections field collection 
  $sections;
  $sectionsCounter = 0;

  $node = node_load(8);
  $items = field_get_items('node', $node, 'field_section');
  foreach ($items as $item) {

     $sectionItems;
     $fc = field_collection_field_get_entity($item);
     $sectionTitle = $fc->field_section_title["und"][0]["value"];
     $sections[$sectionsCounter]["sectionTitle"] = $sectionTitle;
     $itemsField = $fc->field_menu_item["und"][0]["value"];


     $innerCounter = 0;
     for ($i=0; $i <sizeof($fc->field_menu_item["und"]); $i++) { 

        $arr = $fc->field_menu_item["und"][$i]["value"];
        $innerArray = entity_load('field_collection_item', array($arr));
          foreach ($innerArray as $item) {
            $sections[$sectionsCounter]["sectionItem"+$i] = $item;
          }
        $innerCounter++;
     }

     $sectionsCounter++;
  }

I've managed to create 2 objects that each contain:

A section title An object that holds all the fields that the 'inner' field collection of the section contains

1

1 Answer 1

0

You could make use of

entity_load('field_collection_item', array($item_id))

This how-do-i-load-a-field-collection-from-a-node post provides multiple solutions for your problem!!

1
  • I want to read the field collection of the field collection of a node. Reading a field from a node is relatively straightforward. I'd like to carefully traverse the field collection to deeper levels and collect info at each level and build the arrays I need Nov 28, 2014 at 10:45

Your Answer

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

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