3

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!

3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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