function hook_query_alter

Same name in other branches
  1. 7.x modules/system/system.api.php \hook_query_alter()
  2. 9 core/lib/Drupal/Core/Database/database.api.php \hook_query_alter()
  3. 8.9.x core/lib/Drupal/Core/Database/database.api.php \hook_query_alter()
  4. 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

6 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().
field_test_entity_query_alter in core/modules/field/tests/modules/field_test/field_test.module
Implements hook_entity_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().

... See full list

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 485

Code

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.