1

In a project we have a Link field where editors can link to internal content or external content.

There are several cases where nodes of different content types have the same title.

Is there a way to display more information than just the node title in Link fields autocomplete widget - so that we can make it easier for Editors to pick the right node?

We are using LinkIt but that does not support the link module at the moment.

2 Answers 2

1

The autocompletion matches are provided by EntityAutocompleteMatcher::getMatches. The format is hardcoded as $key = "$label ($entity_id)". Label and entity id come from selection manager, which packages entity id and label pairs in an array keyed by entity bundle, so that could be used as a source for adding content type into the results. You'll need to override system.entity_autocomplete route's controller to get to your own matcher.

The same controller is used for rendering the selection dropdown though it does not display the entity ids, only labels (in web/core/misc/autocomplete.js). Overriding that is another thing you'll need to do.

4
  • Thanks. I was dreaming of a pointer to some contrib module I was unaware of. Looks like this solution may take more time than I have; I'll get back to this answer if I go down this route. Commented May 30, 2017 at 11:02
  • @BerntAndreasDrange I encountered the same sort of issue in a project I was working on. My implementation is different to the one suggested by holist but I created something that can append bundle or vocabulary labels to the respective type of entity reference autocomplete field. It is part of my module Starter
    – user34185
    Commented Jun 8, 2017 at 11:28
  • Hei BrynJ, thanks for that - looks/sounds like what we need. Would you consider creating that piece as a contrib module on drupal.org? Commented Jun 8, 2017 at 14:15
  • @BerntAndreasDrange possibly, although not sure when I will have time to do so (it's possibly a bit hacky in implementation too, but needs must sometimes). Feel free to take the relevant code from my module and spin it out into your module though.
    – user34185
    Commented Jun 9, 2017 at 8:49
1

Here is a simple module that I released on github. With some modification you may display anything you need in the autocomplete results/suggestions.

https://github.com/minnur/Alter-Entity-Autocomplete

3
  • Thanks minnur! Have you considered putting this on drupal.org? Commented Sep 15, 2017 at 11:30
  • @BerntAndreasDrange you're welcome! I will think about releasing the module. I have very limited time for contrib work, if I have absolutely nothing else to do I will look into that :)
    – minnur
    Commented Sep 15, 2017 at 23:16
  • The ideal improvement to the Link field is to support the use of a View to define the autocomplete options - as the Entityref field does. This would allow limiting content types, adding any info you like to the title, limiting to published items, etc.
    – liquidcms
    Commented Nov 10, 2022 at 22:42

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.