94a462a93c82b20a7046fdfd61ddb4e5cb3baacd
[yaffs-website] / web / modules / contrib / linkit / src / Plugin / Linkit / Matcher / UserMatcher.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\linkit\Plugin\Linkit\Matcher\UserMatcher.
6  */
7
8 namespace Drupal\linkit\Plugin\Linkit\Matcher;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\user\RoleInterface;
11
12 /**
13  * @Matcher(
14  *   id = "entity:user",
15  *   target_entity = "user",
16  *   label = @Translation("User"),
17  *   provider = "user"
18  * )
19  */
20 class UserMatcher extends EntityMatcher {
21
22   /**
23    * {@inheritdoc}
24    */
25   public function getSummary() {
26     $summery = parent::getSummary();
27
28     $roles = !empty($this->configuration['roles']) ? $this->configuration['roles'] : ['None'];
29     $summery[] = $this->t('Role filter: @role_filter', [
30       '@role_filter' => implode(', ', $roles),
31     ]);
32
33     $summery[] = $this->t('Include blocked users: @include_blocked', [
34       '@include_blocked' => $this->configuration['include_blocked'] ? $this->t('Yes') : $this->t('No'),
35     ]);
36
37     return $summery;
38   }
39
40   /**
41    * {@inheritdoc}
42    */
43   public function defaultConfiguration() {
44     return parent::defaultConfiguration() + [
45       'roles' => [],
46       'include_blocked' => FALSE,
47     ];
48   }
49
50
51   /**
52    * {@inheritdoc}
53    */
54   public function calculateDependencies() {
55     return parent::calculateDependencies() + [
56       'module' => ['user'],
57     ];
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
64     $form = parent::buildConfigurationForm($form, $form_state);
65
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']],
73     );
74
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.'),
80     ];
81
82     return $form;
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
89     parent::submitConfigurationForm($form, $form_state);
90
91     $this->configuration['roles'] = $form_state->getValue('roles');
92     $this->configuration['include_blocked'] = $form_state->getValue('include_blocked');
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   protected function buildEntityQuery($match) {
99     $query = parent::buildEntityQuery($match);
100
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');
104
105     // Filter by role.
106     if (!empty($this->configuration['roles'])) {
107       $query->condition('roles', $this->configuration['roles'], 'IN');
108     }
109
110     if ($this->configuration['include_blocked'] !== TRUE || !$this->currentUser->hasPermission('administer users')) {
111       $query->condition('status', 1);
112     }
113
114     return $query;
115   }
116
117 }