9

I'm trying to customize user login form in /user/login page and I want to use a hook_form_alter function to add some awesome style and placeholder to each of user login form fields and remove or hide:
"Enter your admin username" and "Enter the password that accompanies your username" description expressions under each of theme.
To resolve this issue I added something like below code to my ThemeName.theme:

function ThemeName_form_alter(&$form, $form_state, $form_id) {
  if ( $form['#form_id'] == 'user_login_form' ) {
    $form['actions']['submit']['#attributes']['class'][] = 'button glow button-primary expanded';
    $form['keys']['#attributes']['placeholder']['name'] = t('User name');
    $form['keys']['#attributes']['placeholder']['password'] = t('Password');
  }
}

I try out many different ways but I can't get any result. in finally I want to achieve something like below image:
enter image description here

I read this pages but seems none work in Drupal 8:
How to modify the fields of user registration form and user login form?
Modifying the default user registration page
Customize user login block

5
  • 2
    I'd hazard a guess that user-login-form should be user_login_form in your function. Aug 14, 2016 at 18:27
  • Why not just do it with CSS? .user-login-form .description { display: none; } Aug 14, 2016 at 19:24
  • The actual strings that you are trying to remove are in UserLoginForm.php - it will be helpful for you to read that code to see how the form is constructed. Aug 14, 2016 at 19:29
  • I presume you can add a placeholder attribute when doing the form_alter() stuff Aug 14, 2016 at 21:07
  • 1
    See what Jeff Burnz has said in the answer below. Aug 15, 2016 at 12:21

2 Answers 2

13

You can add placeholders in hook_form_alter().

/**
 * Implements hook_form_alter().
 * @param $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 * @param $form_id
 */
function ThemeName_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_id == 'user_login_form' ) {
    // Add placeholders.
    $form['name']['#attributes']['placeholder'] = t('User name');
    $form['pass']['#attributes']['placeholder'] = t('Password');
  }
}

Use the Devel module functions to print out the form using kint, this way you can visibly see the entire structure of the $form array.

2
  • It seems that User Name should be Username or User name to t() function work's correctly. Aug 15, 2016 at 13:33
  • The problem is not of the function, but of the string within the function, I used what you used because I have to assume you may rely on that string in your translations.
    – Jeff Burnz
    Aug 15, 2016 at 18:33
2

SimpleLogin

https://www.drupal.org/project/simplelogin

It is a simple module for Customize Drupal Login, Password and Register pages with Background images.

Administrators can provide the ability to allow users to attach their own background images/ own background color to user login, password, registration pages. Better features including customize background color, image settings.

Features: Clean & Sleek Design, Customize background color, link color, submit button color, Customize image settings, Background image Opacity, Remove unwanted Css files from simplelogin pages, Login pages wrapper width, Mobile responsive,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.