hook_db_rewrite_sql

Definition

hook_db_rewrite_sql($query, $primary_table, $primary_field, $args)
developer/hooks/core.php, line 291

Description

Add JOIN and WHERE statements to queries and decide whether the primary_field shall be made DISTINCT. For node objects, primary field is always called nid. For taxonomy terms, it is tid and for vocabularies it is vid. For comments, it is cid. Note that at the moment only node queries are rewritten, so tid, vid and cid are for future use only. Primary table is the table where the primary object (node, file, term_node etc.) is.

You shall return an associative array. Possible keys are 'join', 'where' and 'distinct'. The value of 'distinct' shall be 1 if you want that the primary_field made DISTINCT.

Parameters

$query Query to be rewritten.

$primary_table Name or alias of the table which has the primary key field for this query. Possible values are: comments, forum, node, term_data, vocabulary. Typical table names would be: {blocks}, {comments}, {forum}, {node}, {menu}, {term_data} or {vocabulary}. However, it is more common for $primary_table to contain the usual table alias: b, c, f, n, m, t or v.

$primary_field Name of the primary field.

$args Array of additional arguments.

Return value

An array of join statements, where statements, distinct decision.

Related topics

Namesort iconDescription
HooksAllow modules to interact with the Drupal core.

Code

<?php
function hook_db_rewrite_sql($query, $primary_table, $primary_field, $args) {
  switch ($primary_field) {
    case 'nid':
      // this query deals with node objects
      $return = array();
      if ($primary_table != 'n') {
        $return['join'] = "LEFT JOIN {node} n ON $primary_table.nid = n.nid";
      }
      $return['where'] = 'created >' . mktime(0, 0, 0, 1, 1, 2005);
      return $return;
      break;
    case 'tid':
      // this query deals with taxonomy objects
      break;
    case 'vid':
      // this query deals with vocabulary objects
      break;
  }
}
?>
 
 

Drupal is a registered trademark of Dries Buytaert.