Add support for Hierarchical / Dependent Entity Select

Created on 13 December 2022, almost 2 years ago
Updated 3 August 2023, over 1 year ago

Problem/Motivation

I was working on creating a solution for having one Entity Select be dependent upon the value of another Entity Select. I solved the problem for my use case, and I'm hoping maybe it could be generalized and included in the module.

Proposed resolution

Specifically, I solved it for two Entity Selects that are both populated by Entity Reference views. Upon selecting an entity (taxonomy term) from the first select, the second Entity Select is populated with its options (the value of the first was passed by the webform handler into the contextual filter for the view), which in my case were users.

Before I do any more work on generalizing this, I want to see if there is any interest in this becoming a feature included in the webform module.

See the attached webform handler file.

Also, here was a similar idea I found on Drupal Answers.

Here's a guide that shows how to configure some entity reference views . The Business Rules module didn't help with creating a solution the way I needed (updating entities on AJAX), however.

Remaining tasks

  • Evaluate community interest
  • Add configuration options to the webform handler for selecting two fields, one that the "parent" field, and one that's the "dependent" field that gets updated on AJAX
  • Add documentation or link to some for how to create and configure the views and a solution using the handler
  • Evaluate what else needs to be done

User interface changes

API changes

Data model changes

Feature request
Status

Closed: won't fix

Version

6.2

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • solideogloria - Did you end up creating a custom module for this? We have created 2 entity select dropdown with state and city taxonomies. But we are not clear on how to achieve the dependency between state and city dropdown. As you mentioned, Business rules module is not helping within the webform. Could you please add some more steps on how you achieved this?

  • Yes. You need a custom module. Download the webform handler and put it as a PHP file in .../custom_module/src/Plugin/WebformHandler/

    The elements in the example handler are called "first" and "second". Changing the value of "first" will update the values of "second". The second select element needs to be populated by an EntityReference view, and the view takes the value of "first" as an argument. The code in the webform handler passes the value to the view via AJAX when the value is changed.

  • solideogloria Thanks for the response. Regarding first and second in the handler, should those be replaced by the webform field keys?

  • Yes, but you may need to use Xdebug or something else to determine what you need. If your fields are in Fieldsets or something, then it will change.

  • Couldn't get this working. Replaced with the webform element key, but its not working as expected. I see the handler in the webform, but its not working when we use the key. And not sure what else should be replaced with first and second as there is no field names for the webform elements. I just see something like "name=test", but that's also not working.

  • Webform elements don't show in the "fields", they show in $form['elements']

Production build 0.71.5 2024