Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Drupal 10 Development Cookbook

You're reading from   Drupal 10 Development Cookbook Practical recipes to harness the power of Drupal for building digital experiences and dynamic websites

Arrow left icon
Product type Paperback
Published in Feb 2023
Publisher Packt
ISBN-13 9781803234960
Length 442 pages
Edition 3rd Edition
Tools
Concepts
Arrow right icon
Authors (2):
Arrow left icon
Matt Glaman Matt Glaman
Author Profile Icon Matt Glaman
Matt Glaman
Kevin Quillen Kevin Quillen
Author Profile Icon Kevin Quillen
Kevin Quillen
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Chapter 1: Up and Running with Drupal 2. Chapter 2: Content Building Experience FREE CHAPTER 3. Chapter 3: Displaying Content through Views 4. Chapter 4: Extending Drupal with Custom Code 5. Chapter 5: Creating Custom Pages 6. Chapter 6: Accessing and Working with Entities 7. Chapter 7: Creating Forms with the Form API 8. Chapter 8: Plug and Play with Plugins 9. Chapter 9: Creating Custom Entity Types 10. Chapter 10: Theming and Frontend Development 11. Chapter 11: Multilingual and Internationalization 12. Chapter 12: Building APIs with Drupal 13. Chapter 13: Writing Automated Tests in Drupal 14. Chapter 14: Migrating External Data into Drupal 15. Index 16. Other Books You May Enjoy

Customizing the form display for editing content

Form modes allow a site administrator to customize the edit form when modifying a content entity. In the case of nodes, you can rearrange the order of fields and change the form elements used for a fields node edit form. There is also the Field Group module. The Field Group module allows you to group fields into fieldsets.

In this recipe, we will install Field Group and modify the form display to create an Article content type.

How to do it…

  1. First, we must add the Field Group module to the Drupal site using Composer and then install it with Drush:
    composer require drupal/field_group
    php vendor/bin/drush en field_group –yes
  2. To customize the form’s display mode, go to Structure and then Content Types.
  3. We will modify the Article content type’s form. Click on and expand the Operations button and select Manage form display.
  4. Click Add field group to begin adding a new field group.
  5. Select Details Sidebar from Add a new group, give this a Label of Metadata, and click Save and continue.
  6. Press Create group on the next form and use the default values to finish creating the group.
  7. Drag the newly created Metata group (as shown in Figure 2.3) up from the Disabled section so that is it enabled. Directly above the Disabled label is fine.
  8. Take the Tags field and drag it so that it is nested under the Metadata group – below it, and slightly to the right:
Figure 2.3 – The Manage Display form with the Tags widget moved underneath the Metadata field group component

Figure 2.3 – The Manage Display form with the Tags widget moved underneath the Metadata field group component

  1. Click on the Save button at the bottom of the page to save your changes.
  2. Go to Create a New Article; you will find the Metadata tab in the sidebar, which contains the Tags field:
Figure 2.4 – The Article edit form, with the Tags element in the sidebar

Figure 2.4 – The Article edit form, with the Tags element in the sidebar

How it works…

When a content entity form is built, the form is aware of the display mode to be used. Then, it invokes the display mode to build the components for each field using the specified field widgets.

This allows you to customize specific parts of the form without having to replace the entire form. Developers can create new field widgets or leverage ones from contributed modules to enhance the functionality of forms.

Field Group does not create field widgets, but a new structure inside of the form display. It will then arrange field widgets into groupings. This provides a more organized content editing experience.

There’s more…

We will discuss more items for managing the form of a content entity in the following section.

Managing form display modes

Additional form display modes can be added by visiting Structure and then Display Modes under Form Modes. Each content entity type has a hidden default form mode that always exists. Additional form display modes can be added and configured using the display management form.

On their own, these forms and their configured field widgets are not directly integrated with Drupal. Using custom code, or even contributed projects, they can be used to embed for special uses.

For instance, there is the Register form mode for users. The user registration form is built using this display mode and the configured widgets instead of what is normally available when editing an existing user.

You have been reading a chapter from
Drupal 10 Development Cookbook - Third Edition
Published in: Feb 2023
Publisher: Packt
ISBN-13: 9781803234960
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image