Problem/Motivation

Using the media library widget, if you select an image then remove it, when using the widget again the count remains at 1 even though nothing is selected.

Steps to reproduce

  1. Install Drupal
  2. Enable media library
  3. Create a media image
  4. Add a media image field to the article content type with 1 item allowed
  5. Create an article
  6. Click Add media and select an image
  7. Remove the selected image
  8. Click Add media
  9. Note that the count says 1 of 1 even though nothing is selected
  10. Click Insert selected
  11. An empty selection is added
  12. Save
  13. See error 'No item selected'

After saving empty, and editing again, the count goes back to 0 as in the first screenshot.

Proposed resolution

Check if the currentSelection has a length before update the selectItemsText in the function updateSelectionCount(remaining) in media_library.ui.js file. And provide an empty string by default to the selectItemsText.

Existing function

function updateSelectionCount(remaining) {
        var selectItemsText = remaining < 0 ? Drupal.formatPlural(currentSelection.length, '1 item selected', '@count items selected') : Drupal.formatPlural(remaining, '@selected of @count item selected', '@selected of @count items selected', {
          '@selected': currentSelection.length
        });

        $('.js-media-library-selected-count').html(selectItemsText);
      }

could be

function updateSelectionCount(remaining) {
        var selectItemsText = '';
        if (currentSelection.length > 0) {
          selectItemsText = remaining < 0 ? Drupal.formatPlural(currentSelection.length, '1 item selected', '@count items selected') : Drupal.formatPlural(remaining, '@selected of @count item selected', '@selected of @count items selected', {
            '@selected': currentSelection.length
          });
        }
        $('.js-media-library-selected-count').html(selectItemsText);
      }

Remaining tasks

  • Agreed on the fix
  • Write test for fix
  • Review the latest uploaded patch

User interface changes

Correct selected count media is displayed to end users.

API changes

None

Data model changes

None

Release notes snippet

N/A

CommentFileSizeAuthor
#61 3167799-61.patch3.95 KBrajiv.singh
#59 3167799-59.patch5.25 KB_utsavsharma
#59 interdiff_58-59.txt1.11 KB_utsavsharma
#58 3167799-58.patch5.23 KBsahilgidwani
#55 interdiff_48-55.txt8.58 KBsahilgidwani
#55 316779-55.patch9.06 KBsahilgidwani
#49 3167799-48.patch.gif16.4 MBpriyanka.sahni
#48 interdiff_46-48.txt3.07 KBranjith_kumar_k_u
#48 3167799-48.patch8.78 KBranjith_kumar_k_u
#46 3167799-46-media_library_widget_counter.patch8.66 KBcolorfulcoder
#38 3167799-after_patch-36.gif4.4 MBabhijith s
#36 3167799-36.patch8.89 KBanushrikumari
#33 drupal-do_not_stash_media_library_counter-3167799-33-8.x.patch9.07 KBguypaddock
#29 issue exist after applying patch #28.png236.41 KBjanmejaig
#28 3167799-28.patch8.69 KBpaulocs
#28 interdiff-20-28.txt2.2 KBpaulocs
#27 3167799-27.patch9.31 KBpaulocs
#27 interdiff-20-27.patch2.2 KBpaulocs
#25 Afterapplying_Patch_#20.png245.13 KBjanmejaig
#25 Before_Patch_#20.png75.29 KBjanmejaig
#25 Before_Patch_#20_02.jpg25.84 KBjanmejaig
#24 3167799_20.patch7.81 KBpaulocs
#21 test-only_3167799_20.patch1.24 KBvsujeetkumar
#20 interdiff_18-20.txt1.07 KBvsujeetkumar
#20 3167799_20.patch7.81 KBvsujeetkumar
#18 3167799_18.patch6.58 KBayushmishra206
#18 interdiff_16-18.txt6.58 KBayushmishra206
#16 interdiff_4-16.txt1.11 KBvsujeetkumar
#16 3167799_16.patch1.03 KBvsujeetkumar
#4 media-library-3167799-4.patch1.11 KBbandanasharma
#3 Screen Shot 2020-08-28 at 3.39.46 pm.png37.48 KBpameeela
#3 Screen Shot 2020-08-28 at 3.38.20 pm.png67.84 KBpameeela
#3 Screen Shot 2020-08-28 at 3.37.37 pm.png67.87 KBpameeela
#3 Screen Shot 2020-08-28 at 3.37.31 pm.png67.61 KBpameeela
Capture d’écran 2020-08-27 à 22.56.42.png18.04 KBflocondetoile

Comments

flocondetoile created an issue. See original summary.

