You are here

function genpass_form_alter in Generate Password 8

Same name and namespace in other branches
  1. 5 genpass.module \genpass_form_alter()
  2. 6 genpass.module \genpass_form_alter()
  3. 7 genpass.module \genpass_form_alter()

Implements hook_form_alter().

File

./genpass.module, line 120
Contains genpass.module.

Code

function genpass_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  switch ($form_id) {

    // User admin settings form at admin/config/people/accounts.
    case 'user_admin_settings':
      $form['genpass_config'] = [
        '#type' => 'details',
        '#title' => t('Generate Password'),
        '#open' => TRUE,
        '#weight' => 5,
      ];

      // Place genpass configuration details above the system emails accordion.
      $form['email']['#weight'] = 10;
      $settings = \Drupal::config('genpass.settings')
        ->get();
      $form['genpass_config']['genpass_mode'] = [
        '#type' => 'radios',
        '#title' => t('Password handling'),
        '#default_value' => $settings['genpass_mode'],
        '#options' => [
          GenpassInterface::PASSWORD_REQUIRED => t('Users <strong>must</strong> enter a password on registration. This is disabled if e-mail verification is enabled above.'),
          GenpassInterface::PASSWORD_OPTIONAL => t('Users <strong>may</strong> enter a password on registration. If left empty, a random password will be generated. This always applies when an administer is creating the account.'),
          GenpassInterface::PASSWORD_RESTRICTED => t('Users <strong>cannot</strong> enter a password on registration; a random password will be generated. This always applies for the regular user registration form if e-mail verification is enabled above.'),
        ],
        '#description' => t('Choose a password handling mode for new users.'),
      ];
      $form['genpass_config']['genpass_length'] = [
        '#type' => 'textfield',
        '#title' => t('Generated password length'),
        '#default_value' => $settings['genpass_length'],
        '#size' => 2,
        '#maxlength' => 2,
        '#description' => t('Set the length of generated passwords here. Allowed range: 5 to 32.'),
      ];

      // Provide a selection mechanism to choose the preferred algorithm for
      // generating passwords. Any module which implements hook_password() is
      // displayed here.
      $form['genpass_config']['genpass_algorithm'] = [
        '#type' => 'radios',
        '#title' => t('Password generation algorithm'),
        '#default_value' => genpass_algorithm_module(),
        '#options' => genpass_add_samples(genpass_algorithm_modules(), $settings['genpass_length']),
        '#description' => t('If third party modules define a password generation algorithm, you can select which one this module will use.'),
      ];
      $form['genpass_config']['genpass_display'] = [
        '#type' => 'radios',
        '#title' => t('Generated password display'),
        '#default_value' => $settings['genpass_display'],
        '#options' => [
          GenpassInterface::PASSWORD_DISPLAY_NONE => t('Do not display.'),
          GenpassInterface::PASSWORD_DISPLAY_ADMIN => t('Display when site administrators create new user accounts.'),
          GenpassInterface::PASSWORD_DISPLAY_USER => t('Display when users create their own accounts.'),
          GenpassInterface::PASSWORD_DISPLAY_BOTH => t('Display to both site administrators and users.'),
        ],
        '#description' => t('Whether or not the generated password should display after a user account is created.'),
      ];
      $form['#validate'][] = 'genpass_user_admin_settings_validate';
      $form['#submit'][] = 'genpass_user_admin_settings_submit';
      break;

    // User registration form at admin/people/create.
    case 'user_register_form':
      $mode = \Drupal::config('genpass.settings')
        ->get('genpass_mode');

      // Add validation function, where password may get set.
      $form['#validate'][] = 'genpass_register_validate';

      // Administrator is creating the user.
      if (\Drupal::routeMatch()
        ->getRouteName() == 'user.admin_create') {

        // Switch to optional mode.
        $mode = GenpassInterface::PASSWORD_OPTIONAL;

        // Help avoid obvious consequence of password being optional.
        $notify_item =& _genpass_get_form_item($form, 'notify');
        $notify_item['#description'] = t('This is recommended when auto-generating the password; otherwise, neither you nor the new user will know the password.');
      }

      // Pass mode to validation function.
      $form['genpass_mode'] = [
        '#type' => 'value',
        '#value' => $mode,
      ];
      $pass_item =& _genpass_get_form_item($form, 'pass');
      switch ($mode) {

        // If password is optional, don't require it, and give the user an
        // indication of what will happen if left blank.
        case GenpassInterface::PASSWORD_OPTIONAL:
          $pass_item['#required'] = FALSE;
          $pass_item['#description'] = (empty($pass_item['#description']) ? '' : $pass_item['#description'] . ' ') . t('If left blank, a password will be generated for you.');
          break;

        // If password is restricted, remove access.
        case GenpassInterface::PASSWORD_RESTRICTED:
          $pass_item['#access'] = FALSE;
          $pass_item['#required'] = FALSE;
          break;
      }
      break;
  }
}