It is possible to upload multiple images in a media field where the cardinality is set to 1. When this happens you cannot save the node or remove 1 of the images in the field.
To replicate:
1. Enable media module (with media library).
2. Add a media field to the basic page content type.
- Set the limit to 1.
- Set the media type to "Image"
3. Create a new basic page.
- When filling in the media image field:
- first upload an image and insert it.
- remove this image (so you have an image in the library.)
- click 'add media' and select the uploaded image from the library
- when it is selected, upload a new image
- insert both images (when using the new Claro theme, you don't see that you have 2 images selected)
- now you can't save your page or remove any of the selected images.
Short video added where the issue can be seen
| Comment | File | Size | Author |
|---|---|---|---|
| #69 | Screen Recording 2022-07-08 at 14.03.13.mov | 41.61 MB | fabianderijk |
| #68 | 3092536-68.patch | 1.75 KB | ki |
| #66 | 3092536-66.patch | 2.19 KB | NitinLama |
| #65 | 3092536-65.patch | 922 bytes | ki |
| #62 | 3092536-62.patch | 1.79 KB | ki |
Comments
Comment #2
kaduk commentedComment #4
andreasderijckeI can confirm the issue, with slightly difference from the steps above.
Follow all steps above until:
Then:
This was using Seven as backend theme.
Logs show:
Comment #5
himanshu_sindhwani commentedI will try to reproduce this issue and find a suitable fix. Therefore assigning to myself.
Comment #6
himanshu_sindhwani commentedHere is a patch that fixes the issue.
Comment #7
himanshu_sindhwani commentedComment #9
himanshu_sindhwani commentedUploading the patch with a fix.
Also updating the priority of the bug.
Comment #12
hardik_patel_12 commentedworking on this issue
Comment #13
himanshu_sindhwani commentedSorry @hardik_patel_12, I already did some workaround from the patch in #9.
Comment #14
himanshu_sindhwani commentedComment #15
hardik_patel_12 commented@himanshu_sindhwani , great start I have tried patch #9 , this is working fine for limited values like 1 .But it is not working in case of unlimited values. I have tried to solve that as well kindly look in to it.
Thanks
Comment #16
himanshu_sindhwani commentedI am not sure how this is an issue for the unlimited case? You can connect me on slack - himanshu_sindhwani is my user name, for further discussion. Moreover, there are some coding standard fixes issues in the patch in #14.
Comment #18
himanshu_sindhwani commentedFixing the coding standard issues in patch #15 and also I found currentSelection is const in es6.js so we can not do
Drupal.MediaLibrary.currentSelection = currentSelection = [];.Comment #20
pankaj.singh commentedComment #21
pankaj.singh commentedTested the patch given in #18, it works on my end.
Before patch when limit set to 1, we can able to select multiple images which is not the correct behavior. After applying the patch, when limit set to 1 it restricted to select only one image as expected behaviour. Please refer to SS for ref.
Comment #22
pankaj.singh commentedComment #23
snehalgaikwad commentedComment #24
snehalgaikwad commentedVerified on the local, patch in #18 is working correctly. Used media field with 1 limit and tried steps given in summary. Previously multiple images were getting added but after applying a patch, only one image can be added. Attaching screenshots.
Comment #25
snehalgaikwad commentedRe-rolling patch for 9.1.x.
Comment #26
snehalgaikwad commentedRe-rolled patch for Drupal 9.1.x
Comment #27
priyanka.sahni commentedComment #28
priyanka.sahni commentedVerified and tested by applying the patch #26.It looks good to me.Can be moved to RTBC.
Steps to test -
1. Go to the admin site.
2. Go to admin/modules
3. Enable media module (with media library).
4. Go to /admin/structure/types/manage/page/fields
5. Add a media field to the basic page content type.
- Set the limit to 1.
- Set the media type to "Image"
6. Go to admin/content.
7. Create a new basic page.
- When filling in the media image field:
- first upload an image and insert it.
- remove this image (so you have an image in the library.)
- click 'add media' and select the uploaded image from the library
- when it is selected, upload a new image
- insert both images (when using the new Claro theme, you won't able to add more than 1 image.
Before Patch -

After Patch -

Comment #29
priyanka.sahni commentedComment #30
ambuj_gupta commentedComment #31
ambuj_gupta commentedTesting Steps:
1. Enable Media module (with media library).
4. Go to "/admin/structure/types/manage/page/fields"
5. Add a media field to the basic page content type.
- Set the limit to 1.
- Set the media type to "Image"
6. Go to "/admin/content/media" and upload 2-3 images
7. Go to "/node/add/page"
8. Click on Add Media button. And upload the images existing/new.
Testing Results:
1. When we try to select images from existing images. Then we are not able to upload more than one image because selection for all images is disabled after we select the one image. Fixed
2. After selecting one image from the existing image, we are able to upload the new image using the file upload button. Need Fix
Comment #32
ambuj_gupta commentedComment #33
himanshu_sindhwani commented@ambuj30oct We can upload the image even we have selected the image from existing but after uploading only one file will be selected. The upload button is not disabled and should not be disabled. Please read the thred before working on issue. Moreover clear browser cache after applying the patch.
Comment #34
ambuj_gupta commented@himanshu_sindhwani
After uploading the image, both files are selected (previously added and recently added). This was the issue. In this case, the user is able to insert both images. Please check and let me know if I am wrong.
I suggest we should add some validation message on the click of Insert selected button, to fix this.
Comment #35
hardik_patel_12 commented@ambuj30oct , I have tested on local with d9.1.x setup it is working properly , can you please try patch at #26 with d9 setup then it would be great .
Thankyou
Comment #36
hardik_patel_12 commentedComment #37
ambuj_gupta commentedComment #38
ambuj_gupta commented@Hardik_Patel_12 Re-Tested after applying the patch #26 on fresh install Drupal 9. And it's working as expected.
Results:
1. After the image is uploaded, the previously selected image is deselected and the recently uploaded image is selected.
2. When we try to select images from existing images. Then we are not able to upload more than one image because after selecting one image the selection is disabled for all images.
Thanks
Comment #39
himanshu_sindhwani commentedComment #41
himanshu_sindhwani commentedIt is strange the test cases were passed earlier but got failed in retesting. I again tested it for 9.1, It is passing the test cases but still, there are two coding standard issues shown in drupal ci if anyone can look for.
Comment #42
himanshu_sindhwani commentedCorrecting the coding standard issues here.
Comment #43
himanshu_sindhwani commentedComment #44
ambuj_gupta commentedComment #45
ambuj_gupta commentedTested and verified by applying patch #42. The patch is not working as expected.
After selecting an image, when we browse and upload a new image, all the images are de-selected.
In this case, the currently uploaded image must be selected.
Comment #46
tanubansal commentedTested with the above mentioned test steps and latest patch. Using Claro theme, only one image can be uploaded at a time. Rest all images are getting de-selected
Comment #47
hardik_patel_12 commentedThe patch at #26 is working and the latest patch at #42 tried to solve coding standard error but it is still present there. So we just have to remove coding standard errors from the patch at #26 and which are as follows.
Comment #48
hardik_patel_12 commentedKindly review a new patch , solving coding standards error as well.
Comment #50
hardik_patel_12 commentedReview a new patch.
Comment #51
tanubansal commentedAfter adding #50, code changes are clearly visible.
This can be moved to RTBC
Comment #52
phenaproximaI think that #3092536: Possible to select multiple images in a media field where only 1 value is accepted is a duplicate of this one (or vice-versa).
Comment #53
himanshu_sindhwani commented@phenaproxima, you have tagged this same issue here in #52
Comment #54
phenaproximaD'oh! You're right; fixed now.
Comment #55
abhisekmazumdarLooks good to me . Followed the above testing steps.
Moving this to RTBC.
Comment #56
phenaproximaThis needs automated test coverage before it can be committed.
Comment #59
phenaproximaTransferring credit from #3160240: User can select multiple media entities but only one is allowed, which I have closed as a duplicate of this one.
Comment #60
phenaproximaComment #61
phenaproximaIt turns out this is a duplicate of #3059955: It is possible to overflow the number of items allowed in Media Library, which has more activity and seems to be further along. With that being the case, I'm going to close this issue in favor of that one. I will also transfer credit.
Comment #62
ki commentedCleaned up the #50 patch to work with Drupal 9.2
Comment #63
yahyaalhamadApplied patch #50, it is working, but when selecting an item then adding a new file and clicking on "Save and Insert", it will add both items. I'm not sure how it can be fixed, should it be from the backend or the frontend? Maybe adding a simple file upload event to clear up the selections would work.
Comment #64
dariemlazaroThe patch in the comment #62 works perfect in Drupal 9.2.8. Thanks.
Comment #65
ki commentedCleaned up the #62 patch to work with Drupal 9.3.3
Comment #66
NitinLama commentedCleaned up the #65 patch to work with Drupal 9.3.x
Comment #67
dariemlazaroThe patch in the comment #62 not works in Drupal 9.3.x. But the patch in the comment #66 now works perfect in Drupal 9.3.9. Thanks for the support.
Comment #68
ki commentedCleaned up the patch to work with Drupal 9.4.0
Comment #69
fabianderijk#68 still doesn't work for me correctly (on Drupal 9.4.1). When I click an image, go to the next page, I can still click an image. In the bottom of the overlay it says that 2 images are selected, however you only see 1. When I click the image again, it only selects that image. I've made a screen recording of it. I'll see if I can update the patch for Drupal 9.4
Comment #70
xjmThis issue has been closed as a duplicate. Comments on it aren't helpful. #3059955: It is possible to overflow the number of items allowed in Media Library is the issue where we are currently fixing this. We also don't need a bunch of unecessary rerolls and repeated manual testing of the same patch on that issue, either.) Thanks! I am locking this issue to further comments.
Comment #71
xjm