function hook_entity_query_alter

Same name in other branches
  1. 7.x modules/system/system.api.php \hook_entity_query_alter()
  2. 10 core/lib/Drupal/Core/Entity/entity.api.php \hook_entity_query_alter()

Alter an entity query.

Parameters

\Drupal\Core\Entity\Query\QueryInterface $query: The entity query.

See also

hook_entity_query_ENTITY_TYPE_alter()

hook_entity_query_tag__TAG_alter()

\Drupal\Core\Entity\Query\QueryInterface

Related topics

File

core/lib/Drupal/Core/Entity/entity.api.php, line 2321

Code

function hook_entity_query_alter(\Drupal\Core\Entity\Query\QueryInterface $query) : void {
    if ($query->hasTag('entity_reference')) {
        $entityType = \Drupal::entityTypeManager()->getDefinition($query->getEntityTypeId());
        $query->sort($entityType->getKey('id'), 'desc');
    }
}

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