Hướng dẫn hiding Form Fields trong Drupal 8

Hướng dẫn hiding Form Fields trong Drupal 8

f 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.

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’.

Set the On and Off values appropriately and hit save.

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

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!

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

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.

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.

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.

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

Hướng dẫn hiding Form Fields trong Drupal 8

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.

Tags: 
Bạn thấy bài viết này như thế nào?: 
Average: 10 (1 vote)
Ảnh của Khanh Hoang

Khanh Hoang - Kenn

Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Facebook bị kiện vì lén lút “làm tiền” từ thông tin người dùng

Facebook bị kiện vì lén lút “làm tiền” từ thông tin người dùng

Facebook đã bị kiện vì cáo buộc xâm phạm tin nhắn riêng tư của khách hàng trên mạng xã hội và trao đổi với các hãng quảng cáo

Open Android Apps with Voice Control

Open Android Apps with Voice Control

There are lots of smartphone apps are available in the market which allows you to recognize your voice and controls the apps.

Thiết kế website sử dụng hệ màu  RGB, CMYK, và  Pantone

Thiết kế website sử dụng hệ màu RGB, CMYK, và Pantone

Để là một người thiết kế đồ họa tốt bạn phải nắm bắt được màu sắc, các tiêu chuẩn màu trong thiết kế đồ họa, nắm rõ kiến thức về màu sắc giúp các bạn chọn lựa hệ màu sắc phù hợp với công việc thiết kế của mình. Các hệ số màu mình giúp thiệu cho các bạn là RGB, CMYK, và hệ màu Pantone được sử dụng trong thiết kế website, thiết kế in ấn, thiết kế đồ họa kỹ thuật số…

Công ty diệt chuột T&C

 

Diet con trung