You are here

function create_user_permission_form_user_register_form_alter in Create user permission 8

Same name and namespace in other branches
  1. 2.x create_user_permission.module \create_user_permission_form_user_register_form_alter()

Implements hook_form_FORM_ID_alter().

File

./create_user_permission.module, line 15
The create user permission module.

Code

function create_user_permission_form_user_register_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $access = \Drupal::currentUser()
    ->hasPermission('create users');
  if (!$access) {

    // No use in form altering anything.
    return;
  }
  $register_setting = \Drupal::config('user.settings')
    ->get('register');
  if ($register_setting != 'visitors') {

    // Set the admin value to true, so that our user actually gets created with
    // no intervention.
    $form['administer_users']['#value'] = TRUE;
    $form['account']['status']['#default_value'] = 1;
  }

  // Make it possible to send an email to the newly created user.
  $form['account']['notify']['#access'] = \Drupal::currentUser()
    ->hasPermission('create users');
}