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

Perform alterations to a structured query for a given tag.

Some common tags include:

  • 'entity_reference': For queries that return entities that may be referenced by an entity reference field.
  • ENTITY_TYPE . '_access': For queries of entities that will be displayed in a listing (e.g., from Views) and therefore require access control.

Parameters

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

See also

hook_query_alter()

node_query_node_access_alter()

AlterableInterface

SelectInterface

Related topics

18 functions implement hook_query_TAG_alter()

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

block_content_query_entity_reference_alter in core/modules/block_content/block_content.module
Implements hook_query_TAG_alter().
config_test_entity_query_tag__config_query_test__config_entity_query_alter_hook_test_alter in core/modules/config/tests/config_test/config_test.module
Implements hook_entity_query_tag__ENTITY_TYPE__TAG_alter().
database_test_query_database_test_alter_remove_range_alter in core/modules/system/tests/modules/database_test/database_test.module
Implements hook_query_TAG_alter().
entity_test_query_entity_test_access_alter in core/modules/system/tests/modules/entity_test/entity_test.module
Implements hook_query_entity_test_access_alter().
field_test_entity_query_entity_test_mulrev_alter in core/modules/field/tests/modules/field_test/field_test.module
Implements hook_entity_query_ENTITY_TYPE_alter() for 'entity_test_mulrev'.

... See full list

File

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

Code

function hook_query_TAG_alter(Drupal\Core\Database\Query\AlterableInterface $query) {

  // This is an example of a possible hook_query_media_access_alter()
  // implementation. In other words, alter queries of media entities that
  // require access control (have the 'media_access' query tag).
  // Determine which media entities we want to remove from the query. In this
  // example, we hard-code some media IDs.
  $media_entities_to_hide = [
    1,
    3,
  ];

  // In this example, we're only interested in applying our media access
  // restrictions to SELECT queries. hook_media_access() can be used to apply
  // access control to 'update' and 'delete' operations.
  if (!$query instanceof SelectInterface) {
    return;
  }

  // The tables in the query. This can include media entity tables and other
  // tables. Tables might be joined more than once, with aliases.
  $query_tables = $query
    ->getTables();

  // The tables belonging to media entity storage.
  $table_mapping = \Drupal::entityTypeManager()
    ->getStorage('media')
    ->getTableMapping();
  $media_tables = $table_mapping
    ->getTableNames();

  // For each table in the query, if it's a media entity storage table, add a
  // condition to filter out records belonging to a media entity that we wish
  // to hide.
  foreach ($query_tables as $alias => $info) {

    // Skip over subqueries.
    if ($info['table'] instanceof SelectInterface) {
      continue;
    }
    $real_table_name = $info['table'];
    if (in_array($real_table_name, $media_tables)) {
      $query
        ->condition("{$alias}.mid", $media_entities_to_hide, 'NOT IN');
    }
  }
}