You are here

public static function AutoUsernameSettingsForm::autoUsernamePatternprocessor in Automatic User Names 8

Process an account and return its new username according to currentpattern.

Parameters

object $account: The user object to process.

Return value

string The new name for the user object.

1 call to AutoUsernameSettingsForm::autoUsernamePatternprocessor()
AutoUsernameSettingsForm::autoUsernameGenerateUsername in src/Form/AutoUsernameSettingsForm.php
Generating Username value.

File

src/Form/AutoUsernameSettingsForm.php, line 390

Class

AutoUsernameSettingsForm
Class AutoUsernameSettingsForm.

Namespace

Drupal\auto_username\Form

Code

public static function autoUsernamePatternprocessor($account) {
  $output = '';
  $pattern = \Drupal::state()
    ->get('aun_pattern', '');
  if (trim($pattern)) {
    $pattern_array = explode('\\n', trim($pattern));
    $token_service = \Drupal::token();

    // Replace any tokens in the pattern. Uses callback option to clean
    // replacements. No sanitization.
    $output = $token_service
      ->replace($pattern, [
      'user' => $account,
    ], [
      'clear' => TRUE,
      'callback' => [
        self::class,
        'autoUsernameCleanTokenValues',
      ],
    ]);

    // Check if the token replacement has not actually replaced any values. If
    // that is the case, then stop because we should not generate a name.
    // @see token_scan()
    $pattern_tokens_removed = preg_replace('/\\[[^\\s\\]:]*:[^\\s\\]]*\\]/', '', implode('\\n', $pattern_array));
    if ($output === $pattern_tokens_removed) {
      return '';
    }
    if (\Drupal::state()
      ->get('aun_php', 0)) {
      $output = self::autoUsernameEval($output, $account);
    }
  }
  return trim($output);
}