UserName.php in Drupal 8
File
core/modules/user/src/Plugin/views/argument_validator/UserName.php
View source
<?php
namespace Drupal\user\Plugin\views\argument_validator;
use Drupal\Core\Form\FormStateInterface;
class UserName extends User {
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$entity_type = $this->entityTypeManager
->getDefinition('user');
$form['multiple']['#options'] = [
0 => $this
->t('Single name', [
'%type' => $entity_type
->getLabel(),
]),
1 => $this
->t('One or more names separated by , or +', [
'%type' => $entity_type
->getLabel(),
]),
];
}
public function validateArgument($argument) {
if ($this->multipleCapable && $this->options['multiple']) {
$names = array_filter(preg_split('/[,+ ]/', $argument));
}
elseif ($argument) {
$names = [
$argument,
];
}
else {
return FALSE;
}
$accounts = $this->userStorage
->loadByProperties([
'name' => $names,
]);
if (empty($accounts)) {
return FALSE;
}
foreach ($accounts as $account) {
if (!in_array($account
->getAccountName(), $names) || !$this
->validateEntity($account)) {
return FALSE;
}
}
return TRUE;
}
public function processSummaryArguments(&$args) {
$uids_arg_keys = array_flip($args);
foreach ($this->userStorage
->loadMultiple($args) as $uid => $account) {
$args[$uids_arg_keys[$uid]] = $account
->label();
}
}
}
Classes
Name |
Description |
UserName |
Validates whether a user name is valid. |