2

I'm getting the error Warning: array_flip(): Can only flip STRING and INTEGER values! in EntityAPIController->load() when using a field collection in another field collection.

Is this a bug in Field collection or the way I load the entities? Either way: How can I solve the warning?

Here's a simplified version of how I load my entities and sub-entities:

foreach ($node->field_chapter[$language] as $chapter) { 
    $items = entity_load('field_collection_item', $chapter);
    foreach ($items as $item) {
        //Some echo's from fields in $item
        foreach ($item->field_paragraph as $paragraphItem) {
            $paragraphs = entity_load('field_collection_item', $paragraphItem);
            foreach ($paragraphs as $paragraph) {
            //Some echos in paragraph
            }
        }
    }
}
1

The problem is with your code.

You need to pass entity_load an array of entity IDs but the structure of a field on an entity is not just an array of IDs.

$item->field_paragraph is an array of field items and field items can come in many different structures based on the type of field. Then there is also the ability to have different languages so the language is part of the structure.

The basic structure is like this:

$entity->field_name['language code'][delta]['column_name']

I think for field collections the entity id is stored in the "value" column so you would access it like this (this assumes you aren't using multiple languages):

$item->field_paragraph[LANGUAGE_NONE][0]['value']

That would get you the entity ID of the first item for that field.

It is generally considered better though to use field_get_items() like this:

// Pass a lang code into field_get_items() if you have a multilingual site.
if ($chapter_items = field_get_items('field_collection_item', $node, 'field_chapter')) {
  $chapter_ids = array();
  foreach ($chapter_items as $chapter_item) {
    $chapter_ids[] = $chapter_item['value'];
  }
  $chapters = entity_load('field_collection_item', $chapter_ids);
  foreach ($chapters as $chapter) {
    // Some echos from fields in $chapter.

    // Pass a lang code into field_get_items() if you have a multilingual site.
    if ($paragraph_items = field_get_items('field_collection_item', $chapter, 'field_paragraph')) {
      $paragraph_ids = array();
      foreach ($paragraph_items as $paragraph_item) {
        $paragraph_ids[] = $paragraph_item['value'];
      }
      $paragraphs = entity_load('field_collection_item', $paragraph_ids);
      foreach ($paragraphs as $paragraph) {
        // Some echos from fields in $paragraph.
      }
    }
  }
}

When you are using entities and fields like this it is a good idea to do some debugging to check out the actual structure of the data you're trying to use.

| improve this answer | |
0

Is $chapter and $paragraphItem an array? It needs to be even if it's just 1 entity id.

If not you could do

$items = entity_load('field_collection_item', array($chapter));
$paragraphs = entity_load('field_collection_item', array($paragraphItem));
| improve this answer | |
  • Yes. They are arrays. The field_chapter is a multidimensional array. So is field_paragraph. Using array($chapter) does nothing but throw more errors, by the way :-p – CaptainCarl Jan 13 '16 at 7:56

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