Media library widget counter does not reset when removing and re-adding media

Created on 27 August 2020, over 4 years ago
Updated 23 June 2023, over 1 year ago

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

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
Media 

Last updated about 13 hours ago

Created by

🇫🇷France flocondetoile Lyon

Live updates comments and jobs are added and updated live.
  • Usability

    Makes Drupal easier to use. Preferred over UX, D7UX, etc.

  • Novice

    It would make a good project for someone who is new to the Drupal contribution process. It's preferred over Newbie.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024