You are here

function auto_username_user_insert in Automatic User Names 8

Implements hook_user_insert().

File

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

Code

function auto_username_user_insert($account) {
  $new_name = AutoUsernameSettingsForm::autoUsernameGenerateUsername($account);

  // No point updating anything if the generated name was just the same.
  if ($account->name == $new_name) {
    return;
  }

  // Replace with generated username.
  Database::getConnection()
    ->update('users_field_data')
    ->fields(array(
    'name' => $new_name,
  ))
    ->condition('uid', $account
    ->id())
    ->execute();
  $account->name = $new_name;
}