You are here

public function DomainSelection::buildEntityQuery in Domain Access 8

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.

Overrides DefaultSelection::buildEntityQuery

File

domain/src/Plugin/EntityReferenceSelection/DomainSelection.php, line 32

Class

DomainSelection
Provides entity reference selections for the domain entity type.

Namespace

Drupal\domain\Plugin\EntityReferenceSelection

Code

public function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
  $query = parent::buildEntityQuery($match, $match_operator);

  // Let administrators do anything.
  if ($this->currentUser
    ->hasPermission('administer domains')) {
    return $query;
  }

  // Can this user access inactive domains?
  if (!$this->currentUser
    ->hasPermission('access inactive domains')) {
    $query
      ->condition('status', 1);
  }

  // Filter domains by the user's assignments, which are controlled by other
  // modules. Those modules must know what type of entity they are dealing
  // with, so look up the entity type and bundle.
  $info = $query
    ->getMetaData('entity_reference_selection_handler');
  if (!empty($info->configuration['entity'])) {
    $context['entity_type'] = $info->configuration['entity']
      ->getEntityTypeId();
    $context['bundle'] = $info->configuration['entity']
      ->bundle();
    $context['field_type'] = $this->fieldType;

    // Load the current user.
    $account = User::load($this->currentUser
      ->id());

    // Run the alter hook.
    $this->moduleHandler
      ->alter('domain_references', $query, $account, $context);
  }
  return $query;
}