Problem/Motivation
This error occurs when we click on 'Insert Selected' button from the media library without selecting or uploading a media.
Steps to reproduce
- Go to any add/edit node page with body field.
- Click on the media library button from the CKeditor
- Click on the 'Insert Media' button from the image media type tab
It won't show any error in the frontend but logs an error message in watchdog and in browser console.
We see the 'No items selected' message only after switching media tabs.
Proposed resolution
- Prepare the form validate method to the case when there aren't any items selected.
- Display messages if the form has any error.
Remaining tasks
- Patch ✅
- Test coverage
- Approval of core maintainer(s).
User interface changes
When the Media Library form is submitted without any selected media items, an error message appears on the form.
API changes
Nothing.
Data model changes
Nothing.
Release notes snippet
Not needed as this is a bug?..
| Comment | File | Size | Author |
|---|---|---|---|
| #57 | video-51474380-4436431590ff6268b0867e91025064f2.gif | 2.83 MB | arunkumark |
| #46 | 3303889-45-with-test.patch | 7.72 KB | zeeshan_khan |
| #44 | core-3303889-36.patch | 1.49 KB | bingol@ciandt.com |
| #41 | error-fix-viewsFormValidate.png | 867.96 KB | samitk |
| #34 | core-3303889-media-library-widget-error-on-no-selection-34.patch | 1.48 KB | ion.macaria |
Issue fork drupal-3303889
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
srijeeta commentedComment #3
srijeeta commentedComment #4
srijeeta commentedComment #5
cilefen commentedIs there additional setup or can this be reproduced by installing Drupal?
Please post stack traces as code rather than as an image.
Comment #6
srijeeta commentedNo additional setup, just installing Drupal will be fine.
Post stack trace:
Error: Call to a member function uuid() on null in Drupal\media_library\MediaLibraryEditorOpener->getSelectionResponse() (line 72 of /app/docroot/core/modules/media_library/src/MediaLibraryEditorOpener.php)
#0 /app/docroot/core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php(140): Drupal\media_library\MediaLibraryEditorOpener->getSelectionResponse(Object(Drupal\media_library\MediaLibraryState), Array)
#1 [internal function]: Drupal\media_library\Plugin\views\field\MediaLibrarySelectForm::updateWidget(Array, Object(Drupal\Core\Form\FormState), Object(Symfony\Component\HttpFoundation\Request))
#2 /app/docroot/core/lib/Drupal/Core/Form/FormAjaxResponseBuilder.php(69): call_user_func_array(Array, Array)
#3 /app/docroot/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)
#4 [internal function]: Drupal\Core\Form\EventSubscriber\FormAjaxSubscriber->onException(Object(Symfony\Component\HttpKernel\Event\ExceptionEvent), 'kernel.exceptio...', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
#5 /app/docroot/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(142): call_user_func(Array, Object(Symfony\Component\HttpKernel\Event\ExceptionEvent), 'kernel.exceptio...', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
#6 /app/vendor/symfony/http-kernel/HttpKernel.php(219): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object(Symfony\Component\HttpKernel\Event\ExceptionEvent), 'kernel.exceptio...')
#7 /app/vendor/symfony/http-kernel/HttpKernel.php(91): Symfony\Component\HttpKernel\HttpKernel->handleThrowable(Object(Drupal\Core\Form\FormAjaxException), Object(Symfony\Component\HttpFoundation\Request), 1)
#8 /app/docroot/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#9 /app/docroot/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#10 /app/docroot/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#11 /app/docroot/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#12 /app/docroot/modules/contrib/shield/src/ShieldMiddleware.php(265): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#13 /app/docroot/modules/contrib/shield/src/ShieldMiddleware.php(221): Drupal\shield\ShieldMiddleware->bypass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#14 /app/docroot/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\shield\ShieldMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#15 /app/docroot/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#16 /app/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#17 /app/docroot/core/lib/Drupal/Core/DrupalKernel.php(709): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#18 /app/docroot/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#19 {main}
Comment #7
jaykumar95this error also occurred in 9.4.x and 10.0.x
Comment #8
aarti zikre commentednot able to produce the issue.
Testing steps:
1. Created new instance of drupal 9.5.x
2. enable media and media library module
3. configure it in ckeditor basic html /admin/config/content/formats.
4. created new basic page.
5. click on Insert media from library.
6. choose audio tab and inserted one audio.
able to save and see the audio on front end without any error while save and in WD.
performed this same for image too.
Attaching SS for reference.
Comment #9
jaykumar95we need to make some changes on below files.
1.
web\core\modules\media_library\src\Plugin\views\field\MediaLibrarySelectForm.phpto
as we have not selected any item from the list $this->options['id'] is always be null
2.
web\core\modules\media_library\src\MediaLibraryEditorOpener.phpto
Comment #10
jaykumar95after applying above changes I am getting below warning in response
pointing to this line
$flipped_ids = $ids ? array_flip($ids) : FALSE;Comment #11
jaykumar95have fixed it by adding array_filter on it as there is no item selected from the list.
$flipped_ids = $ids ? array_flip(array_filter($ids)) : FALSE;Comment #12
cilefen commented@ aarti zikre I think the steps to reproduce are different. Can you verify?
Comment #13
jaykumar95@ aarti zikre @cilfen
the 6th step is click on Insert selected button without choosing any items.
Comment #14
jaykumar95#11 throwing error on content view page, need to check other way to fix
Comment #15
jaykumar95found the fix.
we need to check the
$selected_idsin response;change in file
web\core\modules\media_library\src\MediaLibraryEditorOpener.phpwould beComment #16
srijeeta commented@JayKumar95 I checked with your fix mentioned, few changes:
Currently the behaviour is if we click on Insert Selected without choosing any media, the modal closes and then when we open it back again it shows a message 'No items selected'.
Comment #17
jaykumar95yes @srijeeta the class will be added in patch. for t() we can use the StringTranslationTrait.
my query is do we need to show the status message or just silently close the dialog box, same as in views where we click on add field button without selecting any fields?
@srijeeta are you creating patch?
Comment #18
srijeeta commentedNo @Jaykumar95, I didn't create a patch yet.
I'm also thinking the same thing, I feel like there should be a way to display the error message 'No items selected' on the button click.
Comment #19
jaykumar95another workaround is to check the selected ids on
updateWidget()function inweb\core\modules\media_library\src\Plugin\views\field\MediaLibrarySelectForm.php.I have updated code according to #16 & 18 to remove the duplicate status messages and only show once inside the dialog box.
Comment #20
jaykumar95Comment #22
jamesyao commentedHello, I am having the same problem after the site was upgraded from Drupal Core 9.3.6 to 9.47. When the anonymous user tries to insert a media item in CKeditor, the error ((Call to a member function uuid() on null in Drupal\media_library\MediaLibraryEditorOpener)) occurs.
Thanks @Jaykumar95 for providing the patch #20. I applied the patch on the Drupal core 9.4.7 site. As a result, the error does not occur anymore, and the media still is not inserted into the CKeditor for the anonymous user. The the anonymous user role has the administer media permission.
Drupal environment:
Drupal Core 9.4.x
PHP 8.0
Comment #23
smustgrave commentedThis issue is being reviewed by the kind folks in Slack, #need-reveiw-queue. We are working to keep the size of Needs Review queue [2700+ issues] to around 200, following Review a patch or merge require as a guide.
Attempted to replicate on 10.1 but could not.
Tagging for IS update as there is no proposed solution.
Tagging for tests as bug will need a test case that replicates the issue and causes the error.
Also not sure about the fix has it seems to be fixing a symptom vs root cause.
Comment #25
balramchauhan684 commentedComment #26
balramchauhan684 commentedComment #27
gaele commentedComment #28
smustgrave commentedStill needs a test case.
Typically with these type of issues just putting an isset() or empty() check is masking a deeper issue that should be determined.
Comment #29
larskleiner commentedComment #30
larskleiner commentedComment #31
alvarito75 commentedI have the same issue.
This is my setup:
By the way, patch #30 is already included in this Drupal version. And I'm upgrading from CKEditor 4 to CKEditor 5.
Comment #32
xjkwak commentedI was able to reproduce the issue only with Twig Debug enabled.
Comment #33
ion.macaria commentedReroll patch for drupal 10.1.7.
Comment #34
ion.macaria commentedFix missed used statements. Drupal 10.1.7
Comment #35
zeeshan_khan commented@ion.macaria - Can you provide interdiff from #30 as it looks same to me.
Also we still need test case for this as mentioned by @smustgrave in #23
Also we need to update the code as suggested in #28 by @smustgrave I agree that using empty() check in this kind situation can cause deeper problems.
Comment #36
joseph.olstadpatch #34 applies cleanly to Drupal 10.2.3 and fixes the bug.
Needed this after an upgrade from 10.0.11
Comment #37
herved commentedThis seems to relate/overlap with #3340973: Media Library: Argument #1 ($array) must be of type array, null given in array_filter()..
The MR there seems to fix it but I haven't compared the code of both solutions yet.
Comment #38
herved commentedComment #41
samitk commentedHi, I have created the MR, I also added an extra condition in
viewsFormValidatethat fixed following error(Also added the screenshot).Test case is still pending, Post observing
core/modules/ckeditor5/tests/src/FunctionalJavascript/MediaLibraryTest.phpit seems not compatible with Drupal 11 as it still usingcreate blog contentpermission.One related issue is https://www.drupal.org/project/drupal/issues/3351597
Thanks
Samit K.
Comment #42
smustgrave commentedPreviously tagged for tests and issue summary both appear still needed.
Comment #43
mortona2k commentedI ran into this error, but replacing my site config with the default media library view fixed it.
I had changed the view display to use responsive grid.
But that display doesn't support row classes, relating to https://www.drupal.org/project/drupal/issues/2474491
I made a custom display that extends responsive grid to add custom classes back in, and it's working again.
Comment #44
bingol@ciandt.com commentedComment #46
zeeshan_khan commentedThis patch addresses the fatal error "Call to a member function uuid() on null" that occurs when clicking "Insert Selected" in the Media Library without selecting any media items.
Changes
MediaLibrarySelectForm.php
Added early validation in `updateWidget()` to check for empty selection and return a user-friendly error message:
```php
if (empty($selected_ids)) {
$response = new AjaxResponse();
$response->addCommand(new MessageCommand(
(string) \Drupal::translation()->translate('No media items were selected.'),
'#media-library-messages',
['type' => 'error']
));
return $response;
}
```
MediaLibraryEditorOpener.php (NEW - not covered by existing MR)
Added defensive null checks in `getSelectionResponse()` to prevent crashes even if validation is bypassed:
```php
// Return empty response if no media items were selected.
if (empty($selected_ids)) {
return $response;
}
$selected_media = $this->mediaStorage->load(reset($selected_ids));
// Return empty response if the media entity could not be loaded.
if (!$selected_media) {
return $response;
}
```
This defensive fix is important because:
- It prevents crashes if `getSelectionResponse()` is called directly (e.g., by custom code)
- It handles the edge case where a media entity is deleted between selection and submission
- It follows defensive programming best practices
Test Coverage
Added new kernel test `MediaLibraryEditorOpenerTest.php` with three test methods:
- `testGetSelectionResponseWithEmptySelection()` - Verifies empty array doesn't crash
- `testGetSelectionResponseWithInvalidMediaId()` - Verifies non-existent media ID doesn't crash
- `testGetSelectionResponseWithValidSelection()` - Verifies normal operation still works
How to Test Manually
1. Apply the patch
2. Go to any CKEditor field with media embed enabled
3. Click the media button to open Media Library
4. Without selecting any media, click "Insert Selected"
5. **Before patch**: Fatal error
6. **After patch**: Error message "No media items were selected."
Difference from Existing MR (!9413)
The existing MR adds validation in `updateWidget()` but does **not** add defensive checks to `MediaLibraryEditorOpener::getSelectionResponse()`. This patch includes both:
1. Form-level validation (similar to MR approach but simpler)
2. Opener-level defensive checks (not in MR)
The defensive checks in the opener are valuable because they protect against:
- Direct calls to `getSelectionResponse()` that bypass form validation
- Race conditions where media is deleted between selection and submission
- Any future code paths that might call the opener directly
Comment #47
zeeshan_khan commentedComment #48
smustgrave commentedThanks but tests need to be added to the MR to show it's fixing the problem. patches don't run the test suite. Also issue summary still needs to be updated.
Comment #50
huzookaComment #51
huzookaFrom now on, we have a test in MR 9413.
Comment #52
huzookaIssue summary must be better now.
Comment #53
huzookaComment #54
dimilias commentedI tested the test with and without the fix.
Namely:
First one passed
Second one failed.
I did not perform a QA in the code as, from the comments, I see that the tests were missing. RTBC +1.
Comment #55
quietone commentedThere are comments in the MR to address.
The title should be description of the change, so adding tag.
Comment #56
sivaji_ganesh_jojodae commentedComment #57
arunkumarkVerified the patch is working fine in the Drupal 10.x version. Sharing the verification in local.
Still the Comments need to be addressed.