function DefaultSelection::buildEntityQuery

Same name in other branches
  1. 9 core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection::buildEntityQuery()
  2. 8.9.x core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection::buildEntityQuery()
  3. 11.x 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.

11 calls to DefaultSelection::buildEntityQuery()
AllExceptHostEntity::buildEntityQuery in core/modules/system/tests/modules/entity_reference_test/src/Plugin/EntityReferenceSelection/AllExceptHostEntity.php
Builds an EntityQuery to get referenceable entities.
CommentSelection::buildEntityQuery in core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php
Builds an EntityQuery to get referenceable entities.
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.

... See full list

8 methods override DefaultSelection::buildEntityQuery()
AllExceptHostEntity::buildEntityQuery in core/modules/system/tests/modules/entity_reference_test/src/Plugin/EntityReferenceSelection/AllExceptHostEntity.php
Builds an EntityQuery to get referenceable entities.
CommentSelection::buildEntityQuery in core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php
Builds an EntityQuery to get referenceable entities.
FileSelection::buildEntityQuery in core/modules/file/src/Plugin/EntityReferenceSelection/FileSelection.php
Builds an EntityQuery to get referenceable entities.
MediaSelection::buildEntityQuery in core/modules/media/src/Plugin/EntityReferenceSelection/MediaSelection.php
Builds an EntityQuery to get referenceable entities.
NodeSelection::buildEntityQuery in core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php
Builds an EntityQuery to get referenceable entities.

... See full list

File

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

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;
        }
        elseif ($entity_type->hasKey('bundle')) {
            $query->condition($entity_type->getKey('bundle'), $configuration['target_bundles'], 'IN');
        }
        else {
            // If 'target_bundle' is set and entity type doesn't support bundles
            // something is wrong.
            $message = \sprintf("Trying to use non-empty 'target_bundle' configuration on entity type '%s' without bundle support.", $entity_type->id());
            throw new UnsupportedEntityTypeDefinitionException($message);
        }
    }
    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;
}

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