Verified Commit 19ae323f authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3001496 by jonathanshaw, SmovS, Prashant.c, ptmkenny, smustgrave,...

Issue #3001496 by jonathanshaw, SmovS, Prashant.c, ptmkenny, smustgrave, manish-31, alexpott, bojanz, tedbow, elber, Berdir, quietone: Add an alter hook to EntityQuery

(cherry picked from commit c5365987)
parent 4e9481d1
Loading
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -78,6 +78,9 @@ public function condition($property, $value = NULL, $operator = NULL, $langcode
   * {@inheritdoc}
   */
  public function execute() {
    // Invoke entity query alter hooks.
    $this->alter();

    // Load the relevant config records.
    $configs = $this->loadRecords();

+25 −0
Original line number Diff line number Diff line
@@ -510,4 +510,29 @@ public static function getClass(array $namespaces, $short_class_name) {
    }
  }

  /**
   * Invoke hooks to allow modules to alter the entity query.
   *
   * Modules may alter all queries or only those having a particular tag.
   * Alteration happens before the query is prepared for execution, so that
   * the alterations then get prepared in the same way.
   *
   * @return $this
   *   Returns the called object.
   */
  protected function alter(): QueryInterface {
    $hooks = ['entity_query', 'entity_query_' . $this->getEntityTypeId()];
    if ($this->alterTags) {
      foreach ($this->alterTags as $tag => $value) {
        // Tags and entity type ids may well contain single underscores, and
        // 'tag' is a possible entity type id. Therefore use double underscores
        // to avoid collisions.
        $hooks[] = 'entity_query_tag__' . $tag;
        $hooks[] = 'entity_query_tag__' . $this->getEntityTypeId() . '__' . $tag;
      }
    }
    \Drupal::moduleHandler()->alter($hooks, $this);
    return $this;
  }

}
+1 −0
Original line number Diff line number Diff line
@@ -77,6 +77,7 @@ public function __construct(EntityTypeInterface $entity_type, $conjunction, Conn
   */
  public function execute() {
    return $this
      ->alter()
      ->prepare()
      ->compile()
      ->addSort()
+59 −0
Original line number Diff line number Diff line
@@ -2289,6 +2289,65 @@ function hook_entity_extra_field_info_alter(&$info) {
  }
}

/**
 * Alter an entity query.
 *
 * @param \Drupal\Core\Entity\Query\QueryInterface $query
 *   The entity query.
 *
 * @see hook_entity_query_ENTITY_TYPE_alter()
 * @see hook_entity_query_tag__TAG_alter()
 * @see \Drupal\Core\Entity\Query\QueryInterface
 */
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');
  }
}

/**
 * Alter an entity query for a specific entity type.
 *
 * @param \Drupal\Core\Entity\Query\QueryInterface $query
 *   The entity query.
 *
 * @see hook_entity_query_alter()
 * @see \Drupal\Core\Entity\Query\QueryInterface
 */
function hook_entity_query_ENTITY_TYPE_alter(\Drupal\Core\Entity\Query\QueryInterface $query): void {
  $query->condition('id', '1', '<>');
}

/**
 * Alter an entity query that has a specific tag.
 *
 * @param \Drupal\Core\Entity\Query\QueryInterface $query
 *   The entity query.
 *
 * @see hook_entity_query_alter()
 * @see hook_entity_query_tag__ENTITY_TYPE__TAG_alter()
 * @see \Drupal\Core\Entity\Query\QueryInterface
 */
function hook_entity_query_tag__TAG_alter(\Drupal\Core\Entity\Query\QueryInterface $query): void {
  $entityType = \Drupal::entityTypeManager()->getDefinition($query->getEntityTypeId());
  $query->sort($entityType->getKey('id'), 'desc');
}

/**
 * Alter an entity query for a specific entity type that has a specific tag.
 *
 * @param \Drupal\Core\Entity\Query\QueryInterface $query
 *   The entity query.
 *
 * @see hook_entity_query_ENTITY_TYPE_alter()
 * @see hook_entity_query_tag__TAG_alter()
 * @see \Drupal\Core\Entity\Query\QueryInterface
 */
function hook_entity_query_tag__ENTITY_TYPE__TAG_alter(\Drupal\Core\Entity\Query\QueryInterface $query): void {
  $query->condition('id', '1', '<>');
}

/**
 * @} End of "addtogroup hooks".
 */
+14 −0
Original line number Diff line number Diff line
@@ -7,6 +7,8 @@

require_once dirname(__FILE__) . '/config_test.hooks.inc';

use Drupal\Core\Entity\Query\QueryInterface;

/**
 * Implements hook_cache_flush().
 */
@@ -41,3 +43,15 @@ function config_test_entity_type_alter(array &$entity_types) {
    $entity_types['config_test']->set('lookup_keys', ['uuid', 'style']);
  }
}

/**
 * Implements hook_entity_query_tag__ENTITY_TYPE__TAG_alter().
 *
 * Entity type is 'config_query_test' and tag is
 * 'config_entity_query_alter_hook_test'.
 *
 * @see Drupal\KernelTests\Core\Entity\ConfigEntityQueryTest::testAlterHook
 */
function config_test_entity_query_tag__config_query_test__config_entity_query_alter_hook_test_alter(QueryInterface $query): void {
  $query->condition('id', '7', '<>');
}
Loading