flocondetoile’s picture

Title: 1 item select always displayed even if no media selected yet » "1 item selected" always displayed even if no media selected yet
pameeela’s picture

Title: "1 item selected" always displayed even if no media selected yet » Media library widget counter does not reset when removing and re-adding media
Issue summary: View changes
Issue tags: +Bug Smash Initiative
StatusFileSize
new67.61 KB
new67.87 KB
new67.84 KB
new37.48 KB

Updated title and summary based on my testing, I was only able to reproduce this when adding, removing and re-adding an image.

bandanasharma’s picture

Status: Active » Needs review
StatusFileSize
new1.11 KB

Write the patch, please review.

Status: Needs review » Needs work

The last submitted patch, 4: media-library-3167799-4.patch, failed testing. View results

flocondetoile’s picture

Original issue was a bit different. Especially with an unlimited entity reference field, and so with the selected count "1 item selected" and/or "X items selected". This is a bit different with a ER field with a cardinality limited.

The issue summary update shows another bug, in a different context. And so the proposed solution is inaccurate.

pameeela’s picture

@flocondetoile ah ok - that is why it is very helpful to include steps to reproduce.

Can you post specific steps to reproduce the original issue?

Do you think it’s possible to fix both at once or should it be two different issues?

flocondetoile’s picture

Issue summary: View changes
flocondetoile’s picture

Both issues looks very similar. This could be possible to fix both at the same time I think.

codersukanta’s picture

Tried to reproduce the mentioned issue but its working fine for unlimited cardinality. I have found only one discrepancy in count message for limited cardinality, If we already select any media and upload a new file from the add file section then the message is showing like this "2 of 1 item selected".

I think the issue https://www.drupal.org/project/drupal/issues/3092536 will take care of it. 

flocondetoile’s picture

@codersukanta I try to reproduced it on another project, and you're right. This is working fine. Sorry. Will try to figure why on the first project I have this small bug.

flocondetoile’s picture

OK. The bug was on Drupal 8.9.3. This is fixed for D9 for the unlimited cardinality.

pameeela’s picture

Issue summary: View changes

This bug definitely still occurs in 9.0.3 as described in my steps - only when you have selected an item already and removed it (for fields with 1 allowed item), or when you have selected an item and open the modal to select another one (for fields with multiple or unlimited).

I have updated the issue summary to reflect this.

phenaproxima’s picture

Priority: Minor » Major
Issue tags: +Media Initiative, +Triaged Media Initiative issue, +Usability, +Needs tests
vsujeetkumar’s picture

Status: Needs work » Needs review
StatusFileSize
new1.03 KB
new1.11 KB

@phenaproxima Write the patch, Please review.

seanb’s picture

