I have a content type that has a field that is a field collection that allows multiple values. One of the fields within that field collection is another field collection which also allows multiple values.

I am trying to set up an ajax handler that simulates the Add another item button when a value is selected in a select list to generate X number of parent field collection rows. Additionally, within each parent, I need to generate Y number of child field collection rows.

I am able to generate the parent field collection fields without any problem, but I am having trouble with the nested/child field collections.

Here is a snippet of the code:

// Parent field collection fields:

        $items_count = count( $arrServices );
        $items = &$form_state['field']['field_MYFIELDCOLLECTION'][ $lang ];
        $items['items_count'] = $items_count;
        $items['field']['cardinality'] = $items_count;

        $form['field_MYFIELDCOLLECTION'][ $lang ] = field_multiple_value_form($items['field'], $items['instance'], LANGUAGE_NONE, array(), $form, $form_state);

    // ^^^^^^^^^  This works ^^^^^^^^^^^^^^^^^

    // Generate children
      $i = 0;

      foreach ( $arrServices as $service) {

        $arrCats = _mymodule_get_child_list( $service->tid );

        $item_count = count($arrCats );

        // Children seem to be handled differently in $form_state
        $items = &$form_state['#parents']['field_MYFIELDCOLLECTION'][ $lang ][$i]['#fields']['field_CHILD_FC'][ $lang];
        $items['items_count'] = $items_count;
        $items['field']['cardinality'] = $items_count;

        $form['field_MYFIELDCOLLECTION'][ $lang ][ $i ]['field_CHILD_FC'][ $lang ] = field_multiple_value_form($items['field'], $items['instance'], LANGUAGE_NONE, array(), $form, $form_state);

          // ^^^^ This line causes an ajax error:
          // EntityMalformedException: Missing bundle property on entity of type field_collection_item in entity_extract_ids() 
          // on line 7929 of includes/common.inc
          // The assignment of $items may not be correct


Can someone point me in the right direction on this?

Thanks in advance!

  • I don't know the exact structure of nested field collections, but have you tried faking up a content type with nested field collections then dumping the form to see how it gets generated? – Quint Apr 4 '17 at 22:22
  • Thanks,I actually dumped out the $form and $form_state variables after clicking on the Add another item button but the structures are pretty complex and I was thinking that it would be better to use the API than to dig that deep. I guess that is what I am looking for - the correct API calls/arguments. – MGMD Apr 6 '17 at 0:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.