function UserSelection::buildEntityQuery

Same name in other branches
  1. 8.9.x core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php \Drupal\user\Plugin\EntityReferenceSelection\UserSelection::buildEntityQuery()
  2. 10 core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php \Drupal\user\Plugin\EntityReferenceSelection\UserSelection::buildEntityQuery()
  3. 11.x core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php \Drupal\user\Plugin\EntityReferenceSelection\UserSelection::buildEntityQuery()

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;
}

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