Hide the title of a form element? I'd like to use placeholders instead of labels for a form built with the form API.

I can get placeholders working well with the Elements module, but now I want to hide the labels. If I just set ['#title'] = NULL; then form errors don't have descriptive names, and it ruins accessibility.

In Drupal 7, you could set the #title_display to invisible (documentation). Was something like this backported in a contrib module somewhere?

Or is there a way I can set my own class on the label element?
You could override the theme_form_element() function in your theme's template.php so that the label does not output:


// Remove  from output

function THEMENAME_form_element($element, $value) {
  // This is also used in the installer, pre-database setup.
  $t = get_t();

$output = '\n";
  $required = !empty($element['#required']) ? '*' : '';

if (!empty($element['#title'])) {
    $title = $element['#title'];
    if (!empty($element['#id'])) {
      $output .= $t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required));
    }
    else {
      $output .= '$t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required));
    }
  }

$output .= " $value\n";

if (!empty($element['#description'])) {
    $output .= ' ' . $element['#description'] . "\n";
  }

$output .= "\n";

return $output;
}


Alternatively, you could target the form id > label and set it to display:none without modifying the theme function.