Modifying Views

June 25, 2009
blog author

Appno Blogger

Appnovation Coop

We use the Views UI a lot to create custom pages, blocks, etc. Sometimes it does not give us exactly what we want and we would like to modify the generated sql query. It's possible to achieve this through the 'views_query_alter' hook. For example, we can do something like:

 

function example_module_views_query_alter(&$view, &$query) {
  if($view->name == 'view_to_modify') {
    $query->where[0]['clauses'][] = 'node.nid IN (10, 20, 30, 40, 50)';
    ...
  }
}

That modifies the WHERE part of the sql query of the particular view. It helps to print out the 'query' variable before making any modifications to it.