2

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.

2 Answers 2

4

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.

1
  • Looking back, this actually is better. Thanks for sharing friend. Apr 17, 2013 at 4:37
1

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 agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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