function hook_query_alter

You are here

7 system.api.php hook_query_alter(QueryAlterableInterface $query)
8 system.api.php hook_query_alter(Drupal\Core\Database\Query\AlterableInterface $query)

Perform alterations to a structured query.

Structured (aka dynamic) queries that have tags associated may be altered by any module before the query is executed.

Parameters

$query: A Query object describing the composite parts of a SQL query.

See also

hook_query_TAG_alter()

node_query_node_access_alter()

QueryAlterableInterface

SelectQueryInterface

Related topics

4 functions implement hook_query_alter()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

database_test_query_alter in modules/simpletest/tests/database_test.module
Implements hook_query_alter().
field_test_entity_query_alter in modules/field/tests/field_test.module
Implements hook_entity_query_alter().
hook_entity_query_alter in modules/system/system.api.php
Alter or execute an EntityFieldQuery.
taxonomy_entity_query_alter in modules/taxonomy/taxonomy.module
Implements hook_entity_query_alter().

File

modules/system/system.api.php, line 3228
Hooks provided by Drupal core and the System module.

Code

function hook_query_alter(QueryAlterableInterface $query) {
  if ($query->hasTag('micro_limit')) {
    $query->range(0, 2);
  }
}

Comments

ONLY queries that have already been tagged (at creation time) can be caught by this hook.
If the query you are hoping to alter doesn't already have a tag (which effectively 'publishes' its willingness to let you mess with it) then you can't alter it using this method.

To find out - inspect the source code of the query you are trying to modify.

Looking at where this hook is (maybe) invoked at preExecute() you see that if it's not tagged, the hook is not fired.

For more information : https://drupal.org/node/310077