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?

  • 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.
    – beltouche
    Commented Nov 30, 2021 at 21:08
  • 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.
    – Lambic
    Commented Dec 1, 2021 at 14:32


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.