Problem/Motivation
When an autocomplete widget is used on a single value field the clientside javascript code treats it as a multivalue field if what is entered by the user contains commas. This results in undesired behavior where only the last value is autocompleted instead of the entire text as entered.
Steps to reproduce
1. Setup a new Autocomplete and set the "allowed number of values" to "1"
2. Type "Pine, Apple, P"
EXPECTED:
Matching tags with text "Pine, Apple, P", for example:
`Pine, Apple, Pine Apple Pen`
`Pine, Apple, Pen`
RESULT:
`Pen`
`Peter`
Proposed resolution
Always search full text in Autocomplete input
Remaining tasks
- Fix it to search full text on Autocomplete input
- Selection on Autocomplete should replace old tag
User interface changes
- TBA
API changes
Yes. Details TBD. But something like:
- A new Form API attribute to toggle the split behavior.
- A new setting passed into autocomplete.js to toggle the split behavior.
Data model changes
- TBA
Issue fork drupal-2864844
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
droplet CreditAttribution: droplet commentedComment #5
dpiSo effectively the results should not explode terms if cardinality==1. Should search literally what the user typed.
Moving out of er.module since its in core itself.
Comment #12
dwwI just found this issue helping someone in #contribute trying to improve the UI of https://www.drupal.org/project/address_autocomplete
The proposal here makes perfect sense to me. If a field can only store a single value, and the widget for that field is using autocomplete, then the autocomplete should not attempt to split any values and should always operate on the full string.
However, this is sort of tricky, since the autocomplete plumbing is happening at the level of a Form API element, while the question of field cardinality is in the Field API layer. So we're going to have to add a new Form API attribute to toggle this behavior, so that field widgets which have knowledge of field cardinality can get the Form API layer to do the right thing.
Furthermore, the splitValues() stuff in autocomplete.js itself needs to know about this. So we also need to be able to propagate the "hey, don't split stuff" flag all the way down into the JS being loaded. So I think it's safe to say this will require some API additions at multiple layers of the system to actually work. Starting to stub that out in the summary.
Comment #13
rhovland CreditAttribution: rhovland commentedComment #14
rhovland CreditAttribution: rhovland commentedAfter messing around with single value field autocomplete I think instead of an option to toggle behavior this should just be default behavior for autocomplete.
Commas are not allowed without quotations in entity reference fields which what autocomplete is traditionally used for. However autocomplete is also being used for other types of fields and forms that aren't entity references where commas are allowed and useful. Things such as autocomplete search boxes, address entry auto completion, and external API based auto completes to accelerate data entry.
It makes more sense to not handle commas at all except when a form element is an entity reference field.
If the default behavior of the autocomplete widget javascript was to just pass the whole value to the backend unless the API specifically says this is a field delimited by commas then it enables use of the autocomplete widget for other applications besides entity references such as search boxes.
Comment #18
DustinYoder CreditAttribution: DustinYoder commentedI agree, seems like autocomplete is just as likely to need the comma splitting as it is to get broken by that splitting. This should be an option that can be controlled by the field itself. Then the entity fields can turn this on and address fields, etc, can turn it off. Don't require core to figure out when it is needed, it surely shouldn't be turned on for all autocomplete fields by default like it is currently. Is there any solution for this yet? Probably needs changes committed to many modules to pull this off. We could default the value to ON and then modules that are breaking because of this can simply turn it off.
Comment #22
alecsmrekar CreditAttribution: alecsmrekar as a volunteer commentedI opened this PR with some initial work done on the JS side, it basically just avoids splitting the string if the form element has the
data-autocomplete-single
attribute.