auto_username.module in Automatic User Names 8
Same filename and directory in other branches
Allows a user's username to be assigned based on tokens.
File
auto_username.moduleView source
<?php
/**
* @file
* Allows a user's username to be assigned based on tokens.
*/
use Drupal\Core\Database\Database;
use Drupal\auto_username\Form\AutoUsernameSettingsForm;
/**
* Implements hook_form_FORM_ID_alter().
*/
function auto_username_form_user_register_form_alter(&$form, &$form_state, $form_id) {
$form['account']['name']['#type'] = 'hidden';
$form['account']['name']['#value'] = user_password();
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function auto_username_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
$form['account']['name']['#type'] = 'hidden';
}
/**
* Implements hook_user_insert().
*/
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;
}
/**
* Implements hook_user_update().
*/
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;
}
}
Functions
Name | Description |
---|---|
auto_username_form_user_profile_form_alter | Implements hook_form_FORM_ID_alter(). |
auto_username_form_user_register_form_alter | Implements hook_form_FORM_ID_alter(). |
auto_username_user_insert | Implements hook_user_insert(). |
auto_username_user_update | Implements hook_user_update(). |