Full user input is not searched when it contains a comma ","

Created on 12 August 2025, about 1 month ago

Problem/Motivation

I am currently investigating using this module and have found an issue with how core's autocomplete handles "," which results in unexpected results using this plugin.

In my context I am searching for an address - so a common user input might be "123 Foobar street, Fooville"

Viewing the network tab I can see that only the user input after the comma is sent to the backend (e.g "Fooville") which results in a sudden change in the returned results.

This behaviour appears related to how the core autocomplete java script handles inputs.

When the user inputs a value then autocomplete.searchHandler inside autocomplete.js is called.

This then is calling autocomplete.extractLastTerm which returns only the content after the last occurrence of ,

This is what is then passed to the callback / Autocomplete controller.

Proposed resolution

Inside search_api_autocomplete.js it looks like we could override the searchHandler option with a custom function that ensure the user input is passed to the backend as entered rather than split.

🐛 Bug report
Status

Active

Version

1.0

Component

General code

Created by

🇳🇿New Zealand ericgsmith

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @ericgsmith
  • 🇦🇹Austria drunken monkey Vienna, Austria

    drunken monkey made their first commit to this issue’s fork.

  • 🇦🇹Austria drunken monkey Vienna, Austria

    Thanks a lot for reporting this problem, and sorry it took me a while to get back to you!

    Unfortunately you are right that it doesn’t really seem easy to fix this problem ourselves as Core’s autocomplete.js doesn’t provide the required extension points.
    It seems extractLastTerm() is called from both searchHandler() (to determine whether the input is long enough to even attempt to autocomplete) and sourceData() (to actually provide the autocomplete suggestions, either from cache or by sending the request).

    Since both are actually set on Drupal.autocomplete.options, however, it does seem like it might in fact be possible to override them? So, in theory we could probably provide our own versions of them for our own autocomplete elements and leave the other ones unchanged.
    However, if we don’t want to duplicate their code (and manually have to port any changes Core makes to them) we’d then have to set some kind of global variable that this is one of our elements, then call the parent and then also override Drupal.autocomplete.splitValues() globally to change what it does (return single token or call old method) based on that global variable.
    Really not ideal, but feasible in theory. However, hopefully one of the Core issues will get fixed and it doesn’t have to come to this. I’d wait for now to see what happens in Core.

    Note, also, that (it seems) we would only have to fix splitValues(), as extractLastTerm() just uses that internally. If that method only returns a single token, then that token is also what extractLastTerm() will return.

    Anyways, thanks again for reporting!

Production build 0.71.5 2024