You are here

function fancy_login_form_alter in Fancy Login 7.3

Same name and namespace in other branches
  1. 8.2 fancy_login.module \fancy_login_form_alter()
  2. 6.2 fancy_login.module \fancy_login_form_alter()
  3. 7.2 fancy_login.module \fancy_login_form_alter()
  4. 3.0.x fancy_login.module \fancy_login_form_alter()

Implements hook_form_alter().

File

./fancy_login.module, line 149
Holds hooks for the Fancy Login module.

Code

function fancy_login_form_alter(&$form, &$form_state, $form_id) {
  global $base_url;
  if ($form_id == 'fancy_login_user_login_block') {

    // Invoke all instances of hook_form_user_login_block_alter()
    foreach (module_implements('form_user_login_block_alter') as $module) {
      $function = $module . '_form_user_login_block_alter';
      $function($form, $form_state);
    }

    // Invoke all instances of hook_form_alter() with a $form_id of
    // 'user_login_block'.
    $id = 'user_login_block';
    foreach (module_implements('form_alter') as $module) {
      $function = $module . '_form_alter';
      $function($form, $form_state, $id);
    }

    // If the SSL icon is to be shown on the form, insert it into the form in
    // the relevant location.
    $icon_position = variable_get('fancy_login_icon_position', 0);
    if ($icon_position && variable_get('fancy_login_https', 0)) {
      $icon = theme('ssl_icon', array(
        'base_url' => $base_url,
      ));
      $form['ssl_logo'] = array(
        '#markup' => $icon,
      );
      if ($icon_position == 1) {
        $form['ssl_logo']['#weight'] = -100;
        $form['#attributes'] = array(
          'class' => array(
            'ssl_icon_above',
          ),
        );
      }
      elseif ($icon_position == 2) {
        $form['ssl_logo']['#weight'] = 100;
        $form['#attributes'] = array(
          'class' => array(
            'ssl_icon_below',
          ),
        );
      }
    }

    // Store the current path and set it before all other validation so that
    // any modules (such as Login Destination) that depend on the current
    // path will respond properly.
    $form['current_path'] = array(
      '#type' => 'value',
      '#value' => current_path(),
    );
    array_unshift($form['#validate'], 'fancy_login_insert_current_path');

    // Add a wrapper for our #ajax callback.
    if (!isset($form['#prefix'])) {
      $form['#prefix'] = '';
    }
    $form['#prefix'] = '<div id="fancy_login_user_login_block_wrapper">';
    if (!isset($form['#suffix'])) {
      $form['#suffix'] = '';
    }
    $form['#suffix'] = '</div>';

    // If Fancy Login is set to use https, change the protocol of the form
    // action if necessary.
    if (variable_get('fancy_login_https', 0) && strpos($base_url, 'https:') !== 0) {
      if (strpos($form['#action'], 'https') !== 0) {
        if (strpos($form['#action'], 'http') === 0) {
          $form['#action'] = preg_replace('/^http:/', 'https:', $form['#action']);
        }
        elseif (strpos($form['#action'], '//') === 0) {
          $form['#action'] = 'https:' . $form['#action'];
        }
        else {
          $form['#action'] = preg_replace('/^http:/', 'https:', $base_url) . $form['#action'];
        }
      }
    }
    else {

      // Set the submit button of the forum to submit with #ajax.
      $form['actions']['submit']['#ajax'] = array(
        'wrapper' => 'fancy_login_user_login_block_wrapper',
        'callback' => 'fancy_login_user_login_block_ajax_callback',
      );

      // The #ajax on submit buttons loaded using AJAX will not work with the
      // current form key if that form has previously been loaded, as the system
      // thinks that the #ajax has already been applied to the given submit
      // button and therefore skips it. This next step ensures that the submit
      // button has a unique key every time the button is loaded, so that the
      // system does not think the #ajax is already applied.
      $form['actions']['submit_' . REQUEST_TIME] = $form['actions']['submit'];
      $form['actions']['submit']['#access'] = FALSE;
    }
  }
  elseif ($form_id == 'fancy_login_user_pass') {
    form_load_include($form_state, 'inc', 'user', 'user.pages');

    // Invoke all instances of hook_form_user_pass_alter()
    foreach (module_implements('form_user_pass_alter') as $module) {
      $function = $module . '_form_user_pass_alter';
      $function($form, $form_state);
    }

    // Invoke all instances of hook_form_alter() with a $form_id of 'user_pass'.
    $id = 'user_pass';
    foreach (module_implements('form_alter') as $module) {
      $function = $module . '_form_alter';
      $function($form, $form_state, $id);
    }

    // Add a wrapper for our #ajax callback.
    if (!isset($form['#prefix'])) {
      $form['#prefix'] = '';
    }
    $form['#prefix'] = '<div id="fancy_login_user_pass_block_wrapper">';
    if (!isset($form['#suffix'])) {
      $form['#suffix'] = '';
    }
    $form['#suffix'] = '</div>';

    // Store the current path and set it before all other validation so that
    // any modules that depend on the current path will respond properly.
    $form['current_path'] = array(
      '#type' => 'value',
      '#value' => current_path(),
    );
    array_unshift($form['#validate'], 'fancy_login_insert_current_path');

    // Add links to be used in our Fancy Login block, allowing the states to be
    // changed between login, register, and recover password.
    $items = array();
    $items[] = l(t('Sign in'), 'user/login', array(
      'attributes' => array(
        'title' => t('Log in to !site_name.', array(
          '!site_name' => variable_get('site_name', t('this site')),
        )),
      ),
    ));
    if (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)) {
      $items[] = l(t('Create new account'), 'user/register', array(
        'attributes' => array(
          'title' => t('Create a new user account.'),
        ),
      ));
    }
    $form['links'] = array(
      '#theme' => 'item_list',
      '#items' => $items,
    );

    // If Fancy Login is set to use https, change the protocol of the form
    // action if necessary.
    if (variable_get('fancy_login_https', 0) && strpos($base_url, 'https:') !== 0) {
      if (strpos($form['#action'], 'https') !== 0) {
        if (strpos($form['#action'], 'http') === 0) {
          $form['#action'] = preg_replace('/^http:/', 'https:', $form['#action']);
        }
        elseif (strpos($form['#action'], '//') === 0) {
          $form['#action'] = 'https:' . $form['#action'];
        }
        else {
          $form['#action'] = preg_replace('/^http:/', 'https:', $base_url) . $form['#action'];
        }
      }
    }
    else {

      // Set the submit button of the forum to submit with #ajax.
      $form['actions']['submit']['#ajax'] = array(
        'wrapper' => 'fancy_login_user_pass_block_wrapper',
        'callback' => 'fancy_login_user_pass_ajax_callback',
      );

      // The #ajax on submit buttons loaded using AJAX will not work with the
      // current form key if that form has previously been loaded, as the system
      // thinks that the #ajax has already been applied to the given submit
      // button and therefore skips it. This next step ensures that the submit
      // button has a unique key every time the button is loaded, so that the
      // system does not think the #ajax is already applied.
      $form['actions']['submit_' . REQUEST_TIME] = $form['actions']['submit'];
      $form['actions']['submit']['#access'] = FALSE;
    }
  }
  elseif ($form_id == 'fancy_login_user_register_form') {

    // Invoke all instances of hook_form_user_register_form_alter()
    foreach (module_implements('form_user_register_form_alter') as $module) {
      $function = $module . '_form_user_register_form_alter';
      $function($form, $form_state);
    }

    // Invoke all instances of hook_form_alter() with a $form_id of
    // 'user_register_form'.
    $id = 'user_register_form';
    foreach (module_implements('form_alter') as $module) {
      $function = $module . '_form_alter';
      $function($form, $form_state, $id);
    }

    // Add a wrapper for our #ajax callback.
    if (!isset($form['#prefix'])) {
      $form['#prefix'] = '';
    }
    $form['#prefix'] = '<div id="fancy_login_user_register_form_block_wrapper">';
    if (!isset($form['#suffix'])) {
      $form['#suffix'] = '';
    }
    $form['#suffix'] = '</div>';

    // Store the current path and set it before all other validation so that
    // any modules that depend on the current path will respond properly.
    $form['current_path'] = array(
      '#type' => 'value',
      '#value' => current_path(),
    );
    array_unshift($form['#validate'], 'fancy_login_insert_current_path');

    // Add links to be used in our Fancy Login block, allowing the states to be
    // changed between login, register, and recover password.
    $items = array();
    $items[] = l(t('Sign in'), 'user/login', array(
      'attributes' => array(
        'title' => t('Log in to !site_name.', array(
          '!site_name' => variable_get('site_name', t('this site')),
        )),
      ),
    ));
    $items[] = l(t('Request new password'), 'user/password', array(
      'attributes' => array(
        'title' => t('Request new password via e-mail.'),
      ),
    ));
    $form['links'] = array(
      '#theme' => 'item_list',
      '#items' => $items,
    );

    // If Fancy Login is set to use https, change the protocol of the form
    // action if necessary.
    if (variable_get('fancy_login_https', 0) && strpos($base_url, 'https:') !== 0) {
      if (strpos($form['#action'], 'https') !== 0) {
        if (strpos($form['#action'], 'http') === 0) {
          $form['#action'] = preg_replace('/^http:/', 'https:', $form['#action']);
        }
        elseif (strpos($form['#action'], '//') === 0) {
          $form['#action'] = 'https:' . $form['#action'];
        }
        else {
          $form['#action'] = preg_replace('/^http:/', 'https:', $base_url) . $form['#action'];
        }
      }
    }
    else {

      // Set the submit button of the forum to submit with #ajax.
      $form['actions']['submit']['#ajax'] = array(
        'wrapper' => 'fancy_login_user_register_form_block_wrapper',
        'callback' => 'fancy_login_user_register_form_ajax_callback',
      );

      // The #ajax on submit buttons loaded using AJAX will not work with the
      // current form key if that form has previously been loaded, as the system
      // thinks that the #ajax has already been applied to the given submit
      // button and therefore skips it. This next step ensures that the submit
      // button has a unique key every time the button is loaded, so that the
      // system does not think the #ajax is already applied.
      $form['actions']['submit_' . REQUEST_TIME] = $form['actions']['submit'];
      $form['actions']['submit']['#access'] = FALSE;
    }
  }
}