Closes #5156: Fix Update Preview and Add Another Item Reorder Issue for AZ Ranking#5309
Closes #5156: Fix Update Preview and Add Another Item Reorder Issue for AZ Ranking#5309
Update Preview and Add Another Item Reorder Issue for AZ Ranking#5309Conversation
Test CasesA working fix will be able to solve all 3 of these issues:
|
The #after_build callback builds previews from Form API-populated values (which are always in the correct order after re-ordering). Our method before was messy and unreliable: trying to use widget_state and deltas to guess where the preview should go.
There was a problem hiding this comment.
Pull request overview
This pull request fixes a bug where ranking widget previews become out of sync with their corresponding form fields after drag-and-drop reordering and triggering AJAX actions (Update Preview, Add Another Item, or Remove buttons). The core issue was that previews were built from $items which could be in a different order than user input during AJAX rebuilds, while form fields were automatically populated by Drupal's Form API in the correct order.
Changes:
- Replaced preview building logic from
$itemsto use Form API-populated field values via an#after_buildcallback - Removed the
buildRankingPreview()method that built previews from$itemsin the wrong order - Added
afterBuildRebuildPreview()static method that builds previews from form field#valueproperties after the Form API has processed them
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
modules/custom/az_ranking/src/Plugin/Field/FieldWidget/AZRankingWidget.php
Outdated
Show resolved
Hide resolved
modules/custom/az_ranking/src/Plugin/Field/FieldWidget/AZRankingWidget.php
Outdated
Show resolved
Hide resolved
…vior Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
modules/custom/az_ranking/src/Plugin/Field/FieldWidget/AZRankingWidget.php
Show resolved
Hide resolved
modules/custom/az_ranking/src/Plugin/Field/FieldWidget/AZRankingWidget.php
Outdated
Show resolved
Hide resolved
modules/custom/az_ranking/src/Plugin/Field/FieldWidget/AZRankingWidget.php
Show resolved
Hide resolved
modules/custom/az_ranking/src/Plugin/Field/FieldWidget/AZRankingWidget.php
Outdated
Show resolved
Hide resolved
- Use $media_input === NULL instead of !$form_state->isRebuilding() to detect initial load, covering validation errors and other non-rebuilding states where user input is present. - Build CSS classes as an array using explode/array_filter/array_merge instead of string concatenation to avoid duplicate spaces.
|
Tugboat has finished building the preview for this pull request! Link: Dashboard: |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Description
Closes #5156 - ranking widget previews getting out of sync with their form fields after drag-and-drop reordering and clicking ajax buttons: "Update Preview", "Add Another Item", or "Remove".
The Core Problem
In
formElement(), we set bothdetails(form elements like textfields and selects) andpreview_wrapper(that renders the preview for the ranking item). The KEY ISSUE is: afterformElement()returns, Drupal's Form API treats these two children differently:detailscontains standard form elements (#type=>textfield,select, etc.). The Form API automatically populates their#valuefrom user input, keyed by the original form delta. This means after drag-and-drop reorder, the fields always show the correct data for their row.preview_wrapperis a plain render array (#theme=>az_ranking). The Form API does not touch render arrays — whatever values are set informElement()remain as-is. The preview was originally built from$items[$delta], which could be in a different order than user input during AJAX rebuilds.The Fix
If the fields are ALWAYS in the right order after ajax requests, why not build previews using the same Drupal form processing pipeline as the fields?! So instead of fidgeting / playing around with widgetState and trying to hack the deltas / original deltas using some sort of mapping: WE JUST BUILD THE PREVIEW AFTER the fields have been built.
#after_buildcallback:#valuesources as the form fields. We use this to replace the current function that builds from$item(buildRankingPreview()).#valuestructure differs from simple form elements. We just read from$form_state->getUserInput()to ensure it matches the same delta order as text fields.Release notes
Related issues
How to test
Make sure
az_paragraphs_rankingsis enabledTypes of changes
Arizona Quickstart (install profile, custom modules, custom theme)
Drupal core
Drupal contrib projects
Checklist