You are here

public function UserSelection::validateReferenceableNewEntities 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::validateReferenceableNewEntities()

Validates which newly created entities can be referenced.

This method should replicate the logic implemented by \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface::validateReferenceableEntities(), but applied to newly created entities that have not been saved yet.

Parameters

\Drupal\Core\Entity\EntityInterface[] $entities: An array of entities to check.

Return value

\Drupal\Core\Entity\EntityInterface[] The incoming $entities parameter, filtered for valid entities. Array keys are preserved.

Overrides DefaultSelection::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;
}