Problem/Motivation
Using the media library widget, if you select an image then remove it, when using the widget again the count remains at 1 even though nothing is selected.
Steps to reproduce
- Install Drupal
- Enable media library
- Create a media image
- Add a media image field to the article content type with 1 item allowed
- Create an article
- Click Add media and select an image


- Remove the selected image
- Click Add media
- Note that the count says 1 of 1 even though nothing is selected

- Click Insert selected
- An empty selection is added
- Save
- See error 'No item selected'

After saving empty, and editing again, the count goes back to 0 as in the first screenshot.
Proposed resolution
Check if the currentSelection has a length before update the selectItemsText in the function updateSelectionCount(remaining) in media_library.ui.js file. And provide an empty string by default to the selectItemsText.
Existing function
function updateSelectionCount(remaining) {
var selectItemsText = remaining < 0 ? Drupal.formatPlural(currentSelection.length, '1 item selected', '@count items selected') : Drupal.formatPlural(remaining, '@selected of @count item selected', '@selected of @count items selected', {
'@selected': currentSelection.length
});
$('.js-media-library-selected-count').html(selectItemsText);
}could be
function updateSelectionCount(remaining) {
var selectItemsText = '';
if (currentSelection.length > 0) {
selectItemsText = remaining < 0 ? Drupal.formatPlural(currentSelection.length, '1 item selected', '@count items selected') : Drupal.formatPlural(remaining, '@selected of @count item selected', '@selected of @count items selected', {
'@selected': currentSelection.length
});
}
$('.js-media-library-selected-count').html(selectItemsText);
}Remaining tasks
- Agreed on the fix
- Write test for fix
- Review the latest uploaded patch
User interface changes
Correct selected count media is displayed to end users.
API changes
None
Data model changes
None
Release notes snippet
N/A
| Comment | File | Size | Author |
|---|---|---|---|
| #61 | 3167799-61.patch | 3.95 KB | rajiv.singh |
| #59 | 3167799-59.patch | 5.25 KB | _utsavsharma |
| #59 | interdiff_58-59.txt | 1.11 KB | _utsavsharma |
| #58 | 3167799-58.patch | 5.23 KB | sahilgidwani |
| #55 | interdiff_48-55.txt | 8.58 KB | sahilgidwani |
Comments
Comment #2
flocondetoileComment #3
pameeela commentedUpdated title and summary based on my testing, I was only able to reproduce this when adding, removing and re-adding an image.
Comment #4
bandanasharma commentedWrite the patch, please review.
Comment #6
flocondetoileOriginal issue was a bit different. Especially with an unlimited entity reference field, and so with the selected count "1 item selected" and/or "X items selected". This is a bit different with a ER field with a cardinality limited.
The issue summary update shows another bug, in a different context. And so the proposed solution is inaccurate.
Comment #7
pameeela commented@flocondetoile ah ok - that is why it is very helpful to include steps to reproduce.
Can you post specific steps to reproduce the original issue?
Do you think it’s possible to fix both at once or should it be two different issues?
Comment #8
flocondetoileComment #9
flocondetoileBoth issues looks very similar. This could be possible to fix both at the same time I think.
Comment #10
codersukanta commentedTried to reproduce the mentioned issue but its working fine for unlimited cardinality. I have found only one discrepancy in count message for limited cardinality, If we already select any media and upload a new file from the add file section then the message is showing like this "2 of 1 item selected".
I think the issue https://www.drupal.org/project/drupal/issues/3092536 will take care of it.
Comment #11
codersukanta commentedComment #12
flocondetoile@codersukanta I try to reproduced it on another project, and you're right. This is working fine. Sorry. Will try to figure why on the first project I have this small bug.
Comment #13
flocondetoileOK. The bug was on Drupal 8.9.3. This is fixed for D9 for the unlimited cardinality.
Comment #14
pameeela commentedThis bug definitely still occurs in 9.0.3 as described in my steps - only when you have selected an item already and removed it (for fields with 1 allowed item), or when you have selected an item and open the modal to select another one (for fields with multiple or unlimited).
I have updated the issue summary to reflect this.
Comment #15
phenaproximaComment #16
vsujeetkumar commented@phenaproxima Write the patch, Please review.
Comment #17
seanbThe change looks straightforward. Maybe we should remove the line:
const currentSelection = Drupal.MediaLibrary.currentSelection;entirely and always directly useDrupal.MediaLibrary.currentSelection.Btw, this needs to be changed in the
media_library.ui.es6.jsfile as well and then compiled as explained in https://www.drupal.org/docs/frontend-developer-tools-for-drupal-core.Last but not least, can we also add a test (and a fail patch) to show the issue and prove the fix to make sure we don't have regressions later on?
Comment #18
ayushmishra206 commentedMade the changes requested in #17. Please review.
Comment #19
seanbLooks good, nice work! Only thing we have to add is a test and a test-only/fail patch to prove this fixes the issue. Easiest might be to add a couple of lines somewhere in
Drupal\Tests\media_library\FunctionalJavascript\WidgetUploadTest.Maybe an extra assert right after this?
Like:
Comment #20
vsujeetkumar commented@seanB test added advised in #19, Please review.
Comment #21
vsujeetkumar commentedAdded test only patch, mentioned in #19
Comment #23
seanbThat is good enough for me, thank you. RTBC.
Comment #24
paulocsHello @all,
I tested patch #20 and it is working as expected.
I attached the same patch again because the last patch from the issue is the test-fail one and if patch #20 will be committed, it needs to be last patch inserted in the issue.
Next time you can attach the test fail patch and the solution patch together in the same comment @vsujeetkumar :)
RTBC+1
Cheers, Paulo.
Comment #25
janmejaig commentedI have checked the above issue with patch at #20. It is working fine to address the media counter issue, however I found a small glitch:
When we click the "Insert Selected" button without selecting any media the popup closes (which is fine), but on reopening the media popup, it displays an error message as "No items selected" which is confusing.
Ideally in this scenario I believe the media popup should not close and must display an error message.
Attaching a screenshot for reference .
Comment #26
janmejaig commentedComment #27
paulocsNot consider this patch and the interdiff.
Hello,This error is not occurring because of patch #20.
I added a patch that disables the button while no media is selected.
Cheers, Paulo.
Comment #28
paulocsPatch #27 and interdiff are wrong.
Please consider what I attached in this comment.
This patch disables the "Insert selected" button while no media is selected.
Thanks.
Comment #29
janmejaig commentedThis is not working as expected , the issue still persist.
Comment #30
paulocsHello @janmejaig,
what are the steps that you did to test the patch?
I'm asking because I saw the image that you attached and no file is selected and the button "Insert selected" is not disabled.
Did you clear your cache after you applied it?
Cheers, Paulo.
Comment #31
janmejaig commentedHello @paulocs ,
I have simply applied the patch at #28, after applying the patch I cleared the cache and hard refreshed the browser and progressed as per #25.
Hope I clarified your query , do let me know if anything specific I am missing here.
Comment #33
guypaddock commentedHere's a stab at backporting this to Drupal 8.9.x.
Comment #34
guypaddock commentedNot sure why #29 indicated this NW. With this patch, media library is working properly for us. The "Insert selected" button is only enabled when an actual selection is made -- even if a selection is made, the library is closed, and then the library is re-opened.
Setting to NR for additional confirmations just in case.
Comment #35
anushrikumari commentedComment #36
anushrikumari commentedRerolled as patch #29 for 9.2.x
Comment #37
anushrikumari commentedComment #38
abhijith s commentedApplied patch #36 and it is not fixing the problem.The 'No items selected' error still appears after applying this patch.
Adding recording
Comment #39
phenaproxima@Abhijith S, thank you for the screen recording! That's very helpful.
What's not clear, though, is if that issue is being caused by this patch. Can you reproduce that problem with a vanilla copy of Drupal core (without this patch applied)? Or does it only happen when this patch is applied?
Comment #40
michaellenahan commentedRemoving Europe2020 tag, since this issue was not worked on at Drupalcon Europe 2020.
Comment #41
michaellenahan commentedComment #44
tamsinfdHaving reviewed the video at #38, I can verify that the issue shown is not caused by patch #36. I was able to reproduce the issue with a vanilla installation of Drupal.
Comment #45
colorfulcoder commentedYesterday I helped tamsinfd with her first contribution, and we totally forgot to add that we did it during #ddd2022.
Comment #46
colorfulcoder commentedThis is a reroll of the patch in #36.
The old patch was outdated, and contained some mistakes causing it to not apply correctly.
This patch works well, except for one edge case. When you upload a new file, instead of choosing an existing one, and then discard it.
I think that is because the check for disabling the button is not done when a newly uploaded file is discarded. You will see that the media library dialog is shown again, with an enabled button.
Comment #47
colorfulcoder commentedAdded DrupalDevDays2022 tag
Comment #48
ranjith_kumar_k_u commentedFixed CS errors.
Comment #49
priyanka.sahni commentedVerified and tested the issue by applying patch 3167799-48.patch. Patch was successfully applied and issue got fixed.
Please refer to the video attached.
Comment #50
mradcliffeI added the Portland2022 tag. I think the issue is almost RTBC, but the issue summary needs to be updated to reflect recent comments. Specifically it would be nice to have for a committer to see @priyanka.sahni 's manual testing animated image in the issue summary as well as to confirm testing on 9.4.x branch.
Comment #51
phenaproximaThis definitely seems pretty good but I think it could use a little work, mostly to document the nature of the change.
I'm a little unclear on why we're changing these variable references. Is this the main part of the fix? If so, it would be good to have a comment explaining why we're referencing the counter this way, so we don't accidentally regress it.
🤔 I thought that
disabledwas a "present or absent" sort of attribute. Wouldn't we want this to be something like:Also, we usually only want to target classes prefixed with
js-in JavaScript. As I understand it, non-prefixed classes are intended for styling. Is there ajs-prefixed class we could use here, or some other way to select it?As with the counter, why are we referring to this variable by its fully qualified scope?
This line could use a comment to explain why we're asserting this text. Also, if we can scope it a little more clearly (maybe using
$assert_session->elementTextContains()or similar so that it's clear that we're looking at the selection counter of the media library, that would probably be helpful to future maintainers.Comment #55
sahilgidwani commentedRerolled patch for drupal core 9.5.x
Comment #56
gaele commentedComment #57
anybodyJust ran into this issue and can confirm it still exists.
Comment #58
sahilgidwani commentedAdding patch for v10.1.x
Comment #59
_utsavsharma commentedFixed failures in #58.
Comment #60
xjm#51 still needs to be addressed. When someone is able to address it, this issue can be converted to a merge request (but credit will not be granted for converting this issue to an MR without addressing #51). Thanks!
Comment #61
rajiv.singh commentedThe patch is not applying to D 11.2.10:
Why the patch wasn't applying:
Drupal 11.2.10 partially includes the fix: it removed const currentSelection = Drupal.MediaLibrary.currentSelection; and updated most references.
Lines 336-341 still referenced currentSelection (undefined), causing a bug.
The patch expected the old code structure (with the const declaration), so it couldn't apply.
What is fixed in attached patch :
Updated lines 334-342 to use Drupal.MediaLibrary.currentSelection instead of currentSelection.
Removed the patch from composer.json (line 299) since it's incompatible with D11.2.10.
The fix:
Changed currentSelection.includes(id) to use Drupal.MediaLibrary.currentSelection.indexOf(id) with position checking
Changed currentSelection.push(id) to Drupal.MediaLibrary.currentSelection.push(id)
Changed currentSelection.splice() to Drupal.MediaLibrary.currentSelection.splice()