Score:0

Entity reference field dependant on other entity reference field

ph flag

Drupal 9.

I have a content type with two entity reference fields.

The first entity reference field references group entities provided by the group module.

The second entity reference field references nodes of a certain type.

I want the second autocomplete to only find nodes belonging to the group selected on the first entity reference field.

What's the easiest way to accomplish this?

Edit: Still haven't found an easy way to do this. I'm currently looking at creating a custom entity reference plugin but I'm not sure how to pass in the value of the other field. The other possibility is to use the entity reference view option, but I run into the same issue there.

Edit 2: I found a way to make this work by using an entity reference view and modifying the ViewsSelection plugin method getDisplayExecutionResults to add this:

if (isset($_POST['field_organization'])) {
  $arguments[] = $_POST['field_organization'];
}

But this seems like a really flaky way to do what I want. Is there a better/safer way?

beltouche avatar
cn flag
If the content type is a "member" of the group, you don't create an ER to the group that way. When you create the content within the group, you would only have access to nodes also in that group. If you really to connect the group as an ER, AJAX is your best bet to update the second field when the first has a value selected.
Lambic avatar
ph flag
This is an admin level content type so it's not a member of any group and has access to all groups. I'm trying to figure out the ajax route now but the autocomplete widget seems to be interfering.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.