1

Appnovation Technologies: Using Hook Views Query Alter

http://www.appnovation.com

Fri, Sep 3, 2010 by Brandon Tate

Building a website I recently ran into an issue where I had HTML search facets interacting with Drupal views. The user would click on the facet and it would filter the view depending on the argument. I ended up implementing hook_views_query_alter. To make sure the values I wanted to filter on were in the view, I added the search facets into the view as fields. This automatically joins the necessary tables so that you don’t have to.
Implementing hook_views_query_alter requires two parameters, view and query, both are passed by reference. Since this hook is called for every view the first thing I did was check that I was altering the right query by adding an if statement that checks the view name. After that you're free to modify the query as you wish. In my example, I looped through the search facets and added them as where statements and arguments to the query.

function search_views_query_alter(&$view, &$query) {
  if($view->name == 'example_view_name'){
    foreach($parsed_url AS $key => $value){
      $query->where[0]['clauses'][] = 'profile_values_profile_' . $key . '.value IN ("%s")';

Read »
admin's picture
Created by admin 1 year 22 weeks ago – Made popular 1 year 22 weeks ago
Category: Open Source CMS   Tags:

Your Ad Here

User login

Who's online

There are currently 0 users and 1 guest online.

Best karma users

  1. bands
  2. sri's picture
    sri
  3. shashi
  4. sunnyholic
  5. admin user