Solution – How to work Ajax property in view exposed filter in drupal 7


It takes a lot of time to find out what is exact reason views exposed filter does not work with #ajax property in form element. In order for Ajax to work, we need the form build info where we check i #ajax has been added to any form elements, and if so, pass this info as settings via Javascript, which get attached to the submitted form on Ajax form submission.

Sample code in your .module file for Drupal 7 as follow :-

/**
* Implements hook_form_views_exposed_form_alter().
*/

function MYMODULE_form_views_exposed_form_alter(&$form, $form_state) {
$form += MYMODULE_view_enable_ajax($form, $form_state);
}

/* * *
* functions for enable ajax call for exposed form elements in views.
*/

function MYMODULE_view_enable_ajax($form, $form_state) {
// Since the exposed form is a GET form, we don’t want it to send a wide
// variety of information.
$form[‘form_build_id’][‘#access’] = FALSE;
$form[‘form_token’][‘#access’] = FALSE;
$form[‘form_id’][‘#access’] = FALSE;

// In order for Ajax to work, we need the form build info. Here we check if
// #ajax has been added to any form elements, and if so, pass this info as
// settings via Javascript, which get attached to the submitted form on Ajax
// form submissions.
foreach (element_children($form) as $key) {
if (isset($form[$key][‘#ajax’])) {
$form[‘#attached’][‘js’][] = array(
‘type’ => ‘setting’,
‘data’ => array(
‘exposed_form_info’ => array(
‘form_id’ => $form[‘#form_id’],
‘form_build_id’ => $form[‘#build_id’],
‘form_token’ => $form[‘#token’],
),
),
);
$form[‘#attached’][‘js’][] = array(
‘type’ => ‘inline’,
‘weight’ => 100,
‘data’ => ‘(function ($) {
Drupal.behaviors.ViewsExposedFormAjax = {
attach: function(context, settings) {
for (ajax_object in Drupal.ajax) {
if (Drupal.ajax[ajax_object].options) {
jQuery.extend(Drupal.ajax[ajax_object].options.data, Drupal.settings.exposed_form_info);
}
}
}
};
})(jQuery);’,
);
break;
}
}

return $form;
}

Advertisements

,

  1. #1 by Vladimir Tulin on December 10, 2014 - 10:56 pm

    Nice! Thank you! )
    Have you figured why does this happen with exposed filters? What’s wrong with them?
    BTW your code expects a single form on a page, multiple will not work.

  2. #2 by takimislam on December 11, 2014 - 12:24 pm

    No, I did not find any reason why it has problem exposed filter but it solved for me when I add this code .-)

    thanks for you reply anyhow.

  3. #3 by Sunny Mathews on January 18, 2016 - 6:10 am

    This was working earlier,but after new update this approach seems to be not working,Any idea

    • #4 by Alex Mazaltov on September 14, 2016 - 12:18 am

      Hello, I am also bumped to this issue. Did anybody found solutions?

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: