You are here

protected function DefaultSelection::buildEntityQuery in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection::buildEntityQuery()
  2. 9 core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection::buildEntityQuery()

Builds an EntityQuery to get referenceable entities.

Parameters

string|null $match: (Optional) Text to match the label against. Defaults to NULL.

string $match_operator: (Optional) The operation the matching should be done with. Defaults to "CONTAINS".

Return value

\Drupal\Core\Entity\Query\QueryInterface The EntityQuery object with the basic conditions and sorting applied to it.

3 calls to DefaultSelection::buildEntityQuery()
DefaultSelection::countReferenceableEntities in core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php
Counts entities that are referenceable.
DefaultSelection::getReferenceableEntities in core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php
Gets the list of referenceable entities.
DefaultSelection::validateReferenceableEntities in core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php
Validates which existing entities can be referenced.
2 methods override DefaultSelection::buildEntityQuery()
NodeSelection::buildEntityQuery in core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php
Builds an EntityQuery to get referenceable entities.
TestSelection::buildEntityQuery in core/modules/block_content/tests/modules/block_content_test/src/Plugin/EntityReferenceSelection/TestSelection.php
Builds an EntityQuery to get referenceable entities.

File

core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php, line 437

Class

DefaultSelection
Default plugin implementation of the Entity Reference Selection plugin.

Namespace

Drupal\Core\Entity\Plugin\EntityReferenceSelection

Code

protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
  $configuration = $this
    ->getConfiguration();
  $target_type = $configuration['target_type'];
  $entity_type = $this->entityTypeManager
    ->getDefinition($target_type);
  $query = $this->entityTypeManager
    ->getStorage($target_type)
    ->getQuery();
  $query
    ->accessCheck(TRUE);

  // If 'target_bundles' is NULL, all bundles are referenceable, no further
  // conditions are needed.
  if (is_array($configuration['target_bundles'])) {

    // If 'target_bundles' is an empty array, no bundle is referenceable,
    // force the query to never return anything and bail out early.
    if ($configuration['target_bundles'] === []) {
      $query
        ->condition($entity_type
        ->getKey('id'), NULL, '=');
      return $query;
    }
    else {
      $query
        ->condition($entity_type
        ->getKey('bundle'), $configuration['target_bundles'], 'IN');
    }
  }
  if (isset($match) && ($label_key = $entity_type
    ->getKey('label'))) {
    $query
      ->condition($label_key, $match, $match_operator);
  }

  // Add entity-access tag.
  $query
    ->addTag($target_type . '_access');

  // Add the Selection handler for system_query_entity_reference_alter().
  $query
    ->addTag('entity_reference');
  $query
    ->addMetaData('entity_reference_selection_handler', $this);

  // Add the sort option.
  if ($configuration['sort']['field'] !== '_none') {
    $query
      ->sort($configuration['sort']['field'], $configuration['sort']['direction']);
  }
  return $query;
}