X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fuser%2Fsrc%2FPlugin%2Fviews%2Ffilter%2FName.php;fp=web%2Fcore%2Fmodules%2Fuser%2Fsrc%2FPlugin%2Fviews%2Ffilter%2FName.php;h=f4260c5c0bda4aad056c19160fbb5556ed6a83ba;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/user/src/Plugin/views/filter/Name.php b/web/core/modules/user/src/Plugin/views/filter/Name.php new file mode 100644 index 000000000..f4260c5c0 --- /dev/null +++ b/web/core/modules/user/src/Plugin/views/filter/Name.php @@ -0,0 +1,126 @@ +value ? User::loadMultiple($this->value) : []; + $default_value = EntityAutocomplete::getEntityLabels($users); + $form['value'] = [ + '#type' => 'entity_autocomplete', + '#title' => $this->t('Usernames'), + '#description' => $this->t('Enter a comma separated list of user names.'), + '#target_type' => 'user', + '#tags' => TRUE, + '#default_value' => $default_value, + '#process_default_value' => $this->isExposed(), + ]; + + $user_input = $form_state->getUserInput(); + if ($form_state->get('exposed') && !isset($user_input[$this->options['expose']['identifier']])) { + $user_input[$this->options['expose']['identifier']] = $default_value; + $form_state->setUserInput($user_input); + } + } + + protected function valueValidate($form, FormStateInterface $form_state) { + $uids = []; + if ($values = $form_state->getValue(['options', 'value'])) { + foreach ($values as $value) { + $uids[] = $value['target_id']; + } + sort($uids); + } + $form_state->setValue(['options', 'value'], $uids); + } + + public function acceptExposedInput($input) { + $rc = parent::acceptExposedInput($input); + + if ($rc) { + // If we have previously validated input, override. + if (isset($this->validated_exposed_input)) { + $this->value = $this->validated_exposed_input; + } + } + + return $rc; + } + + public function validateExposed(&$form, FormStateInterface $form_state) { + if (empty($this->options['exposed'])) { + return; + } + + if (empty($this->options['expose']['identifier'])) { + return; + } + + $identifier = $this->options['expose']['identifier']; + $input = $form_state->getValue($identifier); + + if ($this->options['is_grouped'] && isset($this->options['group_info']['group_items'][$input])) { + $this->operator = $this->options['group_info']['group_items'][$input]['operator']; + $input = $this->options['group_info']['group_items'][$input]['value']; + } + + $uids = []; + $values = $form_state->getValue($identifier); + if ($values && (!$this->options['is_grouped'] || ($this->options['is_grouped'] && ($input != 'All')))) { + foreach ($values as $value) { + $uids[] = $value['target_id']; + } + } + + if ($uids) { + $this->validated_exposed_input = $uids; + } + } + + protected function valueSubmit($form, FormStateInterface $form_state) { + // prevent array filter from removing our anonymous user. + } + + /** + * {@inheritdoc} + */ + public function getValueOptions() { + return $this->valueOptions; + } + + public function adminSummary() { + // set up $this->valueOptions for the parent summary + $this->valueOptions = []; + + if ($this->value) { + $result = \Drupal::entityTypeManager()->getStorage('user') + ->loadByProperties(['uid' => $this->value]); + foreach ($result as $account) { + if ($account->id()) { + $this->valueOptions[$account->id()] = $account->label(); + } + else { + $this->valueOptions[$account->id()] = 'Anonymous'; // Intentionally NOT translated. + } + } + } + + return parent::adminSummary(); + } + +}