[Drupal] How to implement a hook_views_query_alter() for Drupal 7 views?

| | 2 min read

In this article we will be explaining how to use hook_views_query_alter() for Drupal views. While working on Views, I came across a problem. In my search option there is a drop-down menu listing taxonomy terms, what I have to do is instead of taxonomy terms I need to give two options and taxonomy terms comes as a subset of these two terms.

The figure Menu.jpg shows the fields in my search criteria. The search fields and filters are implemented using Drupal views. See figure below.

Menu

In field Status comes the taxonomy terms. See the figure Dropmenu1.jpg. In this, for each taxonomy term we will be passing the corresponding term id to the view for the search when the view gets executed.

Menu

What we have to do is that taxonomy terms should be replaced with two terms 'Current projects' and 'Finished projects' and all taxonomy terms are classified under these two terms. So it is like these two terms will act like a super set of selected taxonomy terms. But here for each term only one value came be passed, shown below

<div class="leftNav_left_new">
<div class="ul_li_bg_menu_top "> </div>
  <div class="ul_li_bg_menu_mid ">
    <div class="ul_li_bg_menu_mid_left ">
      <ul class="keywrap" id="status_li">									          <li><a href="javascript:void(0);" rel="All" class="status">- Any Type -</a></li>
        <li><a href="javascript:void(0);" class="status" rel="47">Current Projects</a></li>
        <li><a href="javascript:void(0);" class="status"  rel="51">Finished Projects</a></li>
      </ul>
    </div>
  <div class="clear"></div>
</div>
<div class="ul_li_bg_menu_bott "> </div>
</div>

Figure Dropmenu.jpg shows how the terms are displayed in drop-down menu.

Menu

Now here problem is that only one value can be passed at a time. So multiple taxonomy terms can't be searched. In order to achieve this we used hook_views_query_alter(). In this hook we alter the query before executing it. Add the hook in a custom module,and add it in module.view.inc. So how we implemented the function

function modulename_views_query_alter(&$view, &$query) {
  // In this if condition will check view name
  if ($view->name == 'view_name') {
    // In this will check with value is returned when we select the state 
    if ($query->where[0]['conditions'][2]['value'] == '47') {
    // In this for the corresponding field the value has been changed, here we passed multiple values instead
      $query->where[0]['conditions'][2] = array(
                                'field' => 'field_data_field_stat.field_stat_tid',
                                'value' => array(47,48,49,50),
                                'operator' =>'IN' 
                            );
    }
    else if ($query->where[0]['conditions'][2]['value'] == '51') {
      $query->where[0]['conditions'][2] = array(
                                'field' => 'field_data_field_stat.field_stat_tid',
                                'value' => array('51'),
                                'operator' =>'IN' 
                            );
    }
  }
}