Copied from SearchApiAutocompleteInterface::getAutocompleteSuggestions(), this is currently the expected structure for a suggestion returned by a service class (and #2475435: Add support for switching the autocomplete implementation wouldn't change that):

  • prefix: For special suggestions, some kind of prefix describing them.
  • suggestion_prefix: A suggested prefix for the entered input.
  • user_input: The input entered by the user. Defaults to $user_input.
  • suggestion_suffix: A suggested suffix for the entered input.
  • results: If available, the estimated number of results for these keys.

This will then automatically be rendered with theme_search_api_autocomplete_suggestion(). In theory, it would be possible to override the theme function and definition to add more variables there and style them anyway you want, but I think we should provide a simpler solution for that.
When doing this, we could also make it possible to specify an URL to redirect to instead of a suggestion to search for – bringing us a lot closer to implementing #1567234: Merge with Search API Live results.
Finally, we should also make it possible to explicitly specify the autocompleted search terms instead of hard-coding this to suggestion_prefix + user_input + suggestion_suffix. The Solr implementation already adds a term key with exactly that, but I don't really know why since it's, at the moment, completely ignored (as far as I can tell).


For the technical implementation, I'd propose adding three new (optional) keys to suggestion structure:

  • render: If present, contains either HTML or a render array to show as the representation for this suggestion. Otherwise, the old variant is used.
  • term: The complete input this suggestion should autocomplete to.
  • url: Instead of being a suggestion, this should redirect to the given URL (which should be absolute). This is mutually exclusive with term.

I don't really think term is the best key here, but I'd leave it like that to remain compatible with the existing Solr code.

Comments

drunken monkey’s picture

Status: Active » Needs review
StatusFileSize
new12.88 KB

This is a first version of the patch, based on #2475435-2: Add support for switching the autocomplete implementation (to avoid rebasing headaches later).

  • drunken monkey committed 5d845fc on 7.x-1.x
    Issue #2502937 by drunken monkey: Added flexibility for returned...
drunken monkey’s picture

drunken monkey’s picture

Status: Needs review » Fixed

Committed.

drunken monkey’s picture

Status: Fixed » Needs review
StatusFileSize
new11.57 KB

Turns out the term key in the Search API Solr module was introduced in #2034719: Improved autocomplete suggestions to provide back the filtered term and serves a different purpose, incompatible with this new definition.
I'd therefore propose changing the term key to keys, which I also think is more appropriate. Or any other wishes here?
Patch attached. (Turns out I also blundered when committing this, adding a second SearchApiAutocompleteSuggesterInterface.php file instead of changing the existing one with the new keys – so another reason for this follow-up.)

drunken monkey’s picture

Status: Needs review » Fixed

Committed the follow-up.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.