How to use Dependent Fields in Drupal 9 Node Entity

31 / Aug / 2023 by Shubham Joshi 0 comments

Introduction

When working with complex forms, you often have fields whose state depends on the input values of other fields in the form. For example, you might have a select list of options, the options for which depend upon the value selected for another select list.

In this blog, I am representing one case study of my Drupal project where I have to achieve multi-dependent fields with the help of the Dependent Fields module. So, the fields must use the widget “Select list.” The child field type must be “Entity reference.” A view with the display mode “Entity Reference” must exist and is configured to receive as the first argument the value of the parent field.

Initial Requirements

We have to install Dependent Fields module, which is a contributed module and helps us to provide options while creating a field on any entity. For add field configuration such as “Make field dependent using views” as the “Reference method” and provide parent field. You can use any entity as the parent-child relationship.

For this example, I have created and populated two Content Types entities: Country and City & one Taxonomy vocabulary: State. For the State vocabulary, I’ve created an entity reference field to link it to the correct country. For the City entity, I’ve created an entity reference field to link it to the correct state.

Steps to follow

1. Create the parent field by clicking on Manage / Content types / Article / Manage fields / Add field.

2. Configure the parent field to reference the Country Entity.

3. Save the field

4. Create a view returning the child content filtered by the parent value (Manage / Structure / Views / Add view).

5. Creating an “Entity Reference” view for State Listing and adding a contextual filter to point to receive the parent value.

6. Click on the view format settings to configure the search option (required for entity reference display), check the appropriate search field, and also change “Pager” settings to set unlimited items; otherwise, your select list would have only 10 items as default settings.

7. Save the view.

8. Go back to the Article fields configuration to create one field for the State. Click on the “Add field” and create a new entity reference field.

9. At the field configuration, on the “Reference Type” box, select “Make field dependent using views” as the “Reference method.” On the field “View used to select the entities,” select the “State List” view we’ve just created. On the “Parent field,” select the country field.

10. Save the field and settings.

11. Similarly, we will create an “Entity Reference View” for populating Cities with a State “Contextual Filter” and save the view.



12. Go back to the Article fields configuration to create one field for the City. Click on the Add field and create a new entity reference field. At the field configuration, on the “Reference Type” box, select “Make field dependent using views” as the “Reference method.” On the field “View used to select the entities,” select the “City List” view we’ve just created. On the “Parent field,” select the state field and save the field.

13. Click on the “Manage form display tab” and select the “Select list” widget for the Country, State, and City fields. Save this setting.

Now, when you create a new article, the State field is driven by the Country field, and the City field is driven by the State field.

Conclusion

Drupal provides the best way by installing a single contributed module, and with the help of a few configurations, we can achieve complex forms and field dependency states with other field values easily.

Let us know in case of any queries, and please feel free to reach out via comments.

FOUND THIS USEFUL? SHARE IT

Tag -

Drupal

Leave a Reply

Your email address will not be published. Required fields are marked *