You are here

public function UserName::validateArgument in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/user/src/Plugin/views/argument_validator/UserName.php \Drupal\user\Plugin\views\argument_validator\UserName::validateArgument()
  2. 10 core/modules/user/src/Plugin/views/argument_validator/UserName.php \Drupal\user\Plugin\views\argument_validator\UserName::validateArgument()

Performs validation for a given argument.

Overrides Entity::validateArgument

File

core/modules/user/src/Plugin/views/argument_validator/UserName.php, line 35

Class

UserName
Validates whether a user name is valid.

Namespace

Drupal\user\Plugin\views\argument_validator

Code

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;
}