Archive for September, 2013

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;
}

,

4 Comments