function hook_query_TAG_alter
Same name in other branches
- 7.x modules/system/system.api.php \hook_query_TAG_alter()
- 9 core/lib/Drupal/Core/Database/database.api.php \hook_query_TAG_alter()
- 8.9.x core/lib/Drupal/Core/Database/database.api.php \hook_query_TAG_alter()
- 10 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
Drupal\Core\Database\Query\AlterableInterface $query: A Query object describing the composite parts of a SQL query.
See also
node_query_node_access_alter()
AlterableInterface
Related topics
7 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.
- 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().
- search_date_query_alter_query_search_node_search_alter in core/
modules/ search/ tests/ modules/ search_date_query_alter/ search_date_query_alter.module - Implements hook_query_TAG_alter().
- search_query_alter_query_search_node_search_alter in core/
modules/ search/ tests/ modules/ search_query_alter/ search_query_alter.module - Implements hook_query_TAG_alter().
- taxonomy_test_query_taxonomy_term_access_alter in core/
modules/ taxonomy/ tests/ modules/ taxonomy_test/ taxonomy_test.module - Implements hook_query_TAG_alter().
File
-
core/
lib/ Drupal/ Core/ Database/ database.api.php, line 510
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');
}
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.