Problem/Motivation

When you click on the "Insert selected" button on the media library widget in a content type without uploading or selecting any elements on the library the modal closes without warning or showing any errors, but when you go back to "Add media" again, the modal opens and then you see the "No items selected" error. This feels off. If you are going to show an error message it should be displayed in the message area of the content type form right after closing the modal or prevent the modal from closing and show the error right there, you should not have to reopen the modal to see the error message.

Steps to reproduce

  1. Install Drupal
  2. Activate core modules Media and Media Library
  3. Create custom content type and add a media field with the media library widget
  4. Go to add a content of the custom content type
  5. Click on the "Add media" button
  6. Click on the "Insert selected" button without uploading or selecting any element on the library

You will notice that the modal closes and nothing else happens, but when you go to "Add media" again you'll see the error message when the modal opens.

Proposed resolution

Make the 'Insert selected' of zero items behave similar to selecting too many items. Ensure validation messages are only shown as a result of clicking 'Insert selected'.

Remaining tasks

Review

User interface changes

The error message is only shown when the 'Insert selected' button is clicked, not when the dialog is closed without clicking it (and then subsequently reopening the dialog).

Introduced terminology

None

API changes

N/A

Data model changes

N/A

Release notes snippet

N/A

Issue fork drupal-3277735

Command icon 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

ramonma1989 created an issue. See original summary.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

scott_euser’s picture

I wonder if the modal should actually have a Cancel button to avoid this.

I also noticed that if you do this:

  1. Create node bundle with required media field, e.g. 'Featured image'

Then when inserting new:

  1. Open media library for 'Featured image'
  2. Change mind and decide to do later, so click 'Insert selected' to close
  3. Add title, body, etc
  4. Select a 'Featured image' this time from Media Library
  5. Save node

Then message is shown 'No items selected' (as well as successfully creating the Node) as it seems the media library 'cancel' did not trigger a form refresh, so the 'No items selected' stayed in the messenger queue even though a Media Library selection was actually made in step 4 above.

So I think possible solutions here:

  1. Ensure that an effective 'Cancel' still triggers form refresh so 'No items selected' message is shown at appropriate time
  2. Avoid showing 'No items selected' validation until full node save; ie, skip validation in the subform and only apply at form level
  3. Provide a Cancel button

(on decision, update issue summary with standard template + proposed solution)

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

anmolgoyal74’s picture

Issue tags: +Needs UX review

We are also facing the same issue.
We can disable the insert selected button until the media is selected.
The user can use the cross "X" button to close the modal if needed,

quietone’s picture

Status: Active » Postponed (maintainer needs more info)
Issue tags: -Needs UX review +Needs usability review, +Bug Smash Initiative

There isn't a "Needs UX review" tag, so changing tag.

This was reported on Drupal 9, which is no longer supported. Is this happening on currently supported versions of Drupal?

scott_euser’s picture

Title: Media library field modal should not close after clicking the "Insert selected" buttom if there is no image selected » Media library field modal should not close after clicking the "Insert selected" button if there is no image selected
Issue summary: View changes
Status: Postponed (maintainer needs more info) » Active

