Problem/Motivation

Only one image displayed but need remove three times to remove image completely after add three same images by file listing

Steps to reproduce

1. upload three same images at a time by file listing
2.Check the images and Remove it
3.Only one image displayed but need click remove button three times to remove image completely

Proposed resolution

Need to remove duplicate file target id form the form.

Remaining tasks

User interface changes

API changes

Data model changes

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

bijiaxing created an issue. See original summary.

connbi’s picture

StatusFileSize
new1.51 KB

change patch file name.

randy tang’s picture

StatusFileSize
new1.91 KB
randy tang’s picture

StatusFileSize
new1.8 KB
keshavv’s picture

Status: Active » Needs review
randy tang’s picture

StatusFileSize
new1.74 KB

When the array_parents level does not have data after 3, there will be a warning

benstallings’s picture

Status: Needs review » Needs work

Claude Code says:

Problems:

1. Code style throughout — Missing spaces after if/foreach (if(, foreach(), missing space after // in comment. Would fail phpcs.
2. Hardcoded array depth — $form[$array_parents[0]][$array_parents[1]] assumes the widget is always exactly 2 levels deep in the form. Entity reference fields nested inside paragraphs, inline entity forms, or layout builder will have different depths. This would either miss those cases or throw undefined index errors.
3. Unused variable — $key_value = '' is assigned and never used.
4. Overcomplicated traversal — The foreach ($array_parents as $k => $array_parent) loop manually walks the form array by reference. But $array_parents is already available — NestedArray::getValue($form, $array_parents) does the same thing in one clean call.
5. Unnecessary loop — The outer foreach iterates widget siblings but then checks in_array($key, $array_parents) — it's really just trying to find the triggering element's own target_id field. The loop is doing work that the $trigger['#array_parents'] already gives directly.
6. Wrong layer for deduplication — If duplicates are appearing in target_id, the root cause is in the selection/submission pipeline (the entity browser or the entities-selected JS event). Silently deduplicating in the AJAX callback masks the real bug and could hide other data integrity issues.
7. No test, no issue context in the code — The commit message says "3300147-5" but there's no explanation of what scenario produces duplicates or how to reproduce.

Verdict: This addresses a symptom, not a cause, and does so with fragile, style-noncompliant code. If duplicate entities are a real problem, the fix belongs where entities are added to the selection — not in the AJAX render callback.

benstallings’s picture

Assigned: connbi » Unassigned
Status: Needs work » Needs review
anybody’s picture

Version: 8.x-2.6 » 8.x-2.x-dev
Status: Needs review » Needs work
Issue tags: +Needs tests

Could we please have a test for these duplicates case to ensure it also works in the future (and proves was broken before)? Thanks!

benstallings’s picture

Assigned: Unassigned » benstallings
benstallings’s picture

Assigned: benstallings » Unassigned
Status: Needs work » Needs review