I want to alter entity auto complete results in hook_form_alter. My functionality is

1) I have a entity reference of taxonomy and bundle is tags which is Level 2 (Field Name: Product Child)

2) In this I select one term from Level2

3) I have another entity autocomplete which refers to taxonomy tags Level 1 (Field Name: Product Parent)

Now my question is if i select one Level 2 Term in Product Child Field , is there any option to show only parent terms of that Product child field in Product Parent Field Like :

Cabbage (L1)
 -> Fruit (L2)
 -> Flower (L2)

 -> Lemon Fruit(L2)
 -> Eatable (L2)

If I select Fruit in Product Child field I want to show only Lemon in Product Parent Field. Is there any way to do this.

3 Answers 3


I wrote a little module that shows how to alter entity autocomplete suggestions. You could try modify it to meet your needs. https://github.com/minnur/Alter-Entity-Autocomplete

  • If I wanted to also display a field called field_code next to the title of the autocomplete dropdown, what code would I need to write in the EntityAutocompleteMatcher.php file? Jul 18, 2018 at 14:07
  • Nice one very handy
    – Leigh
    Jul 26, 2019 at 14:10
  • @ACanadianCoder you would need to modify https://github.com/minnur/Alter-Entity-Autocomplete/blob/master/src/EntityAutocompleteMatcher.php on line 49 by adding something like $entity->get('field_code')->value in case if field_code is a simple string/float or integer field. Something like this $label = $label . ' (' . $entity_id . ') [' . $type . $status . '] code - ' . $entity->get('field_code')->value; . Make sure you check if field in the entity exists before you make the change.
    – minnur
    Jul 27, 2019 at 23:38

Autocomplete currently doesn't receive the parent node as context.

You could try to use JS for different autocomplete configurations to pass that data long somehow. Have a look at how the Dynamic Entity Reference module switches out the autocomplete to support different target entity types.

You would also need to implement your own selection plugin I suppose, so that you can pass it some configuration that you can then act on. Additionally, the configuration is stored server-side and the client only gets a random key, so you likely actually need to use #ajax to rebuild the second field when the first changes.

So, not trivial and will require quite some custom code.

Possible alternatives:

  • Hierarchical Select, operates on a single field and has no D8 port yet.
  • Only allow the user to select the product child, implement a bit of custom code on hook_node_presave() to set the parent field automatically. We do similar things for e.g. a section vocabulary where we store the full hierarchy in a multi-value section_all field so that it is easier to query on. This would work even better in combination with the HS module above, since users could still select the parent first if you want that. But again, unfortunately no port yet it seems.

A much quicker solution that will only change the reference fields you want is making an EntityReferenceSelection plugin and selecting it as the "Reference method" in the field settings. Just make sure your id matches your category in the docblock (otherwise it won't be available to select in field settings):


namespace Drupal\custom_module\Plugin\EntityReferenceSelection;

use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
use Drupal\node\NodeInterface;

 * Provides custom entity reference selection functionality.
 * @EntityReferenceSelection(
 *   id = "custom",
 *   label = @Translation("Custom Reference Method"),
 *   group = "custom",
 *   weight = 1
 * )
class CustomSelection extends DefaultSelection {

Then you can override the entity query or returned labels.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.