Status: Needs review » Needs work
+++ b/core/modules/media_library/js/media_library.ui.js
@@ -147,8 +147,8 @@
+        var selectItemsText = remaining < 0 ? Drupal.formatPlural(Drupal.MediaLibrary.currentSelection.length, '1 item selected', '@count items selected') : Drupal.formatPlural(remaining, '@selected of @count item selected', '@selected of @count items selected', {

The change looks straightforward. Maybe we should remove the line: const currentSelection = Drupal.MediaLibrary.currentSelection; entirely and always directly use Drupal.MediaLibrary.currentSelection.

Btw, this needs to be changed in the media_library.ui.es6.js file as well and then compiled as explained in https://www.drupal.org/docs/frontend-developer-tools-for-drupal-core.

Last but not least, can we also add a test (and a fail patch) to show the issue and prove the fix to make sure we don't have regressions later on?

ayushmishra206’s picture

Status: Needs work » Needs review
StatusFileSize
new6.58 KB
new6.58 KB

Made the changes requested in #17. Please review.

seanb’s picture

Looks good, nice work! Only thing we have to add is a test and a test-only/fail patch to prove this fixes the issue. Easiest might be to add a couple of lines somewhere in Drupal\Tests\media_library\FunctionalJavascript\WidgetUploadTest.

Maybe an extra assert right after this?

    // Remove the item.
    $assert_session->elementExists('css', '.field--name-field-twin-media')->pressButton('Remove');
    $this->waitForNoText($png_image->filename);

    $this->openMediaLibraryForField('field_twin_media');

Like:

    // Remove the item.
    $assert_session->elementExists('css', '.field--name-field-twin-media')->pressButton('Remove');
    $this->waitForNoText($png_image->filename);

    $this->openMediaLibraryForField('field_twin_media');
    $assert_session->pageTextContains('0 of 2 items selected');
vsujeetkumar’s picture

StatusFileSize
new7.81 KB
new1.07 KB

@seanB test added advised in #19, Please review.

vsujeetkumar’s picture

StatusFileSize
new1.24 KB

Added test only patch, mentioned in #19

Status: Needs review » Needs work

The last submitted patch, 21: test-only_3167799_20.patch, failed testing. View results

seanb’s picture

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

That is good enough for me, thank you. RTBC.

paulocs’s picture

StatusFileSize
new7.81 KB

Hello @all,

I tested patch #20 and it is working as expected.
I attached the same patch again because the last patch from the issue is the test-fail one and if patch #20 will be committed, it needs to be last patch inserted in the issue.
Next time you can attach the test fail patch and the solution patch together in the same comment @vsujeetkumar :)

RTBC+1

Cheers, Paulo.

janmejaig’s picture

StatusFileSize
new25.84 KB
new75.29 KB
new245.13 KB

I have checked the above issue with patch at #20. It is working fine to address the media counter issue, however I found a small glitch:

When we click the "Insert Selected" button without selecting any media the popup closes (which is fine), but on reopening the media popup, it displays an error message as "No items selected" which is confusing.

Ideally in this scenario I believe the media popup should not close and must display an error message.

Attaching a screenshot for reference .

janmejaig’s picture

Status: Reviewed & tested by the community » Needs work
paulocs’s picture

Status: Needs work » Needs review
StatusFileSize
new2.2 KB
new9.31 KB

Not consider this patch and the interdiff.
Hello,
This error is not occurring because of patch #20.
I added a patch that disables the button while no media is selected.

Cheers, Paulo.

paulocs’s picture

StatusFileSize
new2.2 KB
new8.69 KB

Patch #27 and interdiff are wrong.

Please consider what I attached in this comment.
This patch disables the "Insert selected" button while no media is selected.

Thanks.

janmejaig’s picture

Status: Needs review » Needs work
StatusFileSize
new236.41 KB

This is not working as expected , the issue still persist.

paulocs’s picture

Hello @janmejaig,

what are the steps that you did to test the patch?

I'm asking because I saw the image that you attached and no file is selected and the button "Insert selected" is not disabled.
Did you clear your cache after you applied it?

Cheers, Paulo.

janmejaig’s picture

Hello @paulocs ,
I have simply applied the patch at #28, after applying the patch I cleared the cache and hard refreshed the browser and progressed as per #25.
Hope I clarified your query , do let me know if anything specific I am missing here.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

guypaddock’s picture

Here's a stab at backporting this to Drupal 8.9.x.

guypaddock’s picture

Status: Needs work » Needs review

Not sure why #29 indicated this NW. With this patch, media library is working properly for us. The "Insert selected" button is only enabled when an actual selection is made -- even if a selection is made, the library is closed, and then the library is re-opened.

Setting to NR for additional confirmations just in case.

anushrikumari’s picture

Assigned: Unassigned » anushrikumari
anushrikumari’s picture

StatusFileSize
new8.89 KB

Rerolled as patch #29 for 9.2.x

anushrikumari’s picture

Assigned: anushrikumari » Unassigned
abhijith s’s picture

StatusFileSize
new4.4 MB

Applied patch #36 and it is not fixing the problem.The 'No items selected' error still appears after applying this patch.
Adding recording

after

phenaproxima’s picture

@Abhijith S, thank you for the screen recording! That's very helpful.

What's not clear, though, is if that issue is being caused by this patch. Can you reproduce that problem with a vanilla copy of Drupal core (without this patch applied)? Or does it only happen when this patch is applied?

michaellenahan’s picture

Issue summary: View changes
Issue tags: +Novice, +Europe2020

Removing Europe2020 tag, since this issue was not worked on at Drupalcon Europe 2020.

michaellenahan’s picture

Issue summary: View changes
Issue tags: -Europe2020

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.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.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

tamsinfd’s picture

Issue summary: View changes

Having reviewed the video at #38, I can verify that the issue shown is not caused by patch #36. I was able to reproduce the issue with a vanilla installation of Drupal.

colorfulcoder’s picture

Yesterday I helped tamsinfd with her first contribution, and we totally forgot to add that we did it during #ddd2022.

colorfulcoder’s picture

Issue summary: View changes
StatusFileSize
new8.66 KB

This is a reroll of the patch in #36.
The old patch was outdated, and contained some mistakes causing it to not apply correctly.
This patch works well, except for one edge case. When you upload a new file, instead of choosing an existing one, and then discard it.
I think that is because the check for disabling the button is not done when a newly uploaded file is discarded. You will see that the media library dialog is shown again, with an enabled button.

colorfulcoder’s picture

Issue tags: +ddd2022

Added DrupalDevDays2022 tag

ranjith_kumar_k_u’s picture

StatusFileSize
new8.78 KB
new3.07 KB

Fixed CS errors.

priyanka.sahni’s picture

StatusFileSize
new16.4 MB

Verified and tested the issue by applying patch 3167799-48.patch. Patch was successfully applied and issue got fixed.
Please refer to the video attached.

3167799-48.patch.gif

mradcliffe’s picture

I added the Portland2022 tag. I think the issue is almost RTBC, but the issue summary needs to be updated to reflect recent comments. Specifically it would be nice to have for a committer to see @priyanka.sahni 's manual testing animated image in the issue summary as well as to confirm testing on 9.4.x branch.

phenaproxima’s picture

Status: Needs review » Needs work

This definitely seems pretty good but I think it could use a little work, mostly to document the nature of the change.

  1. +++ b/core/modules/media_library/js/media_library.ui.es6.js
    @@ -308,7 +307,7 @@
    +                Drupal.MediaLibrary.currentSelection.length,
    

    I'm a little unclear on why we're changing these variable references. Is this the main part of the fix? If so, it would be good to have a comment explaining why we're referencing the counter this way, so we don't accidentally regress it.

  2. +++ b/core/modules/media_library/js/media_library.ui.es6.js
    @@ -317,12 +316,17 @@
    +        if (Drupal.MediaLibrary.currentSelection.length === 0) {
    +          $('.media-library-select').attr('disabled', true);
    +        } else {
    +          $('.media-library-select').attr('disabled', false);
    +        }
    

    🤔 I thought that disabled was a "present or absent" sort of attribute. Wouldn't we want this to be something like:

    if (length === 0) {
      $('.media-library-select').attr('disabled', true);
    }
    else {
      $('.media-library-select').removeAttr('disabled');
    }
    

    Also, we usually only want to target classes prefixed with js- in JavaScript. As I understand it, non-prefixed classes are intended for styling. Is there a js- prefixed class we could use here, or some other way to select it?

  3. +++ b/core/modules/media_library/js/media_library.ui.es6.js
    @@ -331,15 +335,15 @@
    +        const position = Drupal.MediaLibrary.currentSelection.indexOf(id);
    

    As with the counter, why are we referring to this variable by its fully qualified scope?

  4. +++ b/core/modules/media_library/tests/src/FunctionalJavascript/WidgetUploadTest.php
    @@ -140,6 +140,7 @@ public function testWidgetUpload() {
    +    $assert_session->pageTextContains('0 of 2 items selected');
    

    This line could use a comment to explain why we're asserting this text. Also, if we can scope it a little more clearly (maybe using $assert_session->elementTextContains() or similar so that it's clear that we're looking at the selection counter of the media library, that would probably be helpful to future maintainers.

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

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now 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.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now 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: 10.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, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

sahilgidwani’s picture

StatusFileSize
new9.06 KB
new8.58 KB

Rerolled patch for drupal core 9.5.x

anybody’s picture

Just ran into this issue and can confirm it still exists.

sahilgidwani’s picture

StatusFileSize
new5.23 KB

Adding patch for v10.1.x

_utsavsharma’s picture

StatusFileSize
new1.11 KB
new5.25 KB

Fixed failures in #58.

xjm’s picture

Issue tags: -Novice

#51 still needs to be addressed. When someone is able to address it, this issue can be converted to a merge request (but credit will not be granted for converting this issue to an MR without addressing #51). Thanks!

rajiv.singh’s picture

StatusFileSize
new3.95 KB

The patch is not applying to D 11.2.10:
Why the patch wasn't applying:
Drupal 11.2.10 partially includes the fix: it removed const currentSelection = Drupal.MediaLibrary.currentSelection; and updated most references.
Lines 336-341 still referenced currentSelection (undefined), causing a bug.
The patch expected the old code structure (with the const declaration), so it couldn't apply.
What is fixed in attached patch :
Updated lines 334-342 to use Drupal.MediaLibrary.currentSelection instead of currentSelection.
Removed the patch from composer.json (line 299) since it's incompatible with D11.2.10.
The fix:
Changed currentSelection.includes(id) to use Drupal.MediaLibrary.currentSelection.indexOf(id) with position checking
Changed currentSelection.push(id) to Drupal.MediaLibrary.currentSelection.push(id)
Changed currentSelection.splice() to Drupal.MediaLibrary.currentSelection.splice()

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.