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 có thể đánh mất niềm tin của nguời dùng

Facebook có thể đánh mất niềm tin của nguời dùng

Facebook đang hi vọng công cụ tìm kiếm mới của hãng sẽ là bước đột phá lớn kế tiếp trong làng công nghệ. Nhưng chắc chắn mạng xã hội phổ biến nhất thế giới sẽ cần nhiều sự hợp tác hơn từ phía người dùng.

14 điều tuyệt vời khó quên khi làm việc tại Google

14 điều tuyệt vời khó quên khi làm việc tại Google

Google luôn có nhiều điểm nhấn mà nhân viên chẳng thể tìm thấy tại bất kỳ đâu.

Cuộc thi lập trình nhanh trên di động Teckathon 2014 “ Hack Your Life”

Cuộc thi lập trình nhanh trên di động Teckathon 2014 “ Hack Your Life”

Tại Việt Nam, sự kiện Flappy Bird đã mang đến một hơi thở mới, một động lực mới cho những nhà lập trình và thiết kế trên điện thoại di động.

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

 

Diet con trung