"[节点标题] [自定义字段的值]"然而，似乎没有办法实现这一目标。如果我将额外的字段添加到实体参考视图中，则不会显示这些字段。如果我在视图中覆盖标题字段的输出，则会忽略这一点。它似乎实体参考自动填充只需使用实体标题并忽略视图的"字段" 部分。
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!
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
Make that available under its own plugin id (not default:something, just something) and select it in the entity reference field settings.
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?