Problem/Motivation
The order of the media items is messed up when using Media Library widget in a particular use-case (when arranging the media items and after that inserting a new media).
Steps to reproduce:
1. Create a Content Type with an unlimited media reference field.
2. In form display for that field chose the Media library widget.
3. Add new content of that Content Type.
4. In the media field add more than 3 media items.
5. Save the node.
6. Go to the edit page of that node.
7. Re-arrange the media by drag and drop.
8. Press the "Add Media" button.
9. Select a new image to insert and press the "Insert Selected" button.
10. The media items will be ordered correctly but their weights will be messed up. You can look at them by pressing "Show media item weights".
11. Save node.
Expected behavior:
The order of the media should remain the way it was arranged before inserting a new image and the new image should be the last one.
Current behavior:
The media items order is not the way it was arranged.
Proposed resolution
I didn't find what was the cause of this problem but one of the solutions/workarounds will be to get the weights from the $field_state (which are the correct ones) and put them in the $element in the updateWidget memthod.
Remaining tasks
Add tests.
| Comment | File | Size | Author |
|---|---|---|---|
| #17 | before-patch.jpg | 225.58 KB | ranjith_kumar_k_u |
| #17 | after-patch.jpg | 224.5 KB | ranjith_kumar_k_u |
| #16 | Before Patch #14.png | 109.41 KB | janmejaig |
| #15 | After Patch #14 .png | 111.47 KB | janmejaig |
| #14 | interdiff_10-14.txt | 901 bytes | vsujeetkumar |
Issue fork drupal-3115054
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
sergiuteaca commentedThe workaround patch
Comment #4
ramya balasubramanian commentedComment #5
pranali.lanjewar commentedAs patch #2 applied failed, so setting as a need work.
Comment #6
Vidushi Mehta commentedAs #2 is failed to apply so added a new rerolled patch against 9.1.x
Comment #8
shailja179 commentedI have reproduced the issue. I am working on this.
Comment #9
phenaproximaThis sounds like a major bug to me. If the widget is getting the order wrong, it's losing user input and degrading the experience.
Comment #10
vsujeetkumar commentedFixing Test.
Comment #12
naresh_bavaskarComment #13
naresh_bavaskarComment #14
vsujeetkumar commentedFixing Tests
Comment #15
janmejaig commentedI have verified the above issue and found that after applying the patch #14 it is working fine as expected on Drupal 9.1.x .Please find the attachment for the issue.
Steps To Test:
1. Create a Content Type with an unlimited media reference field.
2. In form display for that field chose the Media library widget.
3. Add new content of that Content Type.
4. In the media field add more than 3 media items.
5. Save the node.
6. Go to the edit page of that node.
7. Re-arrange the media by drag and drop.
8. Press the "Add Media" button.
9. Select a new image to insert and press the "Insert Selected" button.
10. The media items will be ordered correctly but their weights will be messed up. You can look at them by pressing "Show media item weights".
11. Save node.
Expected behavior:
The order of the media should remain the way it was arranged before inserting a new image and the new image should be the last one.
Comment #16
janmejaig commentedComment #17
ranjith_kumar_k_u commentedI have applied the #14 patch ,the patch applied cleanly and it resolved the current issue.
.RTBC
before applying the patch
after
Comment #18
ranjith_kumar_k_u commentedComment #19
phenaproximaI don't think I understand the automated test coverage here. As far as I can tell, we are just removing an assertion, but I don't see how that actually tests the bug. Do we have an explicit automated test of this bug?
To clarify: what would be helpful here is a patch which contains ONLY an automated test which tries to do the thing which doesn't work, and therefore fails (what call a "fail patch"). That would prove that the bug exists, and that the automated test is actually testing for it. Then we could post an additional patch which contains that test and the code which fixes the bug.
Having said that, the patch in #10 seems to be on the right track. It doesn't remove any test coverage, but it modifies an existing test to more properly verify that what we actually expect to happen, happens. The fact that it fails means that it's actually checking for the bug. It also includes a fix, of course, but since it still fails, the fix probably isn't working working as intended. :)
So maybe we need to hack on this a bit more, but I don't think that removing test coverage is a viable path forward.
Comment #21
tanubansal commentedPatch #14 is working for me as well on 9.1
RTBC +1
Comment #25
chr.fritschI'm trying to fix this, but I didn't found the solution, yet. But at least I have written some tests.
Comment #26
chr.fritschI think I have it. Let's see if all tests will pass.
I copied all the stuff from the FileWidget. I will update the comments next week.
Comment #27
phenaproxima@chr.fritsch added tests, so removing the "Needs tests" tag.
Comment #28
phenaproximaComment #30
chr.fritschFixed the nitpicks
Comment #31
phenaproximaLooks pretty good to me! The test coverage is solid, and although the inner mechanics of what's going on here are hard to grok in the best of times (mostly because the field system is so arcane, especially with a complex, AJAX-heavy widget like the media library), there are at least comments with cross-references to other places so that another developer will hopefully be able to piece it together. I don't see any reason to block this fine bug fix any further.
Comment #32
alexpottCommitted and pushed 6470ab797cb to 10.0.x and 3e90900fc58 to 9.4.x. Thanks!
We're currently in a code freeze on production branches - will backport once that is over.
Comment #36
xjmCherry-picked to 9.3.x. We should probably set things to RTBC rather than PTBP if cherry-pick works so we don't lose them.
Comment #38
Christopher Riley commentedI Cannot update to Drupal 9.3.11. When I try to do my composer update I get the following:
Gathering patches for dependencies. This might take a minute.
- Installing drupal/core (9.3.11): Cloning b8978dd6a4 from cache
- Applying patches for drupal/core
https://git.drupalcode.org/project/drupal/-/merge_requests/1427.diff (Media library widget forgets ordering when adding or removing items)
Could not apply patch! Skipping. The error was: Cannot apply patch https://git.drupalcode.org/project/drupal/-/merge_requests/1427.diff
In Patches.php line 326:
Cannot apply patch Media library widget forgets ordering when adding or removing items (https://git.drupalcode.org/project/drupal/-/merge_requests/1427.diff)!
I have pulled all of the drupal/core patches from my composer file but still no luck. Any ideas as to what to do now?
Comment #39
cilefen commentedThis is fixed and released so the patch is no longer needed. That's how patches work.
Comment #40
cilefen commentedWe have a report that this causes a regression #3277737: When a user clicks on Add new media (multiple value field) inside an entity reference field (Block) within a content, all existing medias disappeared and are replaced by the new one uploaded.