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

 
Cách cài đặt Network Load Balancing

Cách cài đặt Network Load Balancing

Mạng gồm có 2 Web Server IIS cùng host nội dung tương tự nhau.
Server 1 : SRV-02 , IP 10.2.0.12

Skype mới cho phép gọi video với Facebook (phiên bản Windows)

Skype mới cho phép gọi video với Facebook (phiên bản Windows)

Ngoài ra còn có nhiều tính năng mới nổi bật khác: gọi video với chất lượng full HD, chia sẻ màn hình nhóm,...

Tính năng REST Export trong Drupal 8 View

Tính năng REST Export trong Drupal 8 View

This blog is all about REST Export feature of Drupal 8’ view. Let me first brief you about what is the REST api and than I will explain you how REST Export

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

 

Diet con trung