Problem/Motivation

If a value entered in the "Other" area duplicates a value already stored in the field, duplicate values get stored in the database. The problem is particularly likely to come up with fields with longer option lists and/or when this module is used in conjunction with Chosen module to create a "free tagging" style interface.

Steps to reproduce

screenshot showing duplicate value entered in Other field

Screenshot showing result duplicate values displayed on node.

  1. Create a "list" field (text or integer) and allow multiple values. Example: "favorite colors"
  2. Create an allowed values list. Example: "Red, Green, Blue"
  3. Configure field to use Select or Other.
  4. Create a test node. Select one or more of the allowed values ("Green") AND select the "Other" option.
  5. In the "other" field, enter one of the selected, existing values ("Green").
  6. Save and view the node. The value entered in "Other" field ("Green") is now displayed twice.

Proposed resolution

Add code to remove duplicate values. The attached patch uses array_unique and seems to fix the problem for text and integer fields.

Remaining tasks

  • Confirm the behavior produced by this change is desired: duplicate entries should not be created.
  • Patch needs review and testing.
  • Consider if the code that removes duplicates should be case-sensitive or not. It is currently case-sensitive, so in the above example "green" would be allowed as a new entry but "Green" would be considered a duplicate.
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

isaac77 created an issue. See original summary.

isaac77’s picture

Title: Duplicate values » Duplicate values not removed before saving field
nikhil_110’s picture

Component: Field widget (non-specific or listed) » Code
StatusFileSize
new525.2 KB
new205.76 KB
new523.25 KB

I implemented patch on my local machine using Drupal 9.5 with Select (or other) v4.x-dev. It is working properly..
I have added the test steps and Attaching the screenshot for reference.Please review.

  • Setup Drupal with Select (or other) v4.x-dev
  • Enabled Select (or other)
  • Go to Admin > Structure > Content Types > Basic Page > Add New field List (text).
  • Go to Admin > Structure > Content Types > Basic Page > Manage form display > Go to List field and Chose Select or Other select list .
  • Go to Admin > Add content > Create new node by selecting Color name and with other options
ivnish’s picture

Status: Needs review » Needs work
Issue tags: +Novice

Needs reroll to MR

gokul.jayan made their first commit to this issue’s fork.

gokul.jayan’s picture

Status: Needs work » Needs review

Created an MR using the patch.