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

CommentFileSizeAuthor
#69 Screen Recording 2022-07-08 at 14.03.13.mov41.61 MBfabianderijk
#68 3092536-68.patch1.75 KBki
#66 3092536-66.patch2.19 KBNitinLama
#65 3092536-65.patch922 byteski
#62 3092536-62.patch1.79 KBki
#51 Screen Shot 2020-08-13 at 6.13.44 AM.png426.07 KBtanubansal
#51 Screen Shot 2020-08-13 at 6.12.51 AM.png408.18 KBtanubansal
#50 interdiff-48-50.txt1.94 KBhardik_patel_12
#50 3092536-50.patch2.11 KBhardik_patel_12
#48 interdiff-42-48.txt1.87 KBhardik_patel_12
#48 3092536-48.patch2.11 KBhardik_patel_12
#46 Screen Shot 2020-07-14 at 2.22.25 PM.png335.69 KBtanubansal
#45 image.gif11.8 MBambuj_gupta
#42 3092536-42.patch1.81 KBhimanshu_sindhwani
#41 Screenshot 2020-07-06 at 1.58.14 PM.png107.08 KBhimanshu_sindhwani
#38 image_upload.png759.34 KBambuj_gupta
#31 upload_image_enable.png787.42 KBambuj_gupta
#28 Before.png724.83 KBpriyanka.sahni
#28 After.png642.38 KBpriyanka.sahni
#28 Before.gif2.08 MBpriyanka.sahni
#28 After.gif3.52 MBpriyanka.sahni
#26 3092536-26.patch2.11 KBsnehalgaikwad
#24 selectanother1.png403.82 KBsnehalgaikwad
#24 select1.png380.54 KBsnehalgaikwad
#21 afterPatchManual.JPG88.7 KBpankaj.singh
#21 afterPatchDefault.JPG87.67 KBpankaj.singh
#21 beforePacthManual.JPG100.6 KBpankaj.singh
#21 beforePatchDefault.JPG85.65 KBpankaj.singh
#18 interdiff_13_0-18.txt2.23 KBhimanshu_sindhwani
#18 3092536-18.patch2.1 KBhimanshu_sindhwani
#15 interdiff-9-13.txt1.66 KBhardik_patel_12
#15 3092536-13.patch2.07 KBhardik_patel_12
#13 interdiff_9-13.txt2.32 KBhimanshu_sindhwani
#13 3092536-13.patch2.1 KBhimanshu_sindhwani
#9 interdiff_6-9.txt1.35 KBhimanshu_sindhwani
#9 3092536-9.patch1.93 KBhimanshu_sindhwani
#6 3092536-6.patch1.19 KBhimanshu_sindhwani
bug-media2.mov7.4 MBkaduk

Comments

kaduk created an issue. See original summary.

kaduk’s picture

Issue summary: View changes

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.9 was released on November 6 and is the final full bugfix release for the Drupal 8.7.x series. Drupal 8.7.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.8.0 on December 4, 2019. (Drupal 8.8.0-beta1 is available for testing.)

Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

andreasderijcke’s picture

I can confirm the issue, with slightly difference from the steps above.

Follow all steps above until:

3. Create a new basic page.
- When filling in the media image field:

Then:

  • Upload a new file. It should end up as selected.
  • Upload a second file. It should also end up as selected.
  • Insert the images
  • The field will report '-1 media-items left.'

This was using Seven as backend theme.

Logs show:

Notice: Undefined index: removed_item_id in Drupal\media_library\Plugin\Field\FieldWidget\MediaLibraryWidget::updateWidget() (line 671 of /var/www/html/web/core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php)

