You are here

function simple_pass_reset_form_user_form_alter in Simple Password Reset 8

Implements hook_form_FORM_ID_alter().

File

./simple_pass_reset.module, line 14
Form alters and submits for Simple password reset module.

Code

function simple_pass_reset_form_user_form_alter(&$form, FormStateInterface $form_state) {

  // Retrieve an array which contains the path pieces.
  $current_path = \Drupal::service('path.current')
    ->getPath();
  $path_args = explode('/', $current_path);

  // Don't alter the normal profile edit form, but only the password reset form.
  if (isset($path_args[1], $path_args[2]) && $path_args[1] == 'user' && $path_args[2] == 'reset' && \Drupal::currentUser()
    ->isAnonymous()) {
    $account = \Drupal::entityTypeManager()
      ->getStorage('user')
      ->load($path_args[3]);
    $form['actions']['submit']['#submit'][] = 'simple_pass_reset_pass_reset_submit';
    $form['actions']['submit']['#value'] = t('Save and log in as @username', [
      '@username' => $account
        ->getDisplayName(),
    ]);

    // Some third-party modules (like Bakery) might hide account elements.
    if (!isset($form['account']['#access']) || $form['account']['#access']) {

      // Require a new password.
      $form['account']['pass']['#required'] = TRUE;

      // Hide "To change the current user password...".
      unset($form['account']['pass']['#description']);

      // The user is most interested in getting a working password.
      // don't show their picture, timezone, etc.
      foreach (Element::children($form) as $key) {
        if (isset($form[$key]['#type']) && in_array($form[$key]['#type'], [
          'hidden',
          'actions',
          'captcha',
        ])) {

          // Do not alter these elements.
        }
        else {

          // Hide other elements.
          $form[$key]['#access'] = FALSE;
        }
      }

      // Except don't hide these.
      $form['account']['#access'] = TRUE;
      $form['actions']['#access'] = TRUE;
      if (isset($form['_field_layout'])) {
        $form['_field_layout']['#access'] = TRUE;
      }

      // But seriously do hide these.
      $form['account']['mail']['#access'] = FALSE;
    }

    // This is to avoid a PHP Notice in user_profile_form_submit().
    // https://www.drupal.org/node/2111293#comment-9262499
    if (empty($_SESSION)) {
      $_SESSION = [
        'simple_pass_reset' => TRUE,
      ];
    }
  }
}