public static function AutoUsernameSettingsForm::autoUsernameGenerateUsername in Automatic User Names 8
Generating Username value.
Work out what the new username could be, calling api hooks where applicable, and adding a number suffix if necccessary.
2 calls to AutoUsernameSettingsForm::autoUsernameGenerateUsername()
- auto_username_user_insert in ./
auto_username.module - Implements hook_user_insert().
- auto_username_user_update in ./
auto_username.module - Implements hook_user_update().
File
- src/
Form/ AutoUsernameSettingsForm.php, line 445
Class
- AutoUsernameSettingsForm
- Class AutoUsernameSettingsForm.
Namespace
Drupal\auto_username\FormCode
public static function autoUsernameGenerateUsername(&$account) {
// Other modules may implement hook_auto_username_name($edit, $account) to
// generate a username (return a string to be used as the username, NULL to
// have auto_username generate it).
$names = \Drupal::moduleHandler()
->invokeAll('auto_username_name', [
$account,
]);
// Remove any empty entries.
$names = array_filter($names);
if (empty($names)) {
// Default implementation of name generation.
$new_name = AutoUsernameSettingsForm::autoUsernamePatternprocessor($account);
}
else {
// One would expect a single implementation of the hook, but if there
// are multiples out there use the last one.
$new_name = array_pop($names);
}
// If no new name was found, then either the hook hasn't been implemented,
// or the aun_pattern hasn't been set yet. Therefore leave the username as
// it is.
if (empty($new_name)) {
return $account
->getUsername();
}
// Lets check if our name is used somewhere else, and append _1 if it is
// eg:(chris_123). We do this regardless of whether hook has run, as we
// can't assume the hook implementation will do this santity check.
$i = 0;
do {
$new_name = empty($i) ? $new_name : $new_name . '_' . $i;
$found = Database::getConnection()
->select('users_field_data', 'u')
->fields('u')
->condition('uid', $account
->id(), '!=')
->condition('name', $new_name)
->execute()
->fetchAll();
$i++;
} while (!empty($found));
return $new_name;
}