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
- Install Drupal
- Enable media library
- Create a media image
- Add a media image field to the article content type with 1 item allowed
- Create an article
- Click Add media and select an image
- Remove the selected image
- Click Add media
- Note that the count says 1 of 1 even though nothing is selected
- Click Insert selected
- An empty selection is added
- Save
- 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