You are here

public function UserSelection::validateReferenceableNewEntities in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 189
Contains \Drupal\user\Plugin\EntityReferenceSelection\UserSelection.

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 (!empty($this->configuration['handler_settings']['filter']['role'])) {
    $entities = array_filter($entities, function ($user) {

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

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