View source
<?php
namespace Drupal\user\Plugin\EntityReferenceSelection;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\RoleInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class UserSelection extends DefaultSelection {
protected $connection;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, AccountInterface $current_user, Connection $connection, EntityFieldManagerInterface $entity_field_manager = NULL, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, EntityRepositoryInterface $entity_repository = NULL) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $module_handler, $current_user, $entity_field_manager, $entity_type_bundle_info, $entity_repository);
$this->connection = $connection;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('module_handler'), $container
->get('current_user'), $container
->get('database'), $container
->get('entity_field.manager'), $container
->get('entity_type.bundle.info'), $container
->get('entity.repository'));
}
public function defaultConfiguration() {
return [
'filter' => [
'type' => '_none',
'role' => NULL,
],
'include_anonymous' => TRUE,
] + parent::defaultConfiguration();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$configuration = $this
->getConfiguration();
$form['include_anonymous'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Include the anonymous user.'),
'#default_value' => $configuration['include_anonymous'],
];
$form['filter']['type'] = [
'#type' => 'select',
'#title' => $this
->t('Filter by'),
'#options' => [
'_none' => $this
->t('- None -'),
'role' => $this
->t('User role'),
],
'#ajax' => TRUE,
'#limit_validation_errors' => [],
'#default_value' => $configuration['filter']['type'],
];
$form['filter']['settings'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'entity_reference-settings',
],
],
'#process' => [
[
'\\Drupal\\Core\\Field\\Plugin\\Field\\FieldType\\EntityReferenceItem',
'formProcessMergeParent',
],
],
];
if ($configuration['filter']['type'] == 'role') {
$form['filter']['settings']['role'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Restrict to the selected roles'),
'#required' => TRUE,
'#options' => array_diff_key(user_role_names(TRUE), [
RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID,
]),
'#default_value' => $configuration['filter']['role'],
];
}
$form += parent::buildConfigurationForm($form, $form_state);
return $form;
}
protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
$query = parent::buildEntityQuery($match, $match_operator);
$configuration = $this
->getConfiguration();
if (!$configuration['include_anonymous']) {
$query
->condition('uid', 0, '<>');
}
if (isset($match)) {
$query
->condition('name', $match, $match_operator);
}
if (!empty($configuration['filter']['role'])) {
$query
->condition('roles', $configuration['filter']['role'], 'IN');
}
if (!$this->currentUser
->hasPermission('administer users')) {
$query
->condition('status', 1);
}
return $query;
}
public function createNewEntity($entity_type_id, $bundle, $label, $uid) {
$user = parent::createNewEntity($entity_type_id, $bundle, $label, $uid);
if (!$this->currentUser
->hasPermission('administer users')) {
$user
->activate();
}
return $user;
}
public function validateReferenceableNewEntities(array $entities) {
$entities = parent::validateReferenceableNewEntities($entities);
if ($role = $this
->getConfiguration()['filter']['role']) {
$entities = array_filter($entities, function ($user) use ($role) {
return !empty(array_intersect($user
->getRoles(), $role));
});
}
if (!$this->currentUser
->hasPermission('administer users')) {
$entities = array_filter($entities, function ($user) {
return $user
->isActive();
});
}
return $entities;
}
public function entityQueryAlter(SelectInterface $query) {
parent::entityQueryAlter($query);
if (!$this
->getConfiguration()['include_anonymous']) {
return;
}
if ($this->currentUser
->hasPermission('administer users')) {
$conditions =& $query
->conditions();
foreach ($conditions as $key => $condition) {
if ($key !== '#conjunction' && is_string($condition['field']) && $condition['field'] === 'users_field_data.name') {
unset($conditions[$key]);
$or = new Condition('OR');
$or
->condition($condition['field'], $condition['value'], $condition['operator']);
$value_part = new Condition('AND');
$value_part
->condition('anonymous_name', $condition['value'], $condition['operator']);
$value_part
->compile($this->connection, $query);
$or
->condition((new Condition('AND'))
->where(str_replace($query
->escapeField('anonymous_name'), ':anonymous_name', (string) $value_part), $value_part
->arguments() + [
':anonymous_name' => \Drupal::config('user.settings')
->get('anonymous'),
])
->condition('base_table.uid', 0));
$query
->condition($or);
}
}
}
}
}