protected function ParagraphSelection::buildEntityQuery

File

paragraphs/src/Plugin/EntityReferenceSelection/ParagraphSelection.php, line 248

Class

ParagraphSelection
Default plugin implementation of the Entity Reference Selection plugin.

Namespace

Drupal\paragraphs\Plugin\EntityReferenceSelection

Code

protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
  $target_type = $this->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($this->configuration['target_bundles'])) {
    $target_bundles = array_keys($this
      ->getSortedAllowedTypes());

    // If 'target_bundles' is an empty array, no bundle is referenceable,
    // force the query to never return anything and bail out early.
    if ($target_bundles === []) {
      $query
        ->condition($entity_type
        ->getKey('id'), NULL, '=');
      return $query;
    }
    else {
      $query
        ->condition($entity_type
        ->getKey('bundle'), $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 (!empty($this->configuration['sort'])) {
    $sort_settings = $this->configuration['sort'];
    if ($sort_settings['field'] != '_none') {
      $query
        ->sort($sort_settings['field'], $sort_settings['direction']);
    }
  }
  return $query;
}