You are here

function ip_login_form_user_login_form_alter in IP Login 4.x

Implements hook_form_FORM_ID_alter() for 'user_login_form'.

File

./ip_login.module, line 44
Hooks for the IP login module.

Code

function ip_login_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Adding the 'ip_login' cache tag even when the module is not configured to
  // use the 'form' login mode is needed so we can clear all the caches related
  // to this form (render, page cache, etc.) when the login mode is changed.
  $form['#cache']['tags'][] = 'ip_login';
  if (!\Drupal::config('ip_login.settings')
    ->get('form_login')) {
    return;
  }
  if ($matched_uid = IpLoginController::checkIpLoginExists(\Drupal::request())) {
    $form['actions']['ip_login'] = [
      '#type' => 'link',
      '#title' => t('Log in automatically by IP'),
      '#url' => Url::fromRoute('ip_login.dologin', [], [
        'query' => \Drupal::destination()
          ->getAsArray(),
      ]),
    ];
  }

  // The user login form needs to vary by the IP address of the current request
  // in order to be able to show the 'Log in automatically' link dynamically.
  $form['#cache']['contexts'][] = 'ip';
}