2

I'm looking for a way to modify views' resulting query string (URL) after applying a filter, but have been unsuccessful so far.

I have a view with exposed filters, taxonomy lists to be specific. Hitting apply generates something like:  www.mysite.com/myview?foo=123

My goal is to have it instead generate:  www.mysite.com/myview?foo=bar

I know views' exposed filters depends on tid for the filter to work when visiting a page (e.g. pasting a link and visiting the page), and I have some hook_views_pre_buildcode to handle that. My issue is going the other direction. What hook, function, or other approach can I use to modify that?

For reference I've tried replacing view object contents in hook_views_pre_build, hook_views_post_build, hook_views_pre_execute, hook_views_post_execute, and hook_views_query_alter, but nothing seems to have the desired effect. I do not want contextual filters due to the need for order independence.

1 Answer 1

1

Came up with a solution using hook_views_pre_render combined with a JS history.replacestate. More of a workaround, but it does the job. Would still like to find a way to modify the view object or a PHP hook though.

Here's a stripped down version of the php hook implementation:

function mymodule_views_pre_render(&$view) {
  if ($view->name == "myviewname") {
    $path_settings = array("mymodule" => array('query_params' => array()));
    foreach ($_GET as $key => $val){
      if (array_key_exists($key, $view->exposed_input)) {
        $path_settings['mymodule']['query_params'][$key] = $view->exposed_input[$key];
      }
    }
    drupal_add_js($path_settings, 'setting');
  }
}

Then in the module's js attached behaviors:

function tidyViewsQueryString() {
  currentLocation = window.location.href;
  for (var key in Drupal.settings.mymodule.query_params) {
    if (Drupal.settings.mymodule.query_params.hasOwnProperty(key)) {
      currentLocation = updateUrlParameter(currentLocation, key, Drupal.settings.mymodule.query_params[key])
    }
  }
  if (currentLocation != window.location.href){
    window.history.replaceState(null, document.title, currentLocation);
  }
}

//From https://gist.github.com/niyazpk/f8ac616f181f6042d1e0
function updateUrlParameter(uri, key, value) {
    // remove the hash part before operating on the uri
    var i = uri.indexOf('#');
    var hash = i === -1 ? ''  : uri.substr(i);
         uri = i === -1 ? uri : uri.substr(0, i);

    var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
    var separator = uri.indexOf('?') !== -1 ? "&" : "?";
    if (uri.match(re)) {
        if (value.length > 0)
          uri = uri.replace(re, '$1' + key + "=" + value + '$2');
        else 
          uri = uri.replace(re, '$1' + '$2')
    } else {
        uri = uri + separator + key + "=" + value;
    }
    return uri + hash;  // finally append the hash as well
}

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.