3
function customization_form_user_register_form_alter(&$form, &$form_state, $form_id) {
 $form['account']['pass']['#attributes']['placeholder'] = t('Password');
}

I am trying to set placeholder on password field in registration form.

I seted placeholder on username and email, but on password field won't work...

Any help? Thank you!

9

Wish I'd seen this sooner. You've probably moved on from this problem, but for the benefit of other people seeking an answer to this question: (obviously replace mymodule with the name of your module)

/**
* Implementation of hook_element_info_alter ()
*/
function mymodule_element_info_alter (& $type)
{
  if (isset($type['password_confirm']['#process'])) {
    $func = '_mymodule_process_password_confirm';

    $type['password_confirm']['#process'][] = $func;
  }

  return $type;
}

/**
 * Do a bit of processing for the password_confirm element.
 * Original processing happens in expand_password_confirm()
 */
function _mymodule_process_password_confirm ($element)
{
  // Use the element's title, or create a custom title with the t() function.
  $element['pass1']['#attributes']['placeholder'] = $element['pass1']['#title'];
  $element['pass2']['#attributes']['placeholder'] = $element['pass2']['#title'];

  // Remove these lines if you want to keep the field labels.
  $element['pass1']['#title_display'] = 'none';
  $element['pass2']['#title_display'] = 'none';

  return $element;
}
|improve this answer|||||
  • De nada. Glad to hear it – tvanc May 22 '13 at 20:36
0

turibe solution is true. But you can save #title keys (do not remove), for compatibility with another modules.

/**
* Implementation of hook_element_info_alter ()
*/
function mymodule_element_info_alter (& $type)
{
  if (isset($type['password_confirm']['#process'])) {
    $func = '_mymodule_process_password_confirm';

    $type['password_confirm']['#process'][] = $func;
  }

  return $type;
}

/**
 * Do a bit of processing for the password_confirm element.
 * Original processing happens in expand_password_confirm()
 */
function _mymodule_process_password_confirm ($element)
{
  $element['pass1']['#attributes']['placeholder'] = t('Password');
  $element['pass2']['#attributes']['placeholder'] = t('Confirm password');

  // Remove these lines if you want to keep the field labels
  // Hide labels for pass fields
  $element['pass1']['#title_display'] = 'invisible';
  $element['pass2']['#title_display'] = 'invisible';

  return $element;
}
|improve this answer|||||

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.