3 namespace Drupal\user\Plugin\views\filter;
5 use Drupal\Core\Entity\Element\EntityAutocomplete;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\user\Entity\User;
8 use Drupal\views\Plugin\views\filter\InOperator;
11 * Filter handler for usernames.
13 * @ingroup views_filter_handlers
15 * @ViewsFilter("user_name")
17 class Name extends InOperator {
19 protected $alwaysMultiple = TRUE;
21 protected function valueForm(&$form, FormStateInterface $form_state) {
22 $users = $this->value ? User::loadMultiple($this->value) : [];
23 $default_value = EntityAutocomplete::getEntityLabels($users);
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',
30 '#default_value' => $default_value,
31 '#process_default_value' => $this->isExposed(),
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);
41 protected function valueValidate($form, FormStateInterface $form_state) {
43 if ($values = $form_state->getValue(['options', 'value'])) {
44 foreach ($values as $value) {
45 $uids[] = $value['target_id'];
49 $form_state->setValue(['options', 'value'], $uids);
52 public function acceptExposedInput($input) {
53 $rc = parent::acceptExposedInput($input);
56 // If we have previously validated input, override.
57 if (isset($this->validated_exposed_input)) {
58 $this->value = $this->validated_exposed_input;
65 public function validateExposed(&$form, FormStateInterface $form_state) {
66 if (empty($this->options['exposed'])) {
70 if (empty($this->options['expose']['identifier'])) {
74 $identifier = $this->options['expose']['identifier'];
75 $input = $form_state->getValue($identifier);
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'];
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'];
91 $this->validated_exposed_input = $uids;
95 protected function valueSubmit($form, FormStateInterface $form_state) {
96 // prevent array filter from removing our anonymous user.
102 public function getValueOptions() {
103 return $this->valueOptions;
106 public function adminSummary() {
107 // set up $this->valueOptions for the parent summary
108 $this->valueOptions = [];
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();
118 // Intentionally NOT translated.
119 $this->valueOptions[$account->id()] = 'Anonymous';
124 return parent::adminSummary();