5 * Contains \Drupal\linkit\Plugin\Linkit\Matcher\UserMatcher.
8 namespace Drupal\linkit\Plugin\Linkit\Matcher;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\user\RoleInterface;
15 * target_entity = "user",
16 * label = @Translation("User"),
20 class UserMatcher extends EntityMatcher {
25 public function getSummary() {
26 $summery = parent::getSummary();
28 $roles = !empty($this->configuration['roles']) ? $this->configuration['roles'] : ['None'];
29 $summery[] = $this->t('Role filter: @role_filter', [
30 '@role_filter' => implode(', ', $roles),
33 $summery[] = $this->t('Include blocked users: @include_blocked', [
34 '@include_blocked' => $this->configuration['include_blocked'] ? $this->t('Yes') : $this->t('No'),
43 public function defaultConfiguration() {
44 return parent::defaultConfiguration() + [
46 'include_blocked' => FALSE,
54 public function calculateDependencies() {
55 return parent::calculateDependencies() + [
63 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
64 $form = parent::buildConfigurationForm($form, $form_state);
66 $form['roles'] = array(
67 '#type' => 'checkboxes',
68 '#title' => $this->t('Restrict to the selected roles'),
69 '#options' => array_diff_key(user_role_names(TRUE), array(RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID)),
70 '#default_value' => $this->configuration['roles'],
71 '#description' => $this->t('If none of the checkboxes is checked, allow all roles.'),
72 '#element_validate' => [[get_class($this), 'elementValidateFilter']],
75 $form['include_blocked'] = [
76 '#title' => t('Include blocked user'),
77 '#type' => 'checkbox',
78 '#default_value' => $this->configuration['include_blocked'],
79 '#description' => t('In order to see blocked users, the requesting user must also have permissions to do so.'),
88 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
89 parent::submitConfigurationForm($form, $form_state);
91 $this->configuration['roles'] = $form_state->getValue('roles');
92 $this->configuration['include_blocked'] = $form_state->getValue('include_blocked');
98 protected function buildEntityQuery($match) {
99 $query = parent::buildEntityQuery($match);
101 $match = $this->database->escapeLike($match);
102 // The user entity don't specify a label key so we have to do it instead.
103 $query->condition('name', '%' . $match . '%', 'LIKE');
106 if (!empty($this->configuration['roles'])) {
107 $query->condition('roles', $this->configuration['roles'], 'IN');
110 if ($this->configuration['include_blocked'] !== TRUE || !$this->currentUser->hasPermission('administer users')) {
111 $query->condition('status', 1);