#0 /var/www/html/web/core/includes/bootstrap.inc(600): _drupal_error_handler_real(8, 'Undefined index...', '/var/www/html/w...', 671, Array)
#1 /var/www/html/web/core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php(671): _drupal_error_handler(8, 'Undefined index...', '/var/www/html/w...', 671, Array)
#2 [internal function]: Drupal\media_library\Plugin\Field\FieldWidget\MediaLibraryWidget::updateWidget(Array, Object(Drupal\Core\Form\FormState), Object(Symfony\Component\HttpFoundation\Request))
#3 /var/www/html/web/core/lib/Drupal/Core/Form/FormAjaxResponseBuilder.php(69): call_user_func_array(Array, Array)
#4 /var/www/html/web/core/lib/Drupal/Core/Form/EventSubscriber/FormAjaxSubscriber.php(109): Drupal\Core\Form\FormAjaxResponseBuilder->buildResponse(Object(Symfony\Component\HttpFoundation\Request), Array, Object(Drupal\Core\Form\FormState), Array)
#5 [internal function]: Drupal\Core\Form\EventSubscriber\FormAjaxSubscriber->onException(Object(Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent), 'kernel.exceptio...', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
#6 /var/www/html/web/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(111): call_user_func(Array, Object(Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent), 'kernel.exceptio...', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
#7 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(227): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.exceptio...', Object(Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent))
#8 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(79): Symfony\Component\HttpKernel\HttpKernel->handleException(Object(Drupal\Core\Form\FormAjaxException), Object(Symfony\Component\HttpFoundation\Request), 1)
#9 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#10 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#11 /var/www/html/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#12 /var/www/html/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#13 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#14 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#15 /var/www/html/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#16 /var/www/html/web/core/lib/Drupal/Core/DrupalKernel.php(708): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#17 /var/www/html/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#18 {main}
himanshu_sindhwani’s picture

Assigned: Unassigned » himanshu_sindhwani

I will try to reproduce this issue and find a suitable fix. Therefore assigning to myself.

himanshu_sindhwani’s picture

Status: Active » Needs review
StatusFileSize
new1.19 KB

Here is a patch that fixes the issue.

himanshu_sindhwani’s picture

Assigned: himanshu_sindhwani » Unassigned

Status: Needs review » Needs work

The last submitted patch, 6: 3092536-6.patch, failed testing. View results

himanshu_sindhwani’s picture

Priority: Normal » Major
Status: Needs work » Needs review
StatusFileSize
new1.93 KB
new1.35 KB

Uploading the patch with a fix.
Also updating the priority of the bug.

The last submitted patch, 6: 3092536-6.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 9: 3092536-9.patch, failed testing. View results

hardik_patel_12’s picture

Assigned: Unassigned » hardik_patel_12

working on this issue

himanshu_sindhwani’s picture

StatusFileSize
new2.1 KB
new2.32 KB

Sorry @hardik_patel_12, I already did some workaround from the patch in #9.

himanshu_sindhwani’s picture

Status: Needs work » Needs review
hardik_patel_12’s picture

Assigned: hardik_patel_12 » Unassigned
StatusFileSize
new2.07 KB
new1.66 KB

@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

himanshu_sindhwani’s picture

I 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.

The last submitted patch, 13: 3092536-13.patch, failed testing. View results

himanshu_sindhwani’s picture

StatusFileSize
new2.1 KB
new2.23 KB

Fixing 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 = [];.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

pankaj.singh’s picture

Assigned: Unassigned » pankaj.singh
pankaj.singh’s picture

StatusFileSize
new85.65 KB
new100.6 KB
new87.67 KB
new88.7 KB

Tested 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.

pankaj.singh’s picture

Assigned: pankaj.singh » Unassigned
snehalgaikwad’s picture

Assigned: Unassigned » snehalgaikwad
snehalgaikwad’s picture

Assigned: snehalgaikwad » Unassigned
Status: Needs review » Reviewed & tested by the community
StatusFileSize
new380.54 KB
new403.82 KB

Verified 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.

snehalgaikwad’s picture

Assigned: Unassigned » snehalgaikwad
Status: Reviewed & tested by the community » Needs work

Re-rolling patch for 9.1.x.

snehalgaikwad’s picture

Version: 8.9.x-dev » 9.1.x-dev
Assigned: snehalgaikwad » Unassigned
Status: Needs work » Needs review
StatusFileSize
new2.11 KB

Re-rolled patch for Drupal 9.1.x

priyanka.sahni’s picture

Assigned: Unassigned » priyanka.sahni
priyanka.sahni’s picture

StatusFileSize
new3.52 MB
new2.08 MB
new642.38 KB
new724.83 KB

