1

On the user login form, I am trying to take the "help text" and make it an input placeholder (so that the help text shows up in the input and disappears when the user enters their username and password).

Here's my code:

function mymodule_form_user_login_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  $form['name']['#placeholder'] = $form['name']['#description'];
  $form['pass']['#placeholder'] = $form['pass']['#description'];
//  $form['name']['#description']['#access'] = FALSE;
//  $form['pass']['#description']['#access'] = FALSE;
}

This adds the placeholder correctly but it doesn't hide the help text, so the text appears twice (in the input and below the input). In Drupal 7, I just set access to false, but in Drupal 8 the description is an object so I don't know what to do.

1
  • maybe unset($form['pass']['#description']); will work. Or a simple - $form['pass']['#description'] = ''; Don't have an instance of D8 at hand to test.
    – oksana-c
    Feb 21, 2016 at 19:11

1 Answer 1

2
unset($form['pass']['#description']);

or

hide($form['pass']['#description']);

is probably sufficient for the description element. I wouldn't use both above on form fields/controls though, because that may lead to unwanted validation errors and so forth.


$form['name']['#description']['#access'] = FALSE;

does not work for 'description' element as it is not a form control(field).

If anybody knows of better ways for removing description element, please chime in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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