Results of autocomplete field in form depending on another field - Drupal 7

by Cesar   Last Updated December 21, 2017 19:07 PM

I have an autocomplete field in a Drupal form made with Form API. It works great, suggesting the data from a db_query.

However, I would like to alter this db_query depending on the selection made in another field of the form (a select field).

But I don't know how to do it, as I would need to change the db_query parameters on-the-fly (Javascript) but they are hard-coded in PHP!!

Is what I want possible at all? (it can be done in pure HTML + Javascript + AJAX PHP but I cannot imagine how to do it in Drupal).

Tags : 7 forms fields ajax


Answers 2


I have found this reference: How to dynamically reconfigure Drupal's jQuery-based autocomplete at runtime? However, I see it somehow complicated. I prefer this approach of mine that I am using right now (and working for an exposed filter):

I create a hook to add a Javascript Ajax call to the select list. In that Ajax call, I use a cookie associated to the user, and the value of the selected value in the select list, and insert both in a database table.

Now when I make the Autocomplete call, in the db_query I use the user cookie and read the value inserted, so I can restrict the suggestions based on that value.

The only problem is that the call is asynchronous, but if the autocomplete function cannot find the cookie value in the table, will yield a warning in the suggestion list, indicating to type more letters or type again, giving time to the first Ajax call to complete.

Cesar
Cesar
September 17, 2015 16:55 PM

Keep a wild card in your autcomplete menu path something like

function mymodulename_name_menu() {
  $items['example/autocomplete/%/%'] = array(
    'page callback' => '_mymodulename_autocomplete_callback',
    'page arguments' => array(2, 3),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
}

In your form

function mymodulename_my_form($form, &$form_state) {
// Your select field.
  $form['select_field'] = array(
    '#type' => 'select',
    '#options' => array(
      'option1' => 'Option1',
      'option2' => 'Option2',
    ),
    '#ajax' => array(
      'callback' => '_mymodulename_ajaxcallback',
      'wrapper' => 'replace_autocomplete_field',
    ),
  );
  // A container field
  $form['autocomplete_field_container'] = array(
    '#type' => 'container',
    '#tree' => TRUE,
    '#prefix' => '<div id="replace_autocomplete_field">',
    '#suffix' => '</div>',
  );
  $query_parameter = isset($form_state['values']['select_field']) ? $form_state['values']['select_field'] : 'default_value_to_start_with';
  // Your autocomplete field.
  $form['autocomplete_field_container']['autocomplete_field'] = array(
    '#title' => t('My autocomplete field.'),
    '#type' => 'textfield',
    '#autocomplete_path' => "example/autocomplete/$query_parameter",
  );
}

You Automcomplete menu callback

function _mymodulename_autocomplete_callback($query_parameter, $string) {
  // Use $query parameter to decide autocomplete field behavior.
}

Basically I am trying to update the autocomplete field by attaching '#ajax' behaviour to select field, and while updating the autocomplete field we will change the query parameter which db_query will use.

arpitr
arpitr
September 17, 2015 19:54 PM

Related Questions


Multiple Field , same ajax callback

Updated May 07, 2016 08:03 AM



#Ajax change the input wrapper

Updated September 26, 2016 09:03 AM