You are here

public function UserSelection::validateReferenceableNewEntities in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php \Drupal\user\Plugin\EntityReferenceSelection\UserSelection::validateReferenceableNewEntities()
  2. 9 core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php \Drupal\user\Plugin\EntityReferenceSelection\UserSelection::validateReferenceableNewEntities()

File

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

Class

UserSelection
Provides specific access control for the user entity type.

Namespace

Drupal\user\Plugin\EntityReferenceSelection

Code

public function validateReferenceableNewEntities(array $entities) {
  $entities = parent::validateReferenceableNewEntities($entities);

  // Mirror the conditions checked in buildEntityQuery().
  if ($role = $this
    ->getConfiguration()['filter']['role']) {
    $entities = array_filter($entities, function ($user) use ($role) {

      /** @var \Drupal\user\UserInterface $user */
      return !empty(array_intersect($user
        ->getRoles(), $role));
    });
  }
  if (!$this->currentUser
    ->hasPermission('administer users')) {
    $entities = array_filter($entities, function ($user) {

      /** @var \Drupal\user\UserInterface $user */
      return $user
        ->isActive();
    });
  }
  return $entities;
}