Tuesday, January 28, 2014

Setting "placeholder" attribute in Drupal 7 form elements

Here's a quick way on how to use HTML5 placeholder attribute in Drupal forms. Example below is altering the login form in a custom theme:

function mytheme_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_login') {
    $form['name']['#attributes'] = array('placeholder' => $form['name']['#title']);
    
    $form['pass']['#attributes'] = array('placeholder' => $form['pass']['#title']);
  }
}