Hiding Form Fields in Drupal 8

If you have worked with the Field UI in Drupal 7 you will know that you are able to prevent fields from being displayed when viewing entities (e.g. content, users etc). It was fairly simple, you would go to the Manage Display tab of an entity and move the field to the ‘Hidden’ region as shown in the screenshot below.

Image

So you could hide a fields output from being displayed when viewing that entity. But what about when editing that entity? There was no way in the Drupal 7 Field UI to hide a field on a form. You would have to write some form of hook_form_alter() in a custom module and manually force the field to be hidden, like shown in this example.

You’ll be glad to hear that in Drupal 8 you no longer need to write any code to hide fields in a form! Quoting the official change record, “Fields can now be set to hidden via the form display configuration”.

Let’s take it for a whirl. We're going to add a new field to our user accounts and hide it on user forms (both registration and edit). Firstly, create a new field just as you would have in Drupal 7 by going to Configuration > People > Account settings. Let’s create a boolean labeled ‘Pants’.

Image

Set the On and Off values appropriately and hit save.

Image

Set the default value to “Yes” and hit save.

Image

Now is the important part. Go back to the account settings configuration page and go to the Manage Form Display tab. You can either change the widget to ‘- Hidden -‘ or simply drag the field down to the ‘Disabled' region. They both do the same thing. Don’t forget to hit save!

Image

Now, when a user goes to create a new account or edit an existing one, they will not be asked about their pants.

Image

However, because we set a default value for the field you will notice that after creating the new account the default value is displayed on our profile page.

Image

We can take this a step further by using form view modes. Go back to the Manage Form Display tab on the account settings configuration page and at the bottom enable custom display settings for the Register form view mode.

Image

Now we have 2 different view modes for our form. The ‘Default' mode is used when editing a user and the new ‘Register' mode for when creating a new user. Let’s display Pants again on the ‘Default' mode.

Image

When editing a user account, you will now see the form field for pants. But it will still be hidden on the registration form.

Image

We’ve only touched the surface for Drupal 8 view modes and if you are new to the subject, I would highly recommend taking a look at this excellent blog post by Addison Berry.

The possibilities are endless by combining the improved Field UI and view modes for entities in Drupal 8. Being able to hide fields on forms is just another dollop of icing on the cake to making Drupal delightful to build sites with.

Comments

I was going to suggest Field Permissions too. If you think about it, in fact you don’t really want to be hidding fields - what you are actually saying is that users don’t have permission to edit the field. Hence, it makes sense to use Field Permissions for this.

The use case for Field Permissions is slightly different - this forbids a user from ever editing the field.

But there are still cases where you'd just want to present "shorter" versions of an entity form. "User register" vs "User edit" is a good example: there might be fields that users *can* edit, you just don't want to bug them with those fields straight from the "register" form.

D7 had a one-off hackish option specifically to hide fields from the "register" form (field order still couldn't be changed though). In D8, "form modes" are a cleaner and more flexible way to do something similar for all entity types.

Ah yes, good point. I must have skipped over the bit where you mentioned that forms now had view modes. That’s awesome! I wonder if that will make theming Drupal forms any easier too?

I created a custom module for auto node title in Drupal 8.4.x . I works okay but when I hide(disable ) the title using the UI I can't submit the form. I get an error 'website encountered and error please try again later'. Is there another option rather than setting display to none via css?

Hi Nicholas,

I'd check your site's error log to be sure, but I'm guessing the error has to do with how the auto node title is being set. It's likely not set immediately when the form is submitted so validation is failing since the title field is usually a required field. The easiest thing to do is probably to create a hook_form_alter implementation that sets the #required property on the title to FALSE (as well as #access if you want to hide the field). You can see what this might look like, in part, in the example code here: https://drupal.stackexchange.com/questions/49047/hide-text-field-in-hoo….

Cheers,

Blake

Add new comment

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <code class> <ul type> <ol start type> <li> <dl> <dt> <dd><h3 id> <p>
  • Lines and paragraphs break automatically.

About us

Drupalize.Me is the best resource for learning Drupal online. We have an extensive library covering multiple versions of Drupal and we are the most accurate and up-to-date Drupal resource. Learn more