Yes this is still an issue; insert selected can still be clicked on main branch without any selections (and it still also causes what I noted in #5). I removed the specific reference to Drupal 9

scott_euser’s picture

Status: Active » Needs review
StatusFileSize
new121.51 KB

The inline validation now shows up in the exact same was as selecting too many items (e.g. if 2 are allowed and you selected 3):

Screenshot of no items selected.

I think it makes sense for the message to be "No items selected. Consider closing the dialog instead." instead of just "No items selected." given that the intent of the user could be to close the dialog if they are used to it behaving like that despite the button label. No harm perhaps?

The validate method is called via Views with a method_exists() wrapper on all field handlers, so removing it prevents the unconditional call, and moves it to a conditional call to match the too many items selected behaviour; ie only triggered when clicking 'Insert selected' but not triggered when clicking the close dialog X button.

scott_euser’s picture

Issue summary: View changes

Tests now passing (including new coverage) + updated the description to use the standard issue summary template.

rkoller’s picture

StatusFileSize
new75.31 KB
new88.7 KB
new420.47 KB

Usability review

We discussed this issue at #3589097: Drupal Usability Meeting 2026-05-15. The link to the recording: https://www.youtube.com/watch?v=INHnpnxH7CI&t=1391s . The attendees at the usability meeting were @benjifisher, @rkoller, @simohell, @the_g_bomb, and @worldlinemine.

Thanks for working on the issue @scott_euser. In general the group was in line with the proposed resolution to not close the dialog modal and in consequence to not change the context in case no image was selected when pressing/clicking the Insert selected button. It has to be noted that we haven't had enough time left in yesterday's meeting, so we will most likely revisit and finish the review next week if time permits. For now a brief summary of our explorations about the current state of MR15604:

  • Usually system messages are the full width of a page or dialog, but in this case the error message never spans across the full width of the dialog modal. With no media items available in the media item grid underneath the error message is inline with the No media available. message from the media library view
    add or select media dialog modal with a error message that no items are selected inline with a no media available string at the bottom of the dialog modal viewport
    while with media items available, the error message only spans across the left half of the dialog modal
    add or select media dialog modal with a error message that no items are selected underneath one media item available to select at the bottom of the dialog modal viewport
  • The CTA the error message provides with No items selected. Consider closing the dialog instead. about how to resolve the error is sort of misleading. The error does not block the interaction within the dialog that would require the closing of the dialog modal and or the reload of the page - the user has simply to select a certain number of media items depending on the field settings and/or upload media items in the first place.
  • Probably out of scope for this issue, after pressing the Insert selected button the focus jumps to the filter field, no matter if there are media items to filter or if no media items are available and the list is empty. Problem with that, the filter section is the only part of the form that can't directly change what is getting selected.
  • Definitely out of scope for this issue, when the error message shows up, it is not getting announced in the aural interface, and therefore this fact could go unnoticed for screenreader users (error_message.mp4 - recorded on macOS with Voiceover enabled) - it is rather a matter for Core in general, and already covered in #2942404: System messages should be accessible

The sole recommendation the group agreed on during yesterday's meeting, in case the people working on the issue don't consider it already out of scope, is moving the element wrapping the error message one level up in the DOM so it is placed right before in order, on the same level, as the form element. The visual position of the error message wouldn't change, it would be still positioned between the grid/table toggle switch and the "no media available" message or the block of media item cards if media items are available - the only difference, the error message would be now spanning across the entire dialog modal and would also not be inline with the "no media available" message.

In regard to the element getting into focus after pressing the Insert selected button, one idea that was raised, instead of getting the filter field into focus, put the focus onto the Choose file button. But that most likely would be the proposed resolution for a potential follow up? Or would be extending the scope for this issue, and fixing the focus problem right away, be considered reasonable to the folks working on this issue?

In regard to the micro copy used on the error message, we haven't had the time left for word smithing yesterday. We will get to that as soon as we will continue our discussion.

I'll keep the "Needs usability review" tag on the issue for now, until we've revisited it for a second time and finish our discussion.

If you want more feedback from the usability team, a good way to reach out is in the #ux channel in Slack.

scott_euser’s picture

StatusFileSize
new109.63 KB

Thanks for the feedback! Moving it up a level would not work as it would change its location to above the choose file, so instead I added css so the flexbox of the messages list spans the full width:

Screenshot of media library modal with messages list

I changed the message to "No items selected." removing the bit about closing the dialog.

I am not sure about increasing the scope to cover focus changes either, so I left that untouched.

Ready for review again I think.

smustgrave’s picture

@rkoller if you're good with the changes we can proceed to review

rkoller’s picture

Issue tags: -Needs usability review

Usability review

We discussed this issue for a second time at #3590325: Drupal Usability Meeting 2026-05-22. The link to the recording: https://www.youtube.com/watch?v=5tmSB4zZBfo&t=12s . The attendees at the usability meeting were @benjifisher, @rkoller, and @simohell.

Thanks @scott_euser for applying our suggestions from last week's meeting! The overall behavior of the error message looks good now. fab! So we've focused our discussions during the second meeting onto the micro copy of the error message.

The proposal to go with just No items selected, dropping the second sentence with Consider closing the dialog instead, looked like an interesting approach at first. We've asked ourselves, what are the scenarios a user could run into that kind of error message?

  1. Someone doubled clicked accidentally a media item's checkbox, selected and directly unselected a media item, and then proceed by pressing Insert selected
  2. Someone wanted to exit the dialog modal for what ever reason and didn't know that you can either press the ESC key or click the X button in the upper right corner of the dialog modal. Due to the lack of a cancel button they've simply pressed the Insert selected button instead

The group had a consensus, in case scenario 1 applies people are in control and know the context they are currently in - they have the situational awareness. In that case the No items selected could be considered sufficient and by adding any additional instructions how to add media items would make the error message overly verbose. The only problem No items selected does not cover for scenario 2. We would suggest re-adding a second sentence. Instead of proposing to close the dialog with Consider closing the dialog instead), it might be the better choice to slightly rephrase the sentence by outlining scenario 2 and to provide some sort of guidance to the user how to close the dialog modal with If you want to close the dialog, use the ESC key or the X icon.. So our suggestion for the error message would be:

No items selected. If you want to close the dialog, use the ESC key or the X icon.

If applicable, instead of simply write X like in the aforementioned proposal, it might be considered using the actual X icon used on dialog modal inline in the error message - to illustrate the icon the user has to press/click (and for screenreader users use the Close title, same as on the icon on the dialog modal).

If you want more feedback from the usability team, a good way to reach out is in the #ux channel in Slack.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new840 bytes

The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

smustgrave’s picture

Status: Needs work » Needs review
needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new840 bytes

The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

smustgrave’s picture

Status: Needs work » Needs review
needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new91 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.