You are here

function simple_pass_reset_form_user_profile_form_alter in Simple Password Reset 7

Implements hook_form_FORM_ID_alter().

See also

user_profile_form()

File

./simple_pass_reset.module, line 80

Code

function simple_pass_reset_form_user_profile_form_alter(&$form, &$form_state) {

  // Don't alter the normal profile edit form, but only the password reset form.
  if (arg(0) == 'user' && arg(1) == 'reset' && !user_is_logged_in()) {

    // Our submit handler will log the user in after form submit.
    $form['#submit'][] = 'simple_pass_reset_pass_reset_submit';
    $form['actions']['submit']['#value'] = t('Save and log in as !username', array(
      '!username' => format_username($form['#user']),
    ));

    // Links provided by the Bakery module will not work because the user is not
    // logged in yet.
    if (!empty($form['bakery'])) {
      $form['bakery']['#access'] = FALSE;

      // Normally the Bakery module would make the following change to the
      // user_pass_reset form.
      if (!variable_get('bakery_is_master', FALSE)) {

        // Set a submit handler for the pseudo-reset form.
        $form['#submit'] = array(
          '_bakery_reset_submit',
        );
      }
    }

    // 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;
      if (arg(5) == 'brief') {
        drupal_set_title(t('Choose a new password'));

        // Instead of "Reset password".
        // 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'], array(
            '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;

        // 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 = array(
        'simple_pass_reset' => TRUE,
      );
    }
  }
}