Needs work
Project:
Drupal core
Version:
main
Component:
media system
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
10 Feb 2023 at 19:17 UTC
Updated:
2 Sep 2026 at 17:48 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
ion.macaria commentedTested patch on Drupal 9.5.x and Drupal 10.1.x
Comment #3
ion.macaria commentedComment #4
ion.macaria commentedComment #5
cilefen commentedWe require some steps to reproduce. A bug like this would show in automated tests, so it must be an untested feature.
Comment #6
bhogue commentedI get this error, but only after I've updated my site and modules from 9.4.x to 9.5.x.
If I go to a node edit page for a node that has a Media entity and click "Add media," nothing happens. No error is presented, even though I have seen Ajax errors generate error messages in the admin UI. An Ajax error does show up in the console, but it's very unhelpful and simply points to line 448 of ajax.js, which is inside this function:
Drupal.Ajax.prototype.error = function (xmlhttprequest, uri, customMessage) {Then, in the dblog, the error is as the original poster said:
I have tried uninstalling several Media-related modules like media_library_edit and dropzonejs, and I also toggled off JS aggregation, none of which has helped.
The patch above does not alleviate the problem either, unfortunately.
Comment #7
bhogue commentedAt least in my case, I was getting the error because I applied an unrelated patch (https://www.drupal.org/project/drupal/issues/2784233) to add some view functionality and then removed it, but some of the patched config was left behind.
Comment #8
pacproduct commentedHi everyone :)
I'd like to add my experience on this matter, for info.
I was facing the initial issue as described by @ion.macaria, (with Drupal 10.0.9):
TypeError: array_filter(): Argument #1 ($array) must be of type array, null given in array_filter() (line 134 of [...]/core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php).I was facing the issue when trying to add a custom block containing a media field: When clicking button "Insert selected", nothing was happening on screen, an HTTP error 500 was thrown in the console and the message above was saved in the watchdog.
Patch #2 worked around the error message, but did not solve the behavior in my case, no media was attached and the following error was displayed on screen: "No items selected."
More generally in fact, trying to add images via the dedicated page (/media/add/image) didn't work for me either, as submitting the form was raising an error message: "Image field is required." although I had selected a file. And then I noticed that I also had 413 HTTP errors just after selecting the picture file to upload:
Turns out the issue was with NGinx which wasn't configured to accept large requests, and I was trying to upload pictures larger than 2MB.
Maybe we could argue that
Mediashould be able to handle such errors more gracefully, but (in my case at least) the issue was not directly related to Drupal in the end. YMMV.Reconfiguring Nginx to accept larger requests solved the problem for me.
Comment #9
jurgenhaasWe're facing the same problem every now and then, but we haven't ever been able to reproduce this. The proposed patch solved the issue, however it could be simplified by providing an empty array as the default value to
getValue:In terms of reproducing this, here is a stack trace that we captured when it happened last time:
Other than that, this could be RTBC from my POV.
Comment #10
thidd commentedIn my case, there is some issue with public folder permission.
I tried to upload a new media in media adding form page, then saw the real problem -
the upload directory public://2023-07 for the file field field_media_file could not be created or is not accessible....Then grant 755 for sites//files will fix.
Comment #11
chris matthews commentedClear steps to reproduce have not been provided so closing this issue accordingly. If steps to reproduce can be provided then please feel free to reopen.
Comment #12
jurgenhaasI'm uncertain about closing it for the given reason. A couple of people reported the issue, and all of us tried without luck to make it reproducible. However, providing a default for a method call like
$form_state->getValue($this->options['id'], [])should be "best practice" in the first place, and has no chance of breaking anything, especially in the given situation where the return value is used forarray_filter()which absolutely needs an array as input.Comment #13
chris matthews commentedComment #14
cedeweyI'm getting this error which I think is related but I could be wrong,
ValueError: array_rand(): Argument #1 ($array) cannot be empty in array_rand() (line 346 of core/modules/image/src/Plugin/Field/FieldType/ImageItem.php).when I do the following:
Comment #15
heikkiy commentedWe also encountered the issue after updating modules in our site.
Here is the stacktrace
Location is an Ajax call to media library: en/media-library?_wrapper_format=drupal_ajax&ajax_form=1&hash=pzpbWl_GZU9CKRNLuuvk-Hd-bX3srQqZfWjcJZjzI9o&media_library_allowed_types%5Bremote_video%5D=remote_video&media_library_opener_id=media_library.opener.field_widget&media_library_opener_parameters%5Bfield_widget_id%5D=field_media_video%3A-field_elements-1-subform-field_elements-0-subform-field_right_column_elements-0-subform&media_library_opener_parameters%5Bentity_type_id%5D=paragraph&media_library_opener_parameters%5Bbundle%5D=video&media_library_opener_parameters%5Bfield_name%5D=field_media_video&media_library_opener_parameters%5Bentity_id%5D=39673&media_library_opener_parameters%5Brevision_id%5D=194833&media_library_remaining=1&media_library_selected_type=remote_video&name=ISS%20support%20process&sort_by=created&views_display_id=widget
Comment #16
poker10 commentedThere is not enough information on what is actually the intention of this issue. Issue summary needs update and the issue title as well.
In #8 it was mentioned, that the patch removed the error, but the behavior was not as intended. So it is possible that using that patch could hide a real problem which has root cause somewhere else.
Comment #17
damienmckennaRan into this on a 10.1.5 site:
URL: /media-library
Query arguments:
_wrapper_format=drupal_ajax
ajax_form=1
ajax_page_state[theme]=gin
ajax_page_state[theme_token]=something
ajax_page_state[libraries]=admin_toolbar/toolbar.tree%2Cadmin_toolbar/toolbar.tree.hoverintent (etc)
media_library_allowed_types[0]=image
media_library_allowed_types[video_remote]=video_remote
media_library_opener_id=media_library.opener.editor
media_library_opener_parameters[filter_format_id]=full_wysiwyg
media_library_remaining=1
media_library_selected_type=image
name=some string
sort_by=created
views_display_id=widget
The referrer on the error report is node/add/story, i.e. a node/add path.
This meant the error happened when someone clicked the Add Media button in the ckeditor 5 toolbar.
I noticed that when the media library is loaded the query arguments are actually like this:
media_library_opener_id=media_library.opener.editor
media_library_allowed_types[0]=image
media_library_allowed_types[video_remote]=video_remote
media_library_selected_type=image
media_library_remaining=1
media_library_opener_parameters[filter_format_id]=full_wysiwyg
hash=something
_wrapper_format=drupal_modal
Does the query arguments from the failed path mean the visitor submitted the filter to narrow the results or that they had just uploaded an image and the media library had reloaded to show the new image selected in the list? But if so, why would the referrer be node/add/story instead of /media-library?
Comment #18
s_leu commentedI ran into this too when I had a media bundle that didn't have the media_library view mode configured and enabled. After enabling it on the affected media bundle it works for me. Not 100% that's what was causing it though but maybe worth a try?
Comment #19
s_leu commentedI just noticed that this problem doesn't arise any more after i removed the filters for published media in the media library view.
Comment #20
s_leu commentedAlso as mentioned in #8, the patch from #2 seems not to be a complete solution, as submitting the form with the patch applied gives that error "No items selected".
Comment #21
ion.macaria commentedSorry for confusion, I fished this error from graylog where we are putting ours logs.
After some research I find the way to reproduce it.
So
$selected = array_filter($form_state->getValue($this->options['id'], []));should fix it.Comment #22
ion.macaria commentedUpdated patch:
Comment #23
ion.macaria commentedComment #24
ion.macaria commentedComment #25
ion.macaria commentedComment #26
smustgrave commentedHate to do it, but do we know why that value could be empty? Want to make sure we aren't masking a larger issue.
May need a test case also.
Comment #27
ion.macaria commented@smustgrave value is empty because code tries to get value which is not already found in form after it items list was ajaxfy refreshed.
I don't see the reason to create test for this because by default is clear if you are getting NULL or empty results it means that no item was selected. We just need to provide default value instead NULL to pass array_filter condition.
Comment #28
smustgrave commentedSo since we know the Id isn't there shouldn't we avoid trying to get the value?
Comment #29
hoaqtcom commentedThis is another patch for the the same error on the core Views module. Sorry, I don't see another thread for Views so I post it here.
Comment #30
hoaqtcom commentedComment #31
smustgrave commentedHiding #29 as that seems to be a separate issue that will need to be investigated.
#22 I tested following the steps in the issue summary but still get an error. That said think we will need test coverage for this.
Comment #32
mohit_aghera commentedI am able to reproduce the issue by following exact steps in the issue summary.
Uploaded the patch for the same.
Not creating MR for now until we have conclusion about the solution.
Hence, keeping issue in needs work and triggering test manually to see how many failures we end up with.
Comment #33
mohit_aghera commentedThere is a typo in patch #32
Resolving that error and few tests are passing on local
Comment #34
larowlanWhat is the impact of multiple values here, we're joining them with nothing, is that correct?
Comment #35
mohit_aghera commentedWe are joining with comma here, default argument value in the join() method is comma.
As soon as we keep on selecting multiple media items, all the media item ids are joined with comma and stored in input element field called `current_selection`
`current_selection` hidden input field is retaining all the correct values despite any ajax call.
So in our fix, I am reading the value from the `current_selection` input field and storing values in another hidden input field called `media-library-modal-selection`.
Comment #36
larowlanThanks, I didn't realize comma was the default! Always learn something in the issue queue 🥰
Comment #37
damienmckennaIt turned out that my site was broken because we'd disabled AJAX on the media_library view, so I opened a separate issue about this: #3404365: Media browser breaks if AJAX disabled on views.view.media_library.yml
Comment #40
saidatomComment #41
smustgrave commentedHave not tested but issue summary appears to need some work
Previously tagged for tests which believe are still needed.
Comment #42
damienmckennaThe merge request's FunctionalJavascript failures all seem to be this error:
Comment #43
alorencThanks, the latest patch works fine for me.
I can see an error message in case I do not pick any media
Comment #44
alvimurtaza commentedThanks Mohit, I was getting the same error and patch #33 worked fine here and I'm not getting the error anymore.
Comment #45
adamzimmermann commentedThe MR in its current state fixed the issue I was having. However, I wasn't seeing this in relation to CKEditor. I was having this issue after enabling the core Workspaces module and some associated modules. I can provide more detail if that would be helpful.
Comment #47
malcomio commentedSeems like there's been some progress since this was last in "Needs review" status
Comment #48
smustgrave commentedThis one still appears to need an IS update please.
Comment #50
herved commentedCI is red because of the change from #3567770: Media Library message persists after deselecting items to fix overflow, which looks like a defect there.
It put the message clearing inside checkEnabled(), and that function runs every time the behaviors run, not only when the selection changes.
The "No items selected." message this MR adds goes into the same container, #media-library-messages.
Nothing is selected at that point, so the selection is within the limit and the clearing empties the container on the next behavior run, right after the message arrives. testEmptyWidgetOverflow fails on that. This MR cannot go green without changing that code.
Comment #51
herved commentedThis issue is now fixed by #3303889: Media Library: Clicking "Insert selected" with no media selected triggers fatal error latest MR version.
It moves the validation to the hidden selection field, which removes the array_filter() call that throws here.
I ran the steps of this report on that branch: select an item, filter it out of the view, then press Insert selected. The dialog now shows "No items selected." instead of the PHP error, which is what this issue asks for.
So I suggest closing this as a duplicate of #3303889: Media Library: Clicking "Insert selected" with no media selected triggers fatal error.