Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide a field in a Drupal form

I have a specific content type in drupal6. I want to implement a hook, which hides the body field of that content type from the add form, but not from the edit form. How can I do that?

like image 643
mimrock Avatar asked Dec 16 '10 13:12

mimrock


People also ask

How do you hide a field in a form?

Select the element and click the gear icon to open the Properties panel. Go to the Advanced tab. Scroll down and toggle the Hide Field option to “On”.

How do I hide a form field in Drupal 8?

$form['field_name']['#attributes']['class'][] = 'hidden'; Drupal core will "display: none;" this element from core styles using the 'hidden' css class. Show activity on this post. You don't need to set invisible, it will be hidden by default unless the visible condition is met.

How do I hide a field label in Drupal 8?

Navigate to the "Manage Fields" Tab. Under Label choose "Hidden" and for Format, "Generic File" Save and create a new node of this content type and the name "file" should no longer be displayed.

How do I edit a field in Drupal?

In the content type's "display fields" tab, edit the display of the field for e.g. 'full node' to be editable. Create a new node of this content type and save it. Now go and visit this node. You will find that you can edit the field.


1 Answers

You can use hook_form_alter. Which you can programmatically alter the contents of the form api build. This gives you the full $form array of which you can simply unset($form['the_field_you_dont_want']);.

But the easier way to get rid of the body field is in the edit content type there is a field labelled 'Body field label:' just leave this blank and the body field will be omitted.

like image 194
Richard Avatar answered Nov 07 '22 01:11

Richard