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.

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.

  • 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. – Bernt Andreas Drange May 30 '17 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 Jun 8 '17 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? – Bernt Andreas Drange Jun 8 '17 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 Jun 9 '17 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

  • Thanks minnur! Have you considered putting this on drupal.org? – Bernt Andreas Drange Sep 15 '17 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 Sep 15 '17 at 23:16

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.