You are here

function fancy_login_form_alter in Fancy Login 8.2

Same name and namespace in other branches
  1. 6.2 fancy_login.module \fancy_login_form_alter()
  2. 7.3 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 120
Holds hooks for the Fancy Login module.

Code

function fancy_login_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'fancy_login_user_login_form') {
    unset($form['name']['#attributes']['autofocus']);
    unset($form['name']['#description']);
    unset($form['pass']['#description']);
    $form['name']['#size'] = 15;
    $form['pass']['#size'] = 15;

    // If the SSL icon is to be shown on the form, insert it into the form in
    // the relevant location.
    $config = \Drupal::config('fancy_login.settings');
    $icon_position = $config
      ->get('icon_position');
    $https_config = $config
      ->get('https');
    if ($icon_position && $https_config) {
      $form['ssl_logo'] = [
        '#theme' => 'ssl_icon',
      ];
      if ($icon_position == 1) {
        $form['ssl_logo']['#weight'] = -100;
        $form['#attributes'] = [
          'class' => 'ssl_icon_above',
        ];
      }
      elseif ($icon_position == 2) {
        $form['ssl_logo']['#weight'] = 100;
        $form['#attributes'] = [
          'class' => [
            'ssl_icon_below',
          ],
        ];
      }
    }

    // Add a wrapper for our #ajax callback.
    $form['#prefix'] = '<div id="fancy_login_user_login_block_wrapper">';
    $form['#suffix'] = '</div>';

    // Add links to be used in our Fancy Login block, allowing the states to be
    // changed between login, register, and recover password.
    $items = [];
    if (\Drupal::config('user.settings')
      ->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) {
      $url = Url::fromRoute('user.register', [], [
        'attributes' => [
          'title' => t('Create a new user account.'),
        ],
      ]);
      $items['create_account'] = Link::fromTextAndUrl(t('Create new account'), $url);
    }
    $url = Url::fromRoute('user.pass', [], [
      'attributes' => [
        'title' => t('Request new password via email.'),
      ],
    ]);
    $items['request_password'] = Link::fromTextAndUrl(t('Request new password'), $url);
    $form['user_links'] = [
      '#theme' => 'item_list',
      '#items' => $items,
    ];

    // If Fancy Login is set to use https, change the
    // protocol of the form action if necessary.
    if ($https_config && strpos(\Drupal::request()
      ->getRequestUri(), '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:', \Drupal::request()
            ->getRequestUri()) . $form['#action'];
        }
      }
    }
    else {

      // Set the submit button of the forum to submit with #ajax. Attach
      // JavaScript settings to the element.
      $form['actions']['submit']['#ajax'] = [
        'callback' => 'fancy_login_user_login_block_ajax_callback',
      ];
      $form['actions']['submit-' . REQUEST_TIME] = $form['actions']['submit'];
      unset($form['actions']['submit']);
    }
  }
  elseif ($form_id == 'fancy_login_user_pass') {

    // Add a wrapper for the #ajax callback.
    $form['#prefix'] = '<div id="fancy_login_user_pass_block_wrapper">';
    $form['#suffix'] = '</div>';

    // Add links to be used in the Fancy Login block, allowing the states to be
    // changed between login, register, and recover password.
    $items = [];
    $url = URL::fromRoute('user.login', [], [
      'attributes' => [
        'title' => t('Log in to @site_name.', [
          '@site_name' => \Drupal::config('system.site')
            ->get('name') ?: t('this site'),
        ]),
      ],
    ]);
    $items['sign_in'] = Link::fromTextAndUrl(t('Sign in'), $url);
    if (\Drupal::config('user.settings')
      ->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) {
      $url = Url::fromRoute('user.register', [], [
        'attributes' => [
          'title' => t('Create a new user account.'),
        ],
      ]);
      $items['create_account'] = Link::fromTextAndUrl(t('Create new account'), $url);
    }
    $form['user_links'] = [
      '#theme' => 'item_list',
      '#items' => $items,
    ];

    // If Fancy Login is set to use https, change the  protocol of the form
    // action if necessary.
    $https_config = \Drupal::config('fancy_login.settings')
      ->get('https');
    if ($https_config && strpos(\Drupal::request()
      ->getRequestUri(), '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:', \Drupal::request()
            ->getRequestUri()) . $form['#action'];
        }
      }
    }
    else {

      // Set the submit button of the forum to submit with #ajax.
      $form['actions']['submit']['#ajax'] = [
        'callback' => 'fancy_login_user_pass_ajax_callback',
      ];
      $form['actions']['submit-' . REQUEST_TIME] = $form['actions']['submit'];
      unset($form['actions']['submit']);
    }
  }
}