Convert your photoshop PSD file to drupal theme starting at €345 . Delivered in less than 4 working days

Upload the file

Hiding Drupal Form Labels

Hi again,
Today I was trying to unset the label of my form elements. For some reasons it was not just working.So i finally found this article on the web and this is a copy.

One of the nuisances of the form output generated by Drupal is that you can’t output just the form field. The field is always accompanied by a

One way to remove the label is to unset the title element for the field in question:

unset($form['ELEMENT_NAME']['#title']);

This would be done in your theme’s (or module’s) hook_form_alter(). The downfall to this method is that it will most certainly yield unpleasant results when the theme engine is trying to render the form. The best example of this is the use of the #title element in form validation. If the field does not validate (e.g., not filled in when required or invalid format), then the theme engine outputs #title is required or whatever the case may be. With the #title missing, only the “is required” is shown.
A Better Way

You can unobtrusively extend FormAPI, creating a new property to switch labels on and off. The first step to this is to override theme_form_element(). You can add this code to your theme’s template.php file. Most of the following code is from the core file form.inc except lines 16-26.

/**
* Overriding drupal form.inc
* Return a themed form element.
*/
function MYTHEME_form_element($element, $value) {
// This is also used in the installer, pre-database setup.
$t = get_t();
$output = '

if (!empty($element['#id'])) {
$output .= ' id="'. $element['#id'] .'-wrapper"';
}
$output .= ">\n";
$required = !empty($element['#required']) ? '*' : '';
if (!empty($element['#title']) && !$element['#hidetitle']) {
$title = $element['#title'];
if (!empty($element['#id'])) {
$output .= ' \n";
}
else {
$output .= ' \n";
}
}
$output .= " $value\n";
if (!empty($element['#description']) && !$element['#hidedesc']) {
$output .= '
'. $element['#description'] ."

\n";
}
$output .= "

\n";
return $output;
}

On line 16 we are checking for a new condition, the boolean value of $element['#hidetitle']. If the value is true, the if block is skipped, so the label is not displayed. We do a similar thing on line 26, this time with $element['#hidedesc'] to hide the description. These two conditions are the only differences from the vanilla form.inc.

Now that the boolean checks are in place for our new elements, we only have to add these new properties to our form elements. If you’re building a form using FormAPI, do something like this (example is an e-mail textfield):

$form['myfieldset']['email'] = array(
'#type' => 'textfield',
'#title' => t('E-mail Addresss'),
'#default_value' => 'email',
'#description' => t("Your e-mail address."),
'#maxlength'=> 250,
'#size' => 25,
'#required' => TRUE,
'#hidetitle' => TRUE,
'#hidedesc' => TRUE,
);

The new properties are on lines 9 and 10. When you output this field using <?php print drupal_render($form['myfieldset']['email']); ?>, the label will not be displayed. For a textfield the description is never displayed, so #hidedesc really only applies to fieldsets.

If you built your form using the Webform module, or you’re using someone else’s form, you’ll have to perform a hook_form_alter(). I made a webform_hidetitles module for one project that does only this. You would add the new properties like so:

if($form_id == 'webform_form_id') {
$form['submitted']['email']['#hidetitle'] = true;
}

Replace form_id on line 1 with your own form id (you can find this with Devel), and the form element (email in this case) with your own form element.
Conclusion

Now that you know how this works, maybe you can go write and contribute a module that makes it easy to optionally display form labels and descriptions.

You could follow the link at the footer if you want to read the original.
davidosomething

Share citiface post

BLOG

Podcasts

The developers diary

Webinars