With thanks to @jaypan for trying to help me figure this out. I finally have a solution! Googling for the error message I was getting Invalid form POST data
led me to lots of other issues people were having, but not the one I was having. My problem was very simple! I should not have been setting $form_state['rebuild']
to TRUE
.
Doing that caused form_set_cache()
to never be called, and therefore for drupal to quietly (with a watchdog entry) bail out of the ajax call and not return a response beyond the headers.
After ajax started working I was able to determine that the variable I was using to check the value of the field was wrong too, and nowI was not checking for cases such as the add another item buttons where some other portion of the form was being submitted. Now that I've fixed that, it all works. Here is the working code:
/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
function missions_form_mission_request_node_form_alter(&$form, &$form_state, $form_id)
{
$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['values']['field_mr_home_leave']) && $form_state['values']['field_mr_home_leave'][LANGUAGE_NONE][0]['value'] == 1) || (empty($form_state['values']['field_mr_home_leave']) && $form['field_mr_home_leave'][LANGUAGE_NONE]['#default_value'] == 1) || (!empty($form_state['complete form']['field_mr_home_leave']['und']['#value']) && $form_state['complete form']['field_mr_home_leave']['und']['#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'];
}
I hope this helps someone else not spend 3 days trying to debug this. :(