You are here

function hook_query_alter in Drupal 8

Same name and namespace in other branches
  1. 7 modules/system/system.api.php \hook_query_alter()
  2. 9 core/lib/Drupal/Core/Database/database.api.php \hook_query_alter()
  3. 10 core/lib/Drupal/Core/Database/database.api.php \hook_query_alter()

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()

AlterableInterface

SelectInterface

Related topics

5 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.

content_moderation_test_views_views_query_alter in core/modules/content_moderation/tests/modules/content_moderation_test_views/content_moderation_test_views.module
Implements hook_views_query_alter().
database_test_query_alter in core/modules/system/tests/modules/database_test/database_test.module
Implements hook_query_alter().
taxonomy_test_query_alter in core/modules/taxonomy/tests/modules/taxonomy_test/taxonomy_test.module
Implements hook_query_alter().
views_test_data_views_query_alter in core/modules/views/tests/modules/views_test_data/views_test_data.views_execution.inc
Implements hook_views_query_alter().
workspaces_views_query_alter in core/modules/workspaces/workspaces.module
Implements hook_views_query_alter().
1 invocation of hook_query_alter()
DefaultSelection::reAlterQuery in core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php
Helper method: Passes a query to the alteration system again.

File

core/lib/Drupal/Core/Database/database.api.php, line 419
Hooks related to the Database system and the Schema API.

Code

function hook_query_alter(Drupal\Core\Database\Query\AlterableInterface $query) {
  if ($query
    ->hasTag('micro_limit')) {
    $query
      ->range(0, 2);
  }
}