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")';









