You are here

function auto_username_user_update in Automatic User Names 8

Implements hook_user_update().

File

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

Code

function auto_username_user_update($account) {
  if (\Drupal::state()
    ->get('aun_update_on_edit', TRUE)) {
    $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;
  }
}