Posted on Leave a comment

How to add hierarchical select form fields in drupal 8

Hierarchical select fields are form fields with a list of values dependent on the selection of another field.

Requirements

  • Both fields must use the widget “Select list” or “Checkboxes/radio buttons”;
  • 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.
  • Business Rules Module

This example is using taxonomy, but you can use any entity as the parent-child relationship.

1)For this example, I’ve previously created and populated one Taxonomie vocabulary: car make and car model. 

2) Create a parent field car make ( taxonomy term ) by clicking on Manage / Content types / Article / Manage fields / Add field

Configure the parent field to REFERENCE to the car’s vocabulary. Save the field.

3) Create a view showing taxonomies with the type of (cars), and add the display type “Entity Reference”

4) Configure the CONTEXTUAL FILTERS and select Term Parents, filter to hide the view if the filter value is not available. Configure RELATIONSHIPS and select the Taxonomy term.

5) Click on the view format -> settings to configure the Search fields (required for entity reference display). Click the appropriated search field.

Save the view.

6) Go back to the Article fields configuration to create one field for the cars model
 Click on the Add field and create a new entity reference field.

7) At the field configuration, on the “Reference Type” box, select “Business Rules: Make field dependent using view” as the “Reference method”. On the field “View used to select the entities” select the “cars taxonomies” view we’ve just created. On the “Parent field” select the car make field.

8) Save the settings. Click on “Manage form display tab” and select the “Select list” widget for the make and model fields.

That’s it. Now when you create a new article and select car make, car model will be populated with the child categories of car make. 

Leave a Reply

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