0

Why is the field collection portion of my node form not saving? I am working on a form that collects travel plans from staff members. As part of this I have a nested field collection to collect itineraries and legs within those itineraries.

to give you a rough description of the related fields:

Field structure diagram

This is not all the fields on the form, but this should give the idea of the structure. I fill out the form, save, and the saved node contains all the other information, but nothing that was in the field collections shows up.

Since country is an additional module, I tried deleting the country field, but that didn't solve anything. I'm also using the conditional fields module extensively to show and hide other fields though the fields in the field collection are not affected by this.

Finally I'm using a custom module called missions (UPDATE: I've disabled the module, and the problem persists, so it seems not to have been the form_alter) to show one field in the field_mr_fc_legs field collection when field_mr_home_leave is checked. The entire code of which can be found below:

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function missions_form_mission_request_node_form_alter(&$form, &$form_state, $form_id)
{
  $form['body'][LANGUAGE_NONE][0] += array(
    '#placeholder' => t('Day 1

*  Activity 1
*  Activity 2

Day 2'),
  );
  $form['field_mr_home_leave'][LANGUAGE_NONE]['#ajax'] = array(
    'callback' => 'missions_hide_home_leave_toggle',
    'wrapper' => 'itinerary-section',
  );
  $form['field_mr_fc_itineraries']['#prefix'] = '<div id="itinerary-section">';
  $form['field_mr_fc_itineraries']['#suffix'] = '</div>';
  if ((!empty($form_state['complete form']['field_mr_home_leave']['und']['#value']) && $form_state['complete form']['field_mr_home_leave']['und']['#value'] == 1) || (empty($form_state['values']['field_mr_home_leave']) && $form['field_mr_home_leave'][LANGUAGE_NONE]['#default_value'] == 1)) {
    $home_leave_toggle_access = true;
  } else {
    $home_leave_toggle_access = false;
  }
  foreach ($form['field_mr_fc_itineraries'][LANGUAGE_NONE] as $key => &$itinerary) {
    if (is_integer($key)) {
      foreach ($itinerary['field_mr_fc_legs'][LANGUAGE_NONE] as $key => &$leg) {
        if (is_integer($key)) {
          $leg['field_mr_fc_leg_home_leave']['#access'] = $home_leave_toggle_access;
        }
      }
    }
  }

}

function missions_hide_home_leave_toggle($form, $form_state) {
  return $form['field_mr_fc_itineraries'];
}

What is the likely cause of this? The Recent log messages report shows just that the node was updated. Is it my module? Is it nested field collections?

What I've tried

  • I disabled my module, and it didn't make a difference
  • I removed the nested field collection and it didn't make a difference.
  • I removed the country field to verify that it wasn't the country module causing the issue.
  • The issue seems to be caused by conditional fields, when I disable that, the form saves properly.
0

2 Answers 2

0

I think, it's because field collection is not part of the node, field collection is a separate entity, which needs to be saved separately.

I once had to add up some number fields and save the value in a field collection field on hook_node_presave

/**
 * Implements hook_node_presave().
 */
function MYMODULE_node_presave($node)
{
  foreach ($node->field_collection_field[LANGUAGE_NONE] as $key => $value) {
    $fc_id = $node->field_collection_field[LANGUAGE_NONE][$key]['value'];
    $field_collection_item = entity_load_single('field_collection_item', $fc_id);
    $field_collection_item->save(TRUE);
  }
}
4
  • They've saved fine everywhere else I've used them.
    – UltraBob
    Commented Mar 10, 2017 at 2:02
  • 1
    @UltraBob but are you doing form alters everywhere else?
    – No Sssweat
    Commented Mar 10, 2017 at 2:30
  • No I don't, and I'm going to try out your solution, but I tried this morning disabling my module and putting an entry in. It still discards the itinerary, so it seems not to be the alter that is causing it. Sorry, I hadn't had a chance to update my question yet. I'll do that next.
    – UltraBob
    Commented Mar 10, 2017 at 4:02
  • 1
    @UltraBob yes, sorry I copy pasted from diff parts
    – No Sssweat
    Commented Mar 10, 2017 at 6:58
0

This appears to be caused by a long running, and judging from the long issue threads about it, complicated and poorly understood, incompatibility between field collections and conditional fields. I fully expected conditional fields to be unable to define conditions for the fields in my field collections, but I didn't expect it to sabotage my field collections even with no dependencies that affect field collection fields. I'm currently rewriting all my field dependencies using #states and custom module validation.

Your Answer

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

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