You are here

function email_registration_form_user_form_alter in Email Registration 8

Implements hook_form_BASE_FORM_ID_alter().

File

./email_registration.module, line 124
Allows users to register with an email address as their username.

Code

function email_registration_form_user_form_alter(&$form, FormStateInterface $form_state) {
  $account = \Drupal::currentUser();

  // Allow users to edit their own username or admins to do it if appropriate.
  if (!$account
    ->hasPermission('change own username') && !$account
    ->hasPermission('administer users')) {
    $form['account']['name']['#type'] = 'value';
  }

  // Provide a random user name only if we are creating a new user, i.e. this
  // is a registration.

  /** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
  $form_object = $form_state
    ->getFormObject();
  if ($form_object
    ->getEntity()
    ->isNew()) {
    $form['account']['name']['#value'] = 'email_registration_' . user_password();
  }
  $form['account']['mail']['#title'] = t('Email');
}