You are here

protected function UserSelection::buildEntityQuery in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php \Drupal\user\Plugin\EntityReferenceSelection\UserSelection::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.

Overrides DefaultSelection::buildEntityQuery

File

core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php, line 148

Class

UserSelection
Provides specific access control for the user entity type.

Namespace

Drupal\user\Plugin\EntityReferenceSelection

Code

protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
  $query = parent::buildEntityQuery($match, $match_operator);
  $configuration = $this
    ->getConfiguration();

  // Filter out the Anonymous user if the selection handler is configured to
  // exclude it.
  if (!$configuration['include_anonymous']) {
    $query
      ->condition('uid', 0, '<>');
  }

  // The user entity doesn't have a label column.
  if (isset($match)) {
    $query
      ->condition('name', $match, $match_operator);
  }

  // Filter by role.
  if (!empty($configuration['filter']['role'])) {
    $query
      ->condition('roles', $configuration['filter']['role'], 'IN');
  }

  // Adding the permission check is sadly insufficient for users: core
  // requires us to also know about the concept of 'blocked' and 'active'.
  if (!$this->currentUser
    ->hasPermission('administer users')) {
    $query
      ->condition('status', 1);
  }
  return $query;
}