4

I have an entity reference autocomplete which is populated using an Entity Reference View.

I would like users to be able to type in this autocomplete and for the suggestions to look like this:

"[title of node] [value of custom field]"

However, it seems like there is no way to achieve this. If I add extra fields into the Entity Reference View, these do not display. If I override the output of the title field in the view, this is ignored. It seems like the Entity Reference Autocomplete simply uses the entity title and ignores the 'Fields' section of the view.

Does anyone have any experience with this? I'm sure it was possible in D7!

2 Answers 2

4

You need to implement your own selection plugin for this.

I thought I've already answered a similar question but can't find it anymore.

Assuming the target is a node, extend from \Drupal\node\Plugin\EntityReferenceSelection\NodeSelection, otherwise from its parent or appropriate other child class and override \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection::getReferenceableEntities().

Make that available under its own plugin id (not default:something, just something) and select it in the entity reference field settings.

1
0

To add to Berdir's answer. If you are working with a custom entity. Its best to redefine the label method on the custom entity declaration for the autocomplete to work. Simply creating a EntityReferenceSelection plugin will not work with autocomplete since the autocomplete classes reference the label of an entity and there is no clear workaround that I know of?

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.