0

I'm using Views 7.x-3.11 with Better Exposed Filters and Ajax auto-submit in a panels page, and running into some issues that seem to be stemming from code in Views/js/ajax_view.js. I want to make the exposed filters bookmarkable, and I also need to deep link to certain result sets. Because of this, I added some custom js to add the selected form parameters to the URL as the user selects them.

I also have a section that lists all the currently active terms, where a user can click to remove them. In order to update the results from here, I'm calling $('.view-my-view').trigger('RefreshView');.

By default, from here I can see that the ajax_path for submitting the form has been set to the URL with the query string, which doesn't work. From what I can tell, the native ajax behavior is trying to submit the serialized form data to an already-parameterized URL. To change this behavior, at line 38 of ajax_view.js I've changed var queryString = window.location.search || ''; to var queryString = '';

It's pretty apparent that this is going to cause issues with some sort of intended functionality, but I can't determine what that is. I'm afraid I'm losing sight of something bigger-picture here, or not aware of something about how Views handles ajax submission typically.

Can anyone provide an example of a situation where you'd need the original code? Or what the intended purposed of it is?

Edit: Follow-up question that's a little more practical - is there a way I can make an equivalent change in my custom js that would override the default code in ajax_view.js, so that I don't need to alter the module?

0

I had a similar problem where I wanted to link to filtered versions of an ajaxified view, but the aforementioned line of code in ajax_views.js prevented the view from displaying all results when all filters where cleared. In my use case, I wanted all views filter identifiers removed, but in yours, as I understood it, you need the clicked filter value removed, so you could modify the code accordingly.

The first step is identifying all valid filter identifiers. We want to do that, so we don't parse out all of the query string's parameters, since some of them might serve another functionality. If you don't care about creating a dependency to the better_exposed_filters module you can find the views identifiers from Drupal.settings.better_exposed_filters.views.[view_name].[view_display].filters. Otherwise, you could expose them in your own Drupal setting through a views hook:

function hook_views_pre_render(&$view) {
  if ($view->display_handler->has_exposed) {
    $valid_identifiers = array();

    foreach ($view->filter as $name => $filter) {
      if (!$filter->options['exposed']) {
        continue;
      }

      $valid_identifiers[] = $filter->options['expose']['identifier'];
    }

    drupal_add_js(array('validViewsIdentifiers' => $valid_identifiers), 'setting');
  }
}

So, now we have the identifiers and in our custom js file we can change the related properties of the views instance(s). The code for my use case is the following, but you can change accordingly:

Drupal.behaviors.fixAjaxQueryString = {
  attach: function(context, settings) {
    if (!Drupal.views || !Drupal.views.instances) {
      return;
    }

    for (var i in Drupal.views.instances) {
      var instance = Drupal.views.instances[i],
          url = instance.element_settings.url;

      // Check for any valid views filter identifiers and remove them
      var validViewsIdentifiers = Drupal.settings.validViewsIdentifiers || '';
      if (validViewsIdentifiers) {
        url = decodeURIComponent(url);

        for (var j = 0; j < validViewsIdentifiers.length; j++) {
          var regExpr = '\\&?' + validViewsIdentifiers[j] + '(\\[\\])?=[^&]*';
          var regExprObj = new RegExp(regExpr, 'g');

          url = url.replace(regExprObj, "");
        }
      }

      // Only the .options.url part is needed for the desired functionality
      // The rest is modified for consistency
      instance.element_settings.url = url;

      if (instance.exposedFormAjax) {
        instance.exposedFormAjax.options.url = url;
        instance.exposedFormAjax.element_settings = instance.element_settings;
        instance.exposedFormAjax.url = url;
      }

      if (instance.pagerAjax) {
        instance.pagerAjax.options.url = url;
        instance.pagerAjax.element_settings = instance.element_settings;
        instance.pagerAjax.url = url;
      }

      if (instance.refreshViewAjax) {
        instance.refreshViewAjax.options.url = url;
        instance.refreshViewAjax.element_settings = instance.element_settings;
        instance.refreshViewAjax.url = url;
      }
    }
  }
};
0

This module will allow you to make exposed filters bookmarkable: Views Ajax History Module

It utilizes the History.js library

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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