You are here

function _auto_username_patternprocessor in Automatic User Names 7

Same name and namespace in other branches
  1. 5 auto_username.module \_auto_username_patternprocessor()
  2. 6 auto_username.module \_auto_username_patternprocessor()

Process an account and return its new username according to the current pattern.

Parameters

$account: The user object to process.

Return value

The new name for the user object.

1 call to _auto_username_patternprocessor()
auto_username_generate_username in ./auto_username.module
Work out what the new username could be, calling api hooks where applicable, and adding a number suffix if necccessary.

File

./auto_username.module, line 83
Allows a user's username to be assigned based on tokens.

Code

function _auto_username_patternprocessor($account) {
  $output = '';
  $pattern = variable_get('aun_pattern', '');
  if (trim($pattern)) {

    // Replace any tokens in the pattern. Uses callback option to clean replacements. No sanitization.
    $output = token_replace($pattern, array(
      'user' => $account,
    ), array(
      'sanitize' => FALSE,
      'clear' => TRUE,
      'callback' => 'auto_username_clean_token_values',
    ));

    // 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\\]]*\\]/', '', $pattern);
    if ($output === $pattern_tokens_removed) {
      return '';
    }
    if (variable_get('aun_php', 0)) {
      $output = _auto_username_eval($output, $account);
    }
  }
  return trim($output);
}