1<?php
2
3namespace Drupal\user\Plugin\views\filter;
4
5use Drupal\Core\Entity\Element\EntityAutocomplete;
6use Drupal\Core\Form\FormStateInterface;
7use Drupal\user\Entity\User;
8use Drupal\views\Plugin\views\filter\InOperator;
9
10/**
11 * Filter handler for usernames.
12 *
13 * @ingroup views_filter_handlers
14 *
15 * @ViewsFilter("user_name")
16 */
17class Name extends InOperator {
18
19  protected $alwaysMultiple = TRUE;
20
21  protected function valueForm(&$form, FormStateInterface $form_state) {
22    $users = $this->value ? User::loadMultiple($this->value) : [];
23    $default_value = EntityAutocomplete::getEntityLabels($users);
24    $form['value'] = [
25      '#type' => 'entity_autocomplete',
26      '#title' => $this->t('Usernames'),
27      '#description' => $this->t('Enter a comma separated list of user names.'),
28      '#target_type' => 'user',
29      '#tags' => TRUE,
30      '#default_value' => $default_value,
31      '#process_default_value' => $this->isExposed(),
32    ];
33
34    $user_input = $form_state->getUserInput();
35    if ($form_state->get('exposed') && !isset($user_input[$this->options['expose']['identifier']])) {
36      $user_input[$this->options['expose']['identifier']] = $default_value;
37      $form_state->setUserInput($user_input);
38    }
39  }
40
41  protected function valueValidate($form, FormStateInterface $form_state) {
42    $uids = [];
43    if ($values = $form_state->getValue(['options', 'value'])) {
44      foreach ($values as $value) {
45        $uids[] = $value['target_id'];
46      }
47      sort($uids);
48    }
49    $form_state->setValue(['options', 'value'], $uids);
50  }
51
52  public function acceptExposedInput($input) {
53    $rc = parent::acceptExposedInput($input);
54
55    if ($rc) {
56      // If we have previously validated input, override.
57      if (isset($this->validated_exposed_input)) {
58        $this->value = $this->validated_exposed_input;
59      }
60    }
61
62    return $rc;
63  }
64
65  public function validateExposed(&$form, FormStateInterface $form_state) {
66    if (empty($this->options['exposed'])) {
67      return;
68    }
69
70    if (empty($this->options['expose']['identifier'])) {
71      return;
72    }
73
74    $identifier = $this->options['expose']['identifier'];
75    $input = $form_state->getValue($identifier);
76
77    if ($this->options['is_grouped'] && isset($this->options['group_info']['group_items'][$input])) {
78      $this->operator = $this->options['group_info']['group_items'][$input]['operator'];
79      $input = $this->options['group_info']['group_items'][$input]['value'];
80    }
81
82    $uids = [];
83    $values = $form_state->getValue($identifier);
84    if ($values && (!$this->options['is_grouped'] || ($this->options['is_grouped'] && ($input != 'All')))) {
85      foreach ($values as $value) {
86        $uids[] = $value['target_id'];
87      }
88    }
89
90    if ($uids) {
91      $this->validated_exposed_input = $uids;
92    }
93  }
94
95  protected function valueSubmit($form, FormStateInterface $form_state) {
96    // prevent array filter from removing our anonymous user.
97  }
98
99  /**
100   * {@inheritdoc}
101   */
102  public function getValueOptions() {
103    return $this->valueOptions;
104  }
105
106  public function adminSummary() {
107    // set up $this->valueOptions for the parent summary
108    $this->valueOptions = [];
109
110    if ($this->value) {
111      $result = \Drupal::entityTypeManager()->getStorage('user')
112        ->loadByProperties(['uid' => $this->value]);
113      foreach ($result as $account) {
114        if ($account->id()) {
115          $this->valueOptions[$account->id()] = $account->label();
116        }
117        else {
118          // Intentionally NOT translated.
119          $this->valueOptions[$account->id()] = 'Anonymous';
120        }
121      }
122    }
123
124    return parent::adminSummary();
125  }
126
127}
128