Problem/Motivation

I'm using this module on a fixed-cardinality link field (Allowed number of values: 8) and noticed that clicking Remove on a row in the middle of the list duplicates the last filled row instead of clearing it.

Steps to reproduce

  1. Add a Link field to any content type.
  2. Set Allowed number of values to a fixed number greater than 1 (for example, 3).
  3. Create a node, fill all 3 rows with three different links (A, B, C), and save.
  4. Edit the node and click Remove on row 1 (B).
  5. Save the node and reopen it.

Expected: rows 1 and 2 contain A and C; row 3 is empty.
Actual: the last row repaints with C, so the saved node has A, C, C.

Digging into multiple_fields_remove_button_fixed_submit_handler(), I found that the last delta gets blanked like this:

NestedArray::setValue($formInputs, $moving_element['#parents'], [
  'target_id' => '',
  '_weight' => $cardinality - 1,
]);

That works fine if the field is an entity_reference (which has a single target_id input), but a link field has uri and title inputs, it doesn't have a target_id at all.

So when the AJAX rebuild looks for uri and title on the last delta, it finds nothing, falls back to the default value (whatever's saved on the entity), and renders that.

Proposed resolution

Instead of writing a hardcoded target_id/_weight shape, build the empty row from the actual widget.

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#8 befor patch.png35.76 KBarjunghuge
#8 after patch.png36.25 KBarjunghuge
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

luongosb created an issue. See original summary.

luongosb’s picture

Version: 8.x-1.x-dev » 8.x-2.3

luongosb changed the visibility of the branch 3588076-fixed-cardinality-remove-button to hidden.

luongosb changed the visibility of the branch 3588076-fixed-cardinality-remove-button to active.

luongosb changed the visibility of the branch 3588076-fixed-cardinality-remove-button to hidden.

luongosb’s picture

Status: Active » Needs review
arjunghuge’s picture

StatusFileSize
new36.25 KB
new35.76 KB

Issue reproduced before applying the patch.

Patch tested:
Patch from MR !15

Environment:
Drupal version: 10.6.7
Module version: 2.3.0
OS: Windows (WSL)
Browser: Chrome

Steps followed:
1. Installed Drupal and enabled Multiple Fields Remove Button module.
2. Created a content type with a Link field (cardinality set to 3).
3. Added values A, B, and C.
4. Removed the middle value (B) and saved the node.
5. Reopened the node.

Observed result before patch:
- Last value was duplicated (A, C, C).

After applying the patch:
- Issue is fixed., Values are correctly updated (A, C) without duplication.
- Tested on existing content and verified with new content as well.

RTBC +1

daddison’s picture

Status: Needs review » Reviewed & tested by the community

I also observed the issue before applying the patch from MR !15 on module version 8.x-2.3 running Drupal 11.3.8. RTBC++.