I am implementing dependent filters in views exposed filter for content:datasource search(Solr search). I have a requirement that in exposed dropdown field when a category is selected, by ajax i need to fetch the sub-categories and show up in another dropdown.For this I tried like below but its not going to callback function only. There is nothing logging in the network console also. Can anyone help me here to resolve.

use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\HtmlCommand;

function module_form_alter(&$form, &$form_state, $form_id) {
$form['field_categories']['#ajax'] = array(
        'callback' => 'module_get_subcategories',
        // 'callback' => '::module_get_subcategories',
        'event' => 'change',
        'wrapper' => 'edit-field-subcategory',
        'progress' => array(
          'type' => 'throbber',
          'message' => NULL,

function module_get_subcategories(&$form, FormStateInterface $form_state) {
   $response = new AjaxResponse();
   $message = 'test';
   $response->addCommand(new HtmlCommand('#edit-field-subcategory', $message));
   return $response;

1 Answer 1


Something is weird in the code, What is that return statement in the last two lines?

For the ajax, try using $form['field_categories']['widget']['#ajax']

Make sure that you have cleared the cache too and that your module is actually called module.

  • Hi @Ahmed, I have removed last line from my code. I tried with $form['field_categories']['widget']['#ajax'] this also and cleared cache. but still not calling.
    – Suraj
    Dec 18, 2019 at 9:32
  • What is your module name please? Dec 18, 2019 at 21:19
  • I'm also facing the same problem. Trying to make an #Ajax callback to views form exposed filter.
    – wilNev
    Oct 5, 2020 at 13:26

Your Answer

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

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