You are here

function user_registrationpassword_form_user_register_form_alter in User registration password 8

Same name and namespace in other branches
  1. 7 user_registrationpassword.module \user_registrationpassword_form_user_register_form_alter()

Implements hook_form_FORM_ID_alter().

See also

user_register_form()

user_registrationpassword_form_user_register_submit()

File

./user_registrationpassword.module, line 234
Enables password creation on registration form.

Code

function user_registrationpassword_form_user_register_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  /** @var \Drupal\user\UserInterface $account */
  $account = $form_state
    ->getFormObject()
    ->getEntity();
  $user_config = \Drupal::config('user.settings');
  $config = \Drupal::configFactory()
    ->get('user_registrationpassword.settings');

  // Add the password field from the user_account_form when visitors can
  // register, but require admin approval.
  if ($user_config
    ->get('register') == UserInterface::REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL && $config
    ->get('registration') == UserRegistrationPassword::VERIFICATION_PASS && $account
    ->isNew()) {
    $form['account']['pass'] = [
      '#type' => 'password_confirm',
      '#size' => 25,
      '#description' => t('Provide a password for the new account in both fields.'),
      '#required' => TRUE,
    ];
  }

  // Prevent this from being run if approval with password on registration
  // form is set and the user is an anonymous user registering to the site.
  // When admin users create a user, this does not need to be executed.
  // And when this also does not need to be executed 'user_register' is not set
  // as 'Visitors can create accounts and no administrator approval is
  // required.' User registers, receives user_registrationpass email, would
  // not make sense. Cause that will unblock the user Without
  // the admin 'approving'.
  if ($user_config
    ->get('register') == UserInterface::REGISTER_VISITORS && $config
    ->get('registration') == UserRegistrationPassword::VERIFICATION_PASS && $account
    ->isNew()) {

    // Set the user account to blocked.
    $form['account']['status']['#default_value'] = 0;

    // Suppress any notification.
    $form['account']['notify']['#default_value'] = 0;

    // Register our validate and submit handlers.
    $form['actions']['submit']['#submit'][] = 'user_registrationpassword_form_user_register_submit';
  }
}