Fix widget preview/field mismatch after drag-and-drop reordering#5307
Fix widget preview/field mismatch after drag-and-drop reordering#5307
Conversation
Move item retrieval after original_deltas remapping to ensure preview uses correct item data when rankings are reordered via drag-and-drop. Co-authored-by: kevdevlu <[email protected]>
Apply same fix as az_ranking: move item retrieval after original_deltas remapping to ensure preview uses correct item data when cards are reordered. Co-authored-by: kevdevlu <[email protected]>
|
@copilot Now "Add Another Item" and "Update Preview" now work. But "Remove" doesn't seem to work anymore. Here's how I tested. I have 3 ranking items in an existing node: I reorder the items like this: I click on "Add Another Item". So far so good. The reordering above holds. Preview of Item 1 is now corresponded to Fields of Item 2 in position 1. |
Use separate $original_delta variable to fetch item and collapse status while keeping $delta for form element IDs. This ensures Remove button works correctly while maintaining fix for Update Preview/Add Another Item. Co-authored-by: kevdevlu <[email protected]>
Fixed in commit 2066ef9. The issue was that my previous fix overwrote the |
|
@copilot The case i specified in the previous comment with the "remove" button still does not work (same faulty behavior as before). On top of that i found another scenario with "Add Another Item" button that doesn't work: First, I want to say the original case still works: I have 3 ranking items in an existing node: I reorder the items like this: I click on "Add Another Item". This works. The reordering above holds and still works. But the case where the "Add Another Item" button doesn't work is: if i start with the same 3 ranking items, but drag and drop item 1 into position 3 (instead of position 2). Start with: I reorder the items like this: Then if i click on "Add Another Item", it shows: The fields are holding to the correct order but the previews of the fields are not positionally associated with the right fields. |
Instead of relying on original_deltas (which is only for deletions), build item mapping from submitted weight values. This correctly handles reordering in all scenarios including complex drag-and-drop patterns. Co-authored-by: kevdevlu <[email protected]>
Addressed code review feedback by removing the initial $status assignment that was always overwritten after computing $item_delta. Co-authored-by: kevdevlu <[email protected]>
Addressed code review feedback by removing trailing whitespace. Co-authored-by: kevdevlu <[email protected]>
Fixed in commits 3870847, a962573, and aa76dad. The previous approach using |
Description
After drag-and-drop reordering, widget previews displayed incorrect item data when AJAX callbacks triggered form rebuilds (via "Update Preview", "Add Another Item", or "Remove" buttons).
Root cause: The
$itemsarray parameter comes from entity storage and remains in database order, even after drag-and-drop reordering. The$deltaparameter represents the display position after reordering. The original code incorrectly used$items[$delta], which only worked on initial load. Theoriginal_deltasmapping in widget state is only populated during deletion operations (as noted in the original code comment "We may have had a deleted row"), not during reordering operations.Solution: Build a weight-based mapping from submitted form values to correctly identify which item corresponds to each display position:
$form_state->getUserInput())Implementation:
This approach correctly handles all reordering scenarios including:
Affected widgets:
AZRankingWidget,AZCardWidgetRelease notes
Related issues
How to test
Types of changes
Arizona Quickstart (install profile, custom modules, custom theme)
Drupal core
Drupal contrib projects
Checklist
Original prompt
az_rankingre-order preview issue #5156✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.