diff --git a/js/exposed_form_ajax.js b/js/exposed_form_ajax.js new file mode 100644 index 0000000..6325b97 --- /dev/null +++ b/js/exposed_form_ajax.js @@ -0,0 +1,22 @@ +/** + * @file + * Handles Views' exposed form AJAX data submission. + */ +(function ($) { + + /* + * Gets Form build info from settings and adds it to ajax data. + * + * @see views_exposed_form_ajax_enable(). + */ + 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) diff --git a/views.module b/views.module index b4a37e4..b6445c7 100644 --- a/views.module +++ b/views.module @@ -2023,6 +2023,42 @@ function views_form_views_exposed_form_alter(&$form, &$form_state) { $form['form_build_id']['#access'] = FALSE; $form['form_token']['#access'] = FALSE; $form['form_id']['#access'] = FALSE; + // AJAX behaviors need these data, so we add it back in #after_build. + $form['#after_build'][] = 'views_exposed_form_ajax_enable'; +} + +/** + * Checks whether the exposed form will use ajax and passes required + * form information removed in views_form_views_exposed_form_alter(). + */ +function views_exposed_form_ajax_enable(&$form, &$form_state) { + // 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'], + ), + ), + ); + // Add the javascript behavior that will handle this data. + $form['#attached']['js'][] = array( + 'weight' => 100, + 'data' => drupal_get_path('module', 'views') . '/js/exposed_form_ajax.js', + ); + // We only need to check this once. + break; + } + } + + return $form; } /**