Problem/Motivation

When you have an entity_autocomplete form element and the cardinality is set to 1, the input textbox should only accept a single value. If the cardinality is set to > 1 then >1 textboxes are presented to the user to enter each value. But the same behaviour occurs for each of the textboxes. It is possible to enter multiple comma-separated values in each one. And only the last value is saved.

Steps to reproduce

  1. Install a vanilla Drupal site version ^11.4 or 12-dev
  2. Create a custom content type with one field e.g. an integer field
  3. Create several nodes of the custom content type.
  4. Add an entity reference field to the custom content type
  5. Set the cardinality of the field to 1
  6. Edit one of the nodes. Enter the title of one of the other nodes then a comma, then the title of another node.
  7. Save the node. Note that only the value of the last entered referenced node is saved.
  8. Change the cardinality of the field to 2.
  9. Edit a node. Enter multiple titles of other nodes, comma-separated, in the 2nd entity-autocomplete input textbox
  10. Save the node. Note that only the last of the entered values is saved.

Proposed resolution

  1. Do not autocomplete past the first item.
  2. Add a form error if multiple values were entered.

Remaining tasks

User interface changes

Adding more than one comma-separated value in an entity-autocomplete form field will trigger a form error. No value will be saved until there is only one value entered in the textbox.

Introduced terminology

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

N/A

Original report by [username]

N/A

Issue fork drupal-2692289

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:

Comments

kevin.dutra created an issue. See original summary.

kevin.dutra’s picture

I'm going to leave this at normal priority for the time being. A case could be made for data loss, since it's dropping the other values without any warning, but I don't think that this form element is really being used on it's own much in core. I'll happily defer to someone else if they think priority should be higher.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.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.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.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.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.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.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should 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.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should 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.

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

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should 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.

oknate’s picture

Our QA team reported this bug. I've attached a video I created using the EntityReferenceAutocompleteWidgetTest.php using Drupal 8.8 alpha.

This is still an issue. There should be some sort of javascript preventing more than the allowed cardinality from being entered. Or perhaps validation on the backend that returns an error. Nothing terrible happens, but it allows you do something odd.

danielveza’s picture

Assigned: Unassigned » danielveza

Assigning to myself to have a dig in over the upcoming weekend. If others want to jump ahead feel free.

danielveza’s picture

EDIT - Ah sorry ignore this patch - Had a bunch of local changes I was playing with for a talk.

danielveza’s picture

Looking at the code it looks like this is desired behaviour - But I think I agree that it should fail validation instead of just grab the last value. Thats too confusing for editors IMO.

I think we need a test for this too if everyone agrees it should work this way.

