1

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?

0

1 Answer 1

0

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(&$form, &$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'];
}
1
  • 1
    Thanks for your help. My condition was good (if my select has a value, Drupal relaunch correctly the db_query). My error was the field_types is a container which contains the select field form. So I finally fix my problem when I define my #ajax to the level of my "select", so $form['field_types'][LANGUAGE_NONE]['#ajax']. The ajax is finally call.
    – Claire D
    Feb 6, 2019 at 20:54

Your Answer

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

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