You are here

function logintoboggan_form_alter in LoginToboggan 6

Same name and namespace in other branches
  1. 8 logintoboggan.module \logintoboggan_form_alter()
  2. 5 logintoboggan.module \logintoboggan_form_alter()
  3. 7 logintoboggan.module \logintoboggan_form_alter()

Implementation of hook_form_alter()

Related topics

File

./logintoboggan.module, line 122
Logintoboggan Module

Code

function logintoboggan_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'block_admin_configure':
      if ($form['module']['#value'] == 'user' && $form['delta']['#value'] == 0) {
        $form['#submit'][] = 'logintoboggan_user_block_admin_configure_submit';
        $form['block_settings']['title']['#description'] .= '<div id="logintoboggan-block-title-description">' . t('<strong>Note:</strong> Logintoboggan module is installed. If you are using one of the custom login block types below, it is recommended that you set this to <em>&lt;none&gt;</em>.') . '</div>';
        $form['block_settings']['logintoboggan_login_block_type'] = array(
          '#type' => 'radios',
          '#title' => t('Block type'),
          '#default_value' => variable_get('logintoboggan_login_block_type', 0),
          '#options' => array(
            t('Standard'),
            t('Link'),
            t('Collapsible form'),
          ),
          '#description' => t("'Standard' is a standard login block, 'Link' is a login link that returns the user to the original page after logging in, 'Collapsible form' is a javascript collaspible login form."),
        );
        $form['block_settings']['logintoboggan_login_block_message'] = array(
          '#type' => 'textarea',
          '#title' => t('Set a custom message to appear at the top of the login block'),
          '#default_value' => variable_get('logintoboggan_login_block_message', ''),
        );
      }
      break;

    // This will reset the the site 403 variable to the default if the module is
    // disabled and the toboggan redirect on access denied is enabled.
    case 'system_modules':
      $form['#validate'][] = 'logintoboggan_site_403_validate';
      break;
    case 'logintoboggan_main_settings':
      $form['#submit'][] = 'logintoboggan_flip_user_email_verification';
      break;
    case 'user_profile_form':
      $form['#validate'][] = 'logintoboggan_user_edit_validate';
      $account = $form['_account']['#value'];
      $id = logintoboggan_validating_id();
      $pre_auth = !variable_get('user_email_verification', TRUE) && $id != DRUPAL_AUTHENTICATED_RID;
      $in_pre_auth_role = in_array($id, array_keys($account->roles));

      // Messages are only necessary for user admins, and aren't necessary if
      // there's no valid pre-auth role.
      if (user_access('administer users') && isset($form['account']['roles']) && $pre_auth) {

        // User is still in the pre-auth role, so let the admin know.
        if ($in_pre_auth_role) {

          // To reduce UI confusion, remove the disabled checkbox for the
          // authenticated user role.
          unset($form['account']['roles'][DRUPAL_AUTHENTICATED_RID]);
          if (variable_get('user_register', 1) == 2) {
            $form['account']['status']['#description'] = t('If this user was created using the "Immediate Login" feature of LoginToboggan, and they are also awaiting adminstrator approval on their account, you must remove them from the site\'s pre-authorized role in the "Roles" section below, or they will not receive authenticated user permissions!');
          }
          $form['account']['roles']['#description'] = t("The user is assigned LoginToboggan's pre-authorized role, and is not currently receiving authenticated user permissions.");
        }
        else {
          unset($form['account']['roles']['#options'][$id]);
        }
      }
      break;
    case 'user_login':
    case 'user_login_block':

      // Grab the message from settings for display at the top of the login block.
      if ($login_msg = variable_get('logintoboggan_login_block_message', '')) {
        $form['message'] = array(
          '#value' => filter_xss_admin($login_msg),
          '#weight' => -50,
        );
      }
      if (variable_get('logintoboggan_login_with_email', 0)) {

        // Ensure a valid validate array.
        $form['#validate'] = is_array($form['#validate']) ? $form['#validate'] : array();

        // LT's validation function must run first.
        array_unshift($form['#validate'], 'logintoboggan_user_login_validate');

        // Use theme functions to print the username field's textual labels.
        $form['name']['#title'] = theme('lt_username_title', $form_id);
        $form['name']['#description'] = theme('lt_username_description', $form_id);

        // Use theme functions to print the password field's textual labels.
        $form['pass']['#title'] = theme('lt_password_title', $form_id);
        $form['pass']['#description'] = theme('lt_password_description', $form_id);
      }
      if (isset($GLOBALS['logintoboggan_denied']) && $GLOBALS['logintoboggan_denied'] == TRUE) {
        logintoboggan_destination();
      }
      if ($form_id == 'user_login_block') {
        $block_type = variable_get('logintoboggan_login_block_type', 0);
        if ($block_type == 1) {
          $form = array(
            '#value' => l(theme('lt_login_link'), 'user/login', array(
              'query' => drupal_get_destination(),
            )),
          );
        }
        elseif ($block_type == 2) {
          $form = _logintoboggan_toggleboggan($form);
        }
      }
      break;
    case 'user_register':

      // Admin created accounts are only validated by the module.
      if (user_access('administer users')) {
        $form['#validate'][] = 'logintoboggan_user_register_validate';
        break;
      }
      $mail = variable_get('logintoboggan_confirm_email_at_registration', 0);
      $pass = !variable_get('user_email_verification', TRUE);

      // Ensure a valid submit array.
      $form['#submit'] = is_array($form['#submit']) ? $form['#submit'] : array();

      // Replace core's registration function with LT's registration function.
      // Put the LT submit handler first, so other submit handlers have a valid
      // user to work with upon registration.
      $key = array_search('user_register_submit', $form['#submit']);
      if ($key !== FALSE) {
        unset($form['#submit'][$key]);
      }
      array_unshift($form['#submit'], 'logintoboggan_user_register_submit');
      if ($mail || $pass) {
        $form['#validate'][] = 'logintoboggan_user_register_validate';

        //Display a confirm e-mail address box if option is enabled.
        if ($mail) {

          // Make sure user help is at the top of the form.
          $form['user_registration_help']['#weight'] = -100;
          $form['conf_mail'] = array(
            '#type' => 'textfield',
            '#title' => t('Confirm e-mail address'),
            '#weight' => -28,
            '#maxlength' => 64,
            '#description' => t('Please re-type your e-mail address to confirm it is accurate.'),
            '#required' => TRUE,
          );

          // Weight things properly so that the order is name, mail, conf_mail, then pass
          if (isset($form['account'])) {
            $form['account']['#weight'] = -50;

            // Make sure account form group is at the top of the display.
            $form['account']['name']['#weight'] = -30;
            $form['account']['mail']['#weight'] = -29;
            $form['account']['conf_mail'] = $form['conf_mail'];
            unset($form['conf_mail']);
            $form['account']['conf_mail']['#weight'] = -28;
          }
          else {
            $form['name']['#weight'] = -30;
            $form['mail']['#weight'] = -29;
          }
        }
        $min_pass = variable_get('logintoboggan_minimum_password_length', 0);
        if ($pass && $min_pass > 0) {
          $description = t('Please choose a password for your account; it must be at least %length characters.', array(
            '%length' => $min_pass,
          ));
          if (isset($form['account'])) {
            $form['account']['pass']['#description'] = $description;
          }
          else {
            $form['pass']['#description'] = $description;
          }
        }
      }
      break;

    // Unset the ability to add the pre-auth role in the user admin interface.
    case 'user_admin_account':

      // Test here for a valid pre-auth -- we only remove this role if one exists.
      $id = logintoboggan_validating_id();
      $pre_auth = !variable_get('user_email_verification', TRUE) && $id != DRUPAL_AUTHENTICATED_RID;
      $add = t('Add a role to the selected users');
      if ($pre_auth && isset($form['options']['operation']['#options'][$add]["add_role-{$id}"])) {
        unset($form['options']['operation']['#options'][$add]["add_role-{$id}"]);
      }
      break;

    // Password resets count as validating an email address, so remove the user
    // from the pre-auth role if they are still in it.
    case 'user_pass_reset':

      // We only want to run this code when the user first hits the reset login
      // form.
      if (arg(5) != 'login' && ($uid = (int) arg(2))) {
        if ($account = user_load(array(
          'uid' => $uid,
        ))) {
          $id = logintoboggan_validating_id();
          $in_pre_auth_role = in_array($id, array_keys($account->roles));
          if ($in_pre_auth_role) {
            _logintoboggan_process_validation($account);
            drupal_set_message(t('You have successfully validated your e-mail address.'));
          }
        }
      }
      break;
  }
}