You are here

simple_pass_reset.module in Simple Password Reset 8

Same filename and directory in other branches
  1. 7 simple_pass_reset.module

Form alters and submits for Simple password reset module.

File

simple_pass_reset.module
View source
<?php

/**
 * @file
 * Form alters and submits for Simple password reset module.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;

/**
 * Implements hook_form_FORM_ID_alter().
 */
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,
      ];
    }
  }
}

/**
 * User form submit callback.
 */
function simple_pass_reset_pass_reset_submit(&$form, FormStateInterface $form_state) {

  // Sanity check.
  if (\Drupal::currentUser()
    ->isAnonymous()) {
    $values = $form_state
      ->getValues();

    // Remove roles that were disabled in the form. Normally the User module
    // will array_filter() these out for us.  But remember_me and possibly other
    // modules have bugs that might prevent it from doing so.
    if (!empty($values['roles'])) {
      $form_state
        ->set('roles', array_filter($values['roles']));
    }

    // Load the user account afresh and finalize the login.
    // @see user_login_submit()
    $account = \Drupal::entityTypeManager()
      ->getStorage('user')
      ->load($values['uid']);
    user_login_finalize($account);
    \Drupal::logger('user')
      ->notice(t('User @name used one-time login link.', [
      '@name' => $values['name'],
    ]));
    if (empty($form_state
      ->getRedirect())) {
      $form_state
        ->setRedirect('user.page');
    }
  }
}

/**
 * Implements hook_module_implements_alter().
 *
 * Asks Drupal to run our form_alter hooks after other modules.
 */
function simple_pass_reset_module_implements_alter(&$implementations, $hook) {
  if ($hook == 'form_alter' && isset($implementations['simple_pass_reset'])) {

    // Make our form alters come last
    // (so we act after other modules have already altered).
    $group = $implementations['simple_pass_reset'];
    unset($implementations['simple_pass_reset']);
    $implementations['simple_pass_reset'] = $group;
  }
}