Not working well with Media library modal dialog

Created on 3 March 2020, over 4 years ago
Updated 3 April 2024, 3 months ago

We have switched to using Media Library as our form media browser.
We are using Drupal Core 8.8.2 and Views Infinite Scroll 1.6.

The current behavior I'm experiencing:
When the media library is opened in a modal and we scroll down, multiple 'Insert Selection' buttons appear as the view loads more rows via ajax.
The multiple 'Insert Selection' buttons also appear when using the load more button provided by Views Infinite Scroll.

The the ajax load more is only triggered when the page is scrolled not the modal window.

Image open_modal.png shows the starting default with 'load more' and a items on page of 2.

Image first_load_more.png shows the behavior after the first click on 'load more' with the extra `Insert Selected` button.

Image second_load_more.png is the second click on 'load more' with the two extra 'Insert Selected' buttons.

Image autoloading_example.png is showing the behavior with the autoload enabled, notice the position of the page behind the modal. I had to scroll that to get the autoload to trigger, and we again have the addition of the extra 'Insert Selected' button.

media_library_modal.txt contains the html markup after using 'load more'.

Expected behavior:
Senario 1:
When the automatic infinite scroll enabled,
And I open the media library modal,
And I am focused on the media library modal,
Then I scroll to the end of the loaded content,
Then the modal should trigger the loading of additional content.
Then there should only be one 'Insert Selected' button for submission.

Senario 2:
When the automatic infinite scroll is disabled,
And I open the media library modal,
Then I scroll to the end of the loaded content,
And I should see the 'load more' button.
When I click the 'load more' button,
Then additional content should be loaded,
And there should only be one 'Insert Selected' button for submission.

๐Ÿ› Bug report
Status

Active

Version

2.0

Component

Code

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States oheller

Live updates comments and jobs are added and updated live.
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.

  • Status changed to Postponed: needs info about 1 year ago
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany Anybody Porta Westfalica

    Is this still an issue with 2.0.x? If yes, please update the version and set the status active again with clear steps or (best) with a test showing what's broken.

    2.0.x is the active development branch.

    If nobody replies, we should assume this is fixed and close this issue outdated.

    Thanks!

  • This is still an issue with 2.x. Just installed it on my fresh D10 site and I'm seeing a continually duplicating "Insert selected" button appearing.

  • Status changed to Active 11 months ago
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany Anybody Porta Westfalica

    As of #7.

    Sounds like a JS context / once issue to me (AJAX related). Could someone prepare a MR with a fix?

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany Anybody Porta Westfalica
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany rgpublic Dรผsseldorf ๐Ÿ‡ฉ๐Ÿ‡ช ๐Ÿ‡ช๐Ÿ‡บ

    Has anyone in the meantime found at least the reason for why this happens?

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany rgpublic Dรผsseldorf ๐Ÿ‡ฉ๐Ÿ‡ช ๐Ÿ‡ช๐Ÿ‡บ

    Okay, I've looked into this further:

    1) "Drupal\media_library\Plugin\views\field\MediaLibrarySelectForm" adds the "Insert selected" button at the bottom of the rendered view
    2) Because of the "drupalAutoButtons" feature for modal dialogs which is enabled by default, any buttons which appear in the dialog's content area are moved out of there and into the actions bar at the bottom.
    3) Views Infinite Scroll doesn't just add rows but adds a whole new rendered view page which includes another button. So after click "Load more" once, we have now: [Some rows] + [Button] + [More rows] + [Button]. The drupalAutoButtons features rips out all the buttons from the DOM and places them next to each other at the bottom.
    4) And so on...

    I found one solution (which is perhaps a bit better than just hiding the extra buttons with CSS) to insert the following line in js/infinite_scroll.js :

       var $newRows = $newView.find(contentWrapperSelector).children();
        var $newPager = $newView.find(pagerSelector);
    
        $newRows.find('.button.media-library-select').remove(); // <== This is new
    

    Of course, this is still not really the best solution because the extra buttons shouldn't be generated in the first place.
    I guess we should wait for #3098132 to be resolved here because we actually want to only replicate the rows - not the whole view with HTML form tags etc. After this issue is resolved, I guess this issue here should get resolved automatically without any extra JS mumbo jumbo, because we won't replicate the insert button anymore but just the rows.

    Another side-effect of this btw, is that the fade-out effect doesn't work for the media thumbnails loaded with "Load more". Usually if I can select only one media file (cardinality 1) and I subsequently select only one, all the other thumbnails will get a darken / fade-out effect. This also won't work because we add a whole new form so the items which need to be faded-out are now spread across multiple forms. This might also get resolved with #3098132 but perhaps extra work is needed. We'll see. Just wanted to note that this is sth. to keep track of.

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany rgpublic Dรผsseldorf ๐Ÿ‡ฉ๐Ÿ‡ช ๐Ÿ‡ช๐Ÿ‡บ
Production build 0.69.0 2024