In D7, I am trying to "hide" a field label through form_alter:

 case 'postal_code_node_form':

   // Set fields as not required
    $form['title']['#required'] = FALSE;
    $form['body']['#required'] =  FALSE;

    // Add css to hide fields.
    $form['body']['#attributes']  = array('style' => 'display:none');
    $form['title']['#attributes'] = array('style' => 'display:none;');

    $form['body']['#label_display']  = 'hidden';
    $form['title']['#label_display'] = 'hidden';

    break;

Unfortunately, the label is still displayed. I could do it through CSS or JavaScript, but I would prefer doing this through hook_form_alter(), so all form modifications are at the same place.

up vote 3 down vote accepted

I know this is over a year old, but completely removing the form labels in this way is probably not a good idea in terms of accessibility.

Instead, take a look at #title_display defined by the FormAPI. FieldAPI has something very similar called #label_display.

  • Looking back, this actually is better. Thanks for sharing friend. – stefgosselin Apr 17 '13 at 4:37

Try:

unset($form['title']['#title']);
unset($form['body']['und'][0]['#title']);

you can use dpm($form); to find labels in the form array.

I was using CSS before and I'll go the hook_form_alter or hook_form_id_alter route as well.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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