class UserName
Same name and namespace in other branches
- 11.x core/modules/user/src/Plugin/views/argument_validator/UserName.php \Drupal\user\Plugin\views\argument_validator\UserName
- 10 core/modules/user/src/Plugin/views/argument_validator/UserName.php \Drupal\user\Plugin\views\argument_validator\UserName
- 8.9.x core/modules/user/src/Plugin/views/argument_validator/UserName.php \Drupal\user\Plugin\views\argument_validator\UserName
Validates whether a user name is valid.
Plugin annotation
@ViewsArgumentValidator(
id = "user_name",
title = @Translation("User name"),
entity_type = "user"
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements \Drupal\Component\Plugin\PluginInspectionInterface, \Drupal\Component\Plugin\DerivativeInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait extends \Drupal\Component\Plugin\PluginBase
- class \Drupal\views\Plugin\views\PluginBase implements \Drupal\Core\Plugin\ContainerFactoryPluginInterface, \Drupal\views\Plugin\views\ViewsPluginInterface, \Drupal\Component\Plugin\DependentPluginInterface, \Drupal\Core\Security\TrustedCallbackInterface extends \Drupal\Core\Plugin\PluginBase
- class \Drupal\views\Plugin\views\argument_validator\ArgumentValidatorPluginBase extends \Drupal\views\Plugin\views\PluginBase
- class \Drupal\views\Plugin\views\argument_validator\Entity extends \Drupal\views\Plugin\views\argument_validator\ArgumentValidatorPluginBase
- class \Drupal\user\Plugin\views\argument_validator\User extends \Drupal\views\Plugin\views\argument_validator\Entity
- class \Drupal\user\Plugin\views\argument_validator\UserName extends \Drupal\user\Plugin\views\argument_validator\User
- class \Drupal\user\Plugin\views\argument_validator\User extends \Drupal\views\Plugin\views\argument_validator\Entity
- class \Drupal\views\Plugin\views\argument_validator\Entity extends \Drupal\views\Plugin\views\argument_validator\ArgumentValidatorPluginBase
- class \Drupal\views\Plugin\views\argument_validator\ArgumentValidatorPluginBase extends \Drupal\views\Plugin\views\PluginBase
- class \Drupal\views\Plugin\views\PluginBase implements \Drupal\Core\Plugin\ContainerFactoryPluginInterface, \Drupal\views\Plugin\views\ViewsPluginInterface, \Drupal\Component\Plugin\DependentPluginInterface, \Drupal\Core\Security\TrustedCallbackInterface extends \Drupal\Core\Plugin\PluginBase
- class \Drupal\Core\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait extends \Drupal\Component\Plugin\PluginBase
Expanded class hierarchy of UserName
25 string references to 'UserName'
- AccessDeniedTest::testAccessDenied in core/
modules/ system/ tests/ src/ Functional/ System/ AccessDeniedTest.php - AccountForm::form in core/
modules/ user/ src/ AccountForm.php - AccountForm::form in core/
modules/ user/ src/ AccountForm.php - AssignOwnerNode::buildConfigurationForm in core/
modules/ node/ src/ Plugin/ Action/ AssignOwnerNode.php - DatabaseTestForm::buildForm in core/
modules/ system/ tests/ modules/ database_test/ src/ Form/ DatabaseTestForm.php
File
-
core/
modules/ user/ src/ Plugin/ views/ argument_validator/ UserName.php, line 16
Namespace
Drupal\user\Plugin\views\argument_validatorView source
class UserName extends User {
/**
* {@inheritdoc}
*/
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(),
]),
];
}
/**
* {@inheritdoc}
*/
public function validateArgument($argument) {
if ($this->multipleCapable && $this->options['multiple']) {
// At this point only interested in individual IDs no matter what type,
// just splitting by the allowed delimiters.
$names = array_filter(preg_split('/[,+ ]/', $argument));
}
elseif ($argument) {
$names = [
$argument,
];
}
else {
return FALSE;
}
$accounts = $this->userStorage
->loadByProperties([
'name' => $names,
]);
// If there are no accounts, return FALSE now. As we will not enter the
// loop below otherwise.
if (empty($accounts)) {
return FALSE;
}
// Validate each account. If any fails break out and return false.
foreach ($accounts as $account) {
if (!in_array($account->getAccountName(), $names) || !$this->validateEntity($account)) {
return FALSE;
}
}
return TRUE;
}
/**
* {@inheritdoc}
*/
public function processSummaryArguments(&$args) {
// If the validation says the input is a username, we should reverse the
// argument so it works for example for generation summary urls.
$uids_arg_keys = array_flip($args);
foreach ($this->userStorage
->loadMultiple($args) as $uid => $account) {
$args[$uids_arg_keys[$uid]] = $account->label();
}
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.