You are here

function persistent_login_form_alter in Persistent Login 6

Same name and namespace in other branches
  1. 5 persistent_login.module \persistent_login_form_alter()
  2. 7 persistent_login.module \persistent_login_form_alter()

Implementation of hook_form_alter().

File

./persistent_login.module, line 113
Provide a "Remember Me" checkbox in the login form.

Code

function persistent_login_form_alter(&$form, $form_state, $form_id) {
  $alter_form = FALSE;
  if (substr($form_id, 0, 10) == 'user_login') {

    // This is a login form that we want to alter.
    $alter_form = TRUE;
  }
  elseif (substr($form_id, 0, 13) == 'user_register') {

    // This is a user register form, but we only want to alter this if
    // - Visitors can create accounts and no administrator approval is required.
    // - E-mail verification is not required when a visitor creates an account.
    // - The form is not being executed by a user administrator.
    if (!variable_get('user_email_verification', 1) && variable_get('user_register', 1) == 1 && !user_access('administer users')) {
      $alter_form = TRUE;
    }
  }
  if (!$alter_form) {
    return;
  }

  // If the user is reauthenticating, then fill in the name element with the
  // user name provided by persistent_login_init().
  if (isset($_SESSION['persistent_login_default_user'])) {

    // Make sure we still have a 'name' element on this form. Note that someone
    // else could have removed it from its own hook_form_alter() implementation.
    if (isset($form['name'])) {
      $form['name']['#default_value'] = $_SESSION['persistent_login_default_user'];
    }
    unset($_SESSION['persistent_login_default_user']);
  }

  // Don't show Remember Me checkbox if we're reauthenticating to
  // access a protected page unless I change the code to delete the PL
  // session if the user does not check the box.
  //
  // This variable is not unset until login succeeds so if the user
  // mistypes the password Remember Me will stay hidden.  Since this
  // can only get set within a valid PL session, there is no risk of
  // it hiding Remember Me for a non-logged-in user.
  //
  if (!empty($_SESSION['persistent_login_reauth'])) {
    return;
  }

  // Let's add the "Remember me" checkbox to the login/user register form.
  if (isset($form['account']) && is_array($form['account'])) {
    $form['account']['persistent_login'] = array(
      '#type' => 'checkbox',
      '#title' => t('Remember me'),
    );
  }
  else {
    $form['persistent_login'] = array(
      '#type' => 'checkbox',
      '#title' => t('Remember me'),
    );
  }

  // Add an after_build callback that we'll use to adjust the weight
  // and tabindex attributes of the "Remember me" checkbox.
  if (!isset($form['#after_build'])) {
    $form['#after_build'] = array();
  }
  $form['#after_build'][] = 'persistent_login_form_after_build_proxy';
}