Verified 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 -
BeforePatch

BeforePatch

After Patch -
After Patch

After Patch

priyanka.sahni’s picture

Assigned: priyanka.sahni » Unassigned
ambuj_gupta’s picture

Assigned: Unassigned » ambuj_gupta
ambuj_gupta’s picture

Assigned: ambuj_gupta » Unassigned
Status: Needs review » Needs work
StatusFileSize
new787.42 KB

Testing 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

ambuj_gupta’s picture

himanshu_sindhwani’s picture

@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.

ambuj_gupta’s picture

@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.

hardik_patel_12’s picture

@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

hardik_patel_12’s picture

Status: Needs work » Needs review
ambuj_gupta’s picture

Assigned: Unassigned » ambuj_gupta
ambuj_gupta’s picture

Assigned: ambuj_gupta » Unassigned
Status: Needs review » Reviewed & tested by the community
StatusFileSize
new759.34 KB

@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

himanshu_sindhwani’s picture

Issue tags: +JavaScript

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 26: 3092536-26.patch, failed testing. View results

himanshu_sindhwani’s picture

StatusFileSize
new107.08 KB

It 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.

coding standards

himanshu_sindhwani’s picture

StatusFileSize
new1.81 KB

Correcting the coding standard issues here.

himanshu_sindhwani’s picture

Status: Needs work » Needs review
ambuj_gupta’s picture

Assigned: Unassigned » ambuj_gupta
ambuj_gupta’s picture

Assigned: ambuj_gupta » Unassigned
Status: Needs review » Needs work
StatusFileSize
new11.8 MB

Tested 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.

tanubansal’s picture

StatusFileSize
new335.69 KB

Tested 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

hardik_patel_12’s picture

The 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.

/var/lib/drupalci/workspace/jenkins-drupal_patches-52684/source/core/modules/media_library/js/media_library.ui.es6.js
line 0 File ignored because of a matching ignore pattern. Use "--no-ignore" to override.
/var/lib/drupalci/workspace/jenkins-drupal_patches-52684/source/core/modules/media_library/js/media_library.ui.js
0 File ignored because of a matching ignore pattern. Use "--no-ignore" to override.

hardik_patel_12’s picture

Status: Needs work » Needs review
StatusFileSize
new2.11 KB
new1.87 KB

Kindly review a new patch , solving coding standards error as well.

Status: Needs review » Needs work

The last submitted patch, 48: 3092536-48.patch, failed testing. View results

hardik_patel_12’s picture

Status: Needs work » Needs review
StatusFileSize
new2.11 KB
new1.94 KB

Review a new patch.

tanubansal’s picture

After adding #50, code changes are clearly visible.
This can be moved to RTBC

phenaproxima’s picture

himanshu_sindhwani’s picture

@phenaproxima, you have tagged this same issue here in #52

abhisekmazumdar’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me . Followed the above testing steps.
Moving this to RTBC.

phenaproxima’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

This needs automated test coverage before it can be committed.

phenaproxima’s picture

Transferring credit from #3160240: User can select multiple media entities but only one is allowed, which I have closed as a duplicate of this one.

phenaproxima’s picture

phenaproxima’s picture

Status: Needs work » Closed (duplicate)
Related issues: +#3059955: It is possible to overflow the number of items allowed in Media Library

It 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.

ki’s picture

StatusFileSize
new1.79 KB

Cleaned up the #50 patch to work with Drupal 9.2

yahyaalhamad’s picture

Applied 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.

dariemlazaro’s picture

The patch in the comment #62 works perfect in Drupal 9.2.8. Thanks.

ki’s picture

StatusFileSize
new922 bytes

Cleaned up the #62 patch to work with Drupal 9.3.3

NitinLama’s picture

StatusFileSize
new2.19 KB

Cleaned up the #65 patch to work with Drupal 9.3.x

dariemlazaro’s picture

The 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.

ki’s picture

StatusFileSize
new1.75 KB

Cleaned up the patch to work with Drupal 9.4.0

fabianderijk’s picture

StatusFileSize
new41.61 MB

#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

xjm’s picture

This 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.

xjm’s picture