You are here

function betterlogin_form_alter in Better Login 8

Same name and namespace in other branches
  1. 7 betterlogin.module \betterlogin_form_alter()

Implements hook_form_alter().

Autofocus on the username field. Some proper page titles would be nice for a change, User account is a bit boring..

File

./betterlogin.module, line 43
Make the login screens better :).

Code

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

  // Autofocus on the username field.
  // And add some pretty CSS :).
  // And a few other things too...
  if ($form_id == 'user_login_form' || $form_id == 'user_register_form' || $form_id == 'user_pass' || $form_id == 'user_pass_reset') {
    $form['name']['#attributes']['autofocus'] = 'autofocus';

    // We don't really need descriptions to tell us what we already know...
    unset($form['name']['#description']);
    unset($form['pass']['#description']);

    // Add in some CSS.
    $form['#attached']['library'][] = 'betterlogin/betterlogin_css';
  }

  // Exit if we're not on a target url, since the form may have been rendered
  // programmatically. We don't want to change page titles on other pages.
  $pages = [
    'login',
    'register',
    'password',
    'reset',
  ];

  // Retrieve an array which contains the path pieces.
  $current_path = \Drupal::service('path.current')
    ->getPath();
  $path_args = explode('/', $current_path);
  if (!in_array($path_args, $pages)) {
    return;
  }
}