Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Disable user nick name in Drupal 8
<?php
use \Drupal\user\UserInterface;
use \Drupal\Component\Utility\Random;
/**
* Implements hook_ENTITY_TYPE_create().
*/
function custom_user_user_presave(UserInterface $entity) {
$entity->setUsername($entity->getEmail());
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function custom_user_form_user_form_alter(&$form) {
$form['account']['mail']['#required'] = TRUE;
$form['account']['name']['#access'] = FALSE;
$random = new Random();
$form['account']['name']['#default_value'] = $random->name();
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function custom_user_form_user_register_form_alter(&$form) {
custom_user_form_user_form_alter($form);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment