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
- Add a Link field to any content type.
- Set Allowed number of values to a fixed number greater than 1 (for example, 3).
- Create a node, fill all 3 rows with three different links (A, B, C), and save.
- Edit the node and click Remove on row 1 (B).
- 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
| Comment | File | Size | Author |
|---|---|---|---|
| #8 | befor patch.png | 35.76 KB | arjunghuge |
| #8 | after patch.png | 36.25 KB | arjunghuge |
Issue fork multiple_fields_remove_button-3588076
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
Comment #2
luongosb commentedComment #7
luongosb commentedComment #8
arjunghuge commentedIssue 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
Comment #9
daddison commentedI also observed the issue before applying the patch from MR !15 on module version 8.x-2.3 running Drupal 11.3.8. RTBC++.