Problem/Motivation

I am running into an issue when adding media into a field that allows only one item. I am adding a block in Layout Builder. The block I am adding has a media field with a limit of one item. However, I can check more than one image on that field by clicking on the image even though the input field is disabled after one image is selected. And I can then insert those two images into the block but I can't save it and I can't remove the images so I have to start over.

I found that the issue is in media_library.click_to_select.js line 14.

$input.prop('checked', !$input.prop('checked')).trigger('change');

Steps to reproduce

Note: Make sure that one of the content types has layout builder enabled.

  • Create a "Test" block with a media field
  • Set "Allowed number of values" to 1
  • Set the "Media type" to Image and save
  • Create a new node of the content type that has Layout Builder enabled
  • Click "Layout" to add the new block
  • Click "Add Media" and select more than one image

You'll notice that you can select more than one image and you can click Insert Selected.

Proposed resolution

Change media_library.click_to_select.js line 14 to:

if (!$input.prop('disabled')) {
  $input.prop('checked', !$input.prop('checked')).trigger('change');
}

I also noticed that there is a es6.js version of the file and a similar change would apply there but I am not sure what the criteria is that would allow that file to be used.

Issue fork drupal-3206944

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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

bbytyqi created an issue. See original summary.

dstorozhuk’s picture

Status: Active » Closed (duplicate)
Related issues: +#3059955: It is possible to overflow the number of items allowed in Media Library
JurgenR’s picture

Status: Closed (duplicate) » Active
FileSize
1.08 KB

Reopening the ticket as the mentioned duplicate is related to the file module.
This ticket is related to the media_library module, which causes the bug in the layout builder.
Sure the problem is similar but the solution seems to be module specific.

I've added a patch based on the proposal of bbytyqi.
Not sure how to test and/or change the es6 file. Help is appreciated.

ironsizide’s picture

Reporting that the patch works with Drupal 9.5.10 for me.

Anybody’s picture

Version: 9.1.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.

dbrowncode made their first commit to this issue’s fork.

dbrowncode’s picture

Status: Active » Needs review
Issue tags: -JavaScript +JavaScript

Pushed MR in #9 that works on D10.2.5 for me.
Similar to the previous one, but changed the selector to exclude disabled inputs instead of adding the check after.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

This may have been fixed already in 11.x

If not will need a test case