larowlan’s picture

  1. +++ b/core/lib/Drupal/Core/Entity/Element/EntityAutocomplete.php
    @@ -240,8 +240,14 @@ public static function validateEntityAutocomplete(array &$element, FormStateInte
    +        $has_multiple = (sizeof(explode(',', $input)) > 1) ? TRUE : FALSE;
    

    I think this should use \Drupal\Component\Utility\Tags::explode instead of just regular explode

  2. +++ b/core/lib/Drupal/Core/Entity/Element/EntityAutocomplete.php
    @@ -240,8 +240,14 @@ public static function validateEntityAutocomplete(array &$element, FormStateInte
    +          $form_state->setError($element, t('This element can only have one value'));
    

    we can return here and avoid the else to reduce complexity

I agree - this sounds like a genuine bug - we should be able to reproduce it in a test

krzysztof domański’s picture

Version: 8.6.x-dev » 8.8.x-dev
Status: Active » Needs work
glenndw’s picture

I have edit the patch. When you have references for autocomplete that contain a comma (like node title) previous patch will not allow it.
When Drupal detects a comma in a reference it will put quotes around the hole thing. So now we have a regex that will see if a comma is in the value that is outside of quotes.

Example
This wil be oké
value -> "title part1,2,3 (node_ID)"

This is not
value -> "title part1,2,3 (node_ID)", "title part 4,5,6 (node_ID)"

In previous patch
value -> "title part1,2,3 (node_ID)" Trows Error message

hardik_patel_12’s picture

Assigned: danielveza » Unassigned
Status: Needs work » Needs review
Lal_’s picture

+++ b/core/lib/Drupal/Core/Entity/Element/EntityAutocomplete.php
@@ -240,8 +240,17 @@ public static function validateEntityAutocomplete(array &$element, FormStateInte
+        //$has_multiple = (sizeof(explode(',', $input)) > 1) ? TRUE : FALSE;
+        // instead of checking for comma we check if the field has comma's outside of quotes
+        // drupal puts quotes around references that have comma's in them

why are we commenting this ?

Lal_’s picture

Status: Needs review » Needs work
neslee canil pinto’s picture

Status: Needs work » Needs review
StatusFileSize
new1.19 KB
new1.05 KB

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

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should 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.

karthikeyan-manivasagam’s picture

Hi,

Thank you all, after applying the patch, When trying to add multiple values in comma separated instead of showing validation error it throws fatal error, on Drupal core 8.9.20

InvalidArgumentException: Placeholders must have a trailing [] if they are to be expanded with an array of values. in Drupal\Core\Database\Connection->expandArguments() (line 744 of /Applications/MAMP/htdocs/core/lib/Drupal/Core/Database/Connection.php)

Thanks
Karthikeyan

karthikeyan-manivasagam’s picture

Status: Needs review » Needs work

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

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

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

naveenvalecha’s picture

StatusFileSize
new1.16 KB

Here's the patch for 9.3.0

danflanagan8’s picture

This is an interesting issue because, as pointed out in #12, the code spells out this behavior:

+++ b/core/lib/Drupal/Core/Entity/Element/EntityAutocomplete.php
@@ -286,8 +286,15 @@ public static function validateEntityAutocomplete(array &$element, FormStateInte
       // Use only the last value if the form element does not support multiple
       // matches (tags).

The patches above address item #2 in the IS, which might not even be classified as a bug since it is the behavior specified in code. I think we all agree the current situation is bad UX though. But if we add this validation though we clearly must update the comment too.

+++ b/core/lib/Drupal/Core/Entity/Element/EntityAutocomplete.php
@@ -286,8 +286,15 @@ public static function validateEntityAutocomplete(array &$element, FormStateInte
+        $has_multiple = preg_match('/(,)(?=(?:[^"]|"[^"]*")*$)/', $input) ? TRUE : FALSE;

I also think that the regex in #26 should be removed in favor of calling Tags::explode and then calling count on the result. We might as well re-use the existing code that does this.

As far as item #1 in the IS, there's nothing in any of the patches that addresses that. That would probably involve some changes to the core/drupal.autocomplete library.

I would think that we could split those up into separate issues if we wanted. My guess is that messing with core/drupal.autocomplete could get really nasty.

Version: 9.2.x-dev » 9.3.x-dev
liquidcms’s picture

Just tossing in my test results and comments:
- tested with core 9.2.16 and patch from #20 (#26 doesnt apply)

without patch:
- only the LAST item on the list is passed to my form submit handler

with patch:
- if i enter 2 items i get a form validation error

this is independent of setting #tags

IMHO, the proper functionality here is:
- if something is set on the form (#tags seems like an odd attr to define this), nothing should autocomplete past the 1st item - i think currently missing
- if user just enters values manually without aide of autocomplete; then validation error (as the patch does now)
- if single attr is not set; then all items should be passed to form submit - not working now

are these the goals here?

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

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should 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.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should 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.

basvanderheijden’s picture

StatusFileSize
new1.14 KB

The patch in #25 does not work because of a malformed RegEx. My patch uses Tags::explode instead.

Version: 9.5.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. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

sascha_meissner’s picture

+1 Just stumbled into this ... IMHO it makes more sense to prevent completing multiple values client side in the first place. Just adding serverside validation is good but will just raise questionmarks in the opposite direction.

EDIT: looked a bit further and as the autocomplete js code will be replaced soon https://www.drupal.org/project/drupal/issues/3466088 my comment doesnt worth mention here

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

marckwee’s picture

StatusFileSize
new1.04 KB

Hi,

I made a patch that is working with D11.

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

phthlaap’s picture

Status: Needs work » Needs review

I added tests to reproduce the issue, and applied patch for it.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update, +Needs steps to reproduce

Could use a summary update to include the steps to rerpoduce

ironnuts’s picture

Title: EntityAutocomplete form element allows multiple values when #tags is FALSE » EntityAutocomplete form element with single cardinality allows input of multiple comma-separated values and saves the last value
Issue summary: View changes
ironnuts’s picture

Issue summary: View changes
ironnuts’s picture

Manually tested MR !16064 on Drupal 12.0-dev.

Only local images are allowed.

When attempting to save a node with multiple values entered in a single cardinality entity autocomplete element, save fails with an error message.

ironnuts’s picture

ironnuts’s picture

Issue summary: View changes
ironnuts’s picture

Status: Needs work » Needs review

There is now a failing test in place. The pipeline is otherwise all green. Manual testing seems acceptable.

quietone’s picture

Everyone, please remember to update the issue tags.

This is changing the user interface, so adding tag, and there should be before and after screenshots available from the issue summary.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs screenshots, +Needs usability review

@quietone left some good comments on the MR. If UX team is needed lets update the IS with screenshots to help them.