Change checkboxes in a node edit form alter 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(&amp;$form, &amp;$form_state) {
  $form['field_options']['#prefix'] = '&lt;div id="options-choice"&gt;';
  $form['field_options']['#suffix'] = '&lt;/div&gt;';
  $form['field_types']['#ajax'] = [
    'callback' =&gt; 'ajax_options_choice',
    'wrapper' =&gt; 'options-choice',
    'method' =&gt; 'replace',
    'effect' =&gt; '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-&gt;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" =&gt; $bundle))-&gt;fetchAllKeyed(0, 1);
    $options = [];
    foreach ($results as $nid =&gt; $title) {
      $path_alias =  drupal_get_path_alias("node/$nid", "en");
      $value = '&lt;span class="views-field views-field-title"&gt; &lt;span class="field-content"&gt;&lt;a href="' . $path_alias. '"&gt;' . $title . '&lt;/a&gt;&lt;/span&gt; &lt;/span&gt;';
      $options[$nid] = $value;
    }
    $form['field_options'][LANGUAGE_NONE]['#options'] = $options;
  } else {
    $form['field_options'][LANGUAGE_NONE]['#options'] = [];
  }
}

function ajax_options_choice(&amp;$form, &amp;$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?
But when I change the value of the select list, the ajax doesn't seem
  work and options stay the same.


Simply because there is no db_query logic inside the ajax_options_choice callback.

How is Drupal suppose to know that it needs to run again the results db_query and then assign it to #options? 

So essentially, currently, AJAX is executing, but you're just returning the same value that was calculated on initial page load.




  Which ways can I test the ajax works or not?


function ajax_options_choice(&amp;$form, &amp;$form_state) {
  drupal_set_message('Yes AJAX was called');
  dpm('yay dpm works!'); 
  watchdog('my_module', 'ajax was fired', array(), WATCHDOG_NOTICE); // now check /admin/reports/dblog
  return $form['field_options'];
}