I have a problem with an ajax function which doesn't work properly and I don't understand why. I done a similar thing long time ago in Drupal 7 and it seems I forget how it works.
I have a node type with terms references, a type and options. In the node edit, the selection for the type is a select list and for options it's a list of checkboxes. In function of this type, I want only show related options to my user.
Here my code:
function my_module_form_my_content_type_node_form_alter(&$form, &$form_state) {
$form['field_options']['#prefix'] = '<div id="options-choice">';
$form['field_options']['#suffix'] = '</div>';
$form['field_types']['#ajax'] = [
'callback' => 'ajax_options_choice',
'wrapper' => 'options-choice',
'method' => 'replace',
'effect' => 'fade',
];
if(isset($form_state['values']['field_types']) || isset($form['field_types'][LANGUAGE_NONE]['#default_value'][0])) {
$type_tid = isset($form_state['values']['field_types'])? $form_state['values']['field_types']: $form['field_types'][LANGUAGE_NONE]['#default_value'][0];
$type = taxonomy_term_load($type_tid);
$type_option = $type->field_type_option[LANGUAGE_NONE][0]['value'];
$bundle = "my_content_type";
$results = db_query("SELECT nid, title FROM {node} as n inner join {field_data_field_is_$type_option} as fdf on n.nid = fdf.entity_id WHERE n.type = :bundle AND n.status = '1' AND fdf.field_is_". $type_option . "_value = '1'", array(":bundle" => $bundle))->fetchAllKeyed(0, 1);
$options = [];
foreach ($results as $nid => $title) {
$path_alias = drupal_get_path_alias("node/$nid", "en");
$value = '<span class="views-field views-field-title"> <span class="field-content"><a href="' . $path_alias. '">' . $title . '</a></span> </span>';
$options[$nid] = $value;
}
$form['field_options'][LANGUAGE_NONE]['#options'] = $options;
} else {
$form['field_options'][LANGUAGE_NONE]['#options'] = [];
}
}
function ajax_options_choice(&$form, &$form_state) {
return $form['field_options'];
}
`
When I edit a content which have already a type, options are correctly limited. But when I change the value of the select list, the ajax doesn't seem work and options stay the same. I have no error in the firefox or chrome console so I suppose the ajax is simply not execute.
Additionnal questions: Which ways can I test the ajax works or not? Is that possible to use dpm() in the callback?