Posted By

ReeceMarsland on 11/16/09


Tagged

query drupal views hook


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

level09


hook_views_query_alter drupal 6.x


 / Published in: PHP
 

  1. <?php
  2. /**
  3. * Implementation of hook_views_query_alter().
  4. */
  5. function modulename_views_query_alter(&$view, &$query) {
  6. if ($view->name == 'viewname') {
  7. $type_clause = FALSE;
  8. // check to see if we already have a node.type clause
  9. foreach ($query->where[0]['clauses'] as $clause) {
  10. if (substr(0, 10, $clause) == 'node.type ') {
  11. // yes, we do
  12. $type_clause = TRUE;
  13. }
  14. }
  15.  
  16. // if we don't, let's OR our content types together and add a clause
  17. if (!$type_clause) {
  18. // get types from the view
  19. $types = $view->filter['type']->options['value'];
  20. // we want an OR query
  21. $query->where[1]['type'] = 'OR';
  22. // add each type in turn to the clauses and args for this query
  23. foreach ($types as $type) {
  24. $query->where[1]['clauses'][] = "node.type in ('%s')";
  25. $key = key($query->where[1]['clauses']);
  26. next($query->where[1]['clauses']);
  27. $query->where[1]['args'][$key] = $type;
  28. }
  29.  
  30. }
  31. }
  32. }
  33. ?>

Report this snippet  

You need to login to post a comment.