Return to Snippet

Revision: 20436
at November 16, 2009 06:42 by ReeceMarsland


Initial Code
<?php
/**
* Implementation of hook_views_query_alter().
*/
function modulename_views_query_alter(&$view, &$query) {
  if ($view->name == 'viewname') {
    $type_clause = FALSE;
    // check to see if we already have a node.type clause
    foreach ($query->where[0]['clauses'] as $clause) {
      if (substr(0, 10, $clause) == 'node.type ') {
        // yes, we do
        $type_clause = TRUE;
      }
    }
   
    // if we don't, let's OR our content types together and add a clause
    if (!$type_clause) {
      // get types from the view
      $types = $view->filter['type']->options['value'];
      // we want an OR query
      $query->where[1]['type'] = 'OR';
      // add each type in turn to the clauses and args for this query
      foreach ($types as $type) {
        $query->where[1]['clauses'][] = "node.type in ('%s')";
        $key = key($query->where[1]['clauses']);
        next($query->where[1]['clauses']);
        $query->where[1]['args'][$key] = $type;
      }
     
    }
  }
}
?>

Initial URL

                                

Initial Description

                                

Initial Title
hook_views_query_alter drupal 6.x

Initial Tags
query, drupal

Initial Language
PHP