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:

  1. A new Form API attribute to toggle the split behavior.
  2. A new setting passed into autocomplete.js to toggle the split behavior.

Data model changes

- TBA

Issue fork drupal-2864844

Command icon 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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

droplet created an issue. See original summary.

droplet’s picture

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dpi’s picture

Component: entity_reference.module » field system
Category: Task » Feature request
Issue tags: +entity reference field

So 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.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

dww’s picture

Issue summary: View changes
Issue tags: -JavaScript +JavaScript

I 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.

rhovland’s picture

Title: Autocomplete with 1 limited number of value should always search full input text » Autocomplete used on a single value field should not split input on commas
Issue summary: View changes
rhovland’s picture

After 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.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

DustinYoder’s picture

I 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.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

alecsmrekar made their first commit to this issue’s fork.

alecsmrekar’s picture

I 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.