MediaLibraryUiBuilder service does not properly allow additional contextual filter arguments

Created on 15 November 2023, about 1 year ago
Updated 10 August 2024, 3 months ago

Problem/Motivation

If you attempt to add contextual filters to the core media library, they will correctly be used when the library results are initially loaded, but are not passed to the view itself, so internal display links (the "grid" and "table" buttons) get URLs built that ignore the contextual arguments.

Steps to reproduce

Add/edit the any content type
Add media reference field, in manage form display of content type: select
'media library' display
Then edit the view of core media libray (widget display):
https://your_site_url/admin/structure/views/view/media_library
Then add a contextual argument to view displays (for eg. wigets , widget table), such as "media: authored by" and select "user ID from logged in user" as the default value or either can add fixed uid for test purpose
Add node, try to add media , Click on one of the display buttons in the header, either grid or table, you 'll find contextual filter not work as view will show all media instead of just your own.

Actual behaviour

Click on one of the display buttons in the header, either grid or table, and the view will refresh to show all media instead of just your own. This is wrong.

Expected behaviour

when try to upload image using a media library widget, then view that is loaded only shows the media your user has created. This is correct.

Proposed resolution

Replace the current build call in buildMediaLibraryView()

return $view_executable->buildRenderable($display_id, $args, FALSE);

with

return $view_executable->buildRenderable($display_id, [], FALSE);

I did initially try this with $view_executable->args, but that seems to be redundant and passing [] has exactly the same effect. Current behaviour which passes through just the entity type argument ($args) stops any other contextual filters from working securely when the view is rendered. The $view_executable->execute($args) call a few lines earlier already does the full argument calculations for all contextual filters.

Add tests

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
Media 

Last updated about 6 hours ago

Created by

🇳🇿New Zealand dieuwe Auckland, NZ

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

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

  • Issue created by @dieuwe
  • Status changed to Needs review about 1 year ago
  • 🇳🇿New Zealand dieuwe Auckland, NZ
  • Status changed to Needs work about 1 year ago
  • The Needs Review Queue Bot tested this issue.

    While you are making the above changes, we recommend that you convert this patch to a merge request . Merge requests are preferred over patches. Be sure to hide the old patch files as well. (Converting an issue to a merge request without other contributions to the issue will not receive credit.)

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update about 1 year ago
    29,686 pass
  • First commit to issue fork.
  • 🇦🇺Australia sime Melbourne

    Patch worked for me, and I've created the issue fork for it.

  • Pipeline finished with Failed
    9 months ago
    Total: 606s
    #99057
  • Status changed to RTBC 6 months ago
  • 🇮🇳India Manthan.Chauhan

    I confirm that this patch fixes the issue since i was having the same issue.

  • 🇫🇷France erwangel

    Confirmed working by me too.

  • Pipeline finished with Success
    6 months ago
    Total: 1448s
    #177445
  • 🇦🇺Australia sime Melbourne

    Rebased and tests passing. There was an failed test that passwed when i reran.

  • Status changed to Needs work 6 months ago
  • 🇳🇿New Zealand quietone

    I read the issue summary, comments and the MR. I do think that there should be a test here.

    I also tried to test this but wasn't able to reproduce the problem. Following the steps to reproduce I found that the 'Grid' showed only media from the logged in user and the 'Table' showed media for all users. This was true with or without the diff applied. But, I seem to be in the minority here about that.

    Still, I do recommend tests.

  • 🇧🇪Belgium herved

    Amazing!
    I debugged this for a while before commenting 🐛 Media Library view widgets do not support contextual filters Active and implementing a service decorator to modify $args...
    This is a much nicer fix and works perfectly in my case as well.
    I'll see if I can add tests on Monday.
    Thanks

  • First commit to issue fork.
  • Pipeline finished with Failed
    4 months ago
    Total: 160s
    #235509
  • Pipeline finished with Failed
    4 months ago
    #235510
  • Pipeline finished with Success
    4 months ago
    Total: 625s
    #235513
  • Status changed to Needs review 4 months ago
  • 🇨🇦Canada pavlosdan

    Added test. Setting back to needs review.

  • Pipeline finished with Failed
    4 months ago
    Total: 159s
    #235650
  • Pipeline finished with Failed
    4 months ago
    Total: 179s
    #235654
  • Pipeline finished with Success
    4 months ago
    Total: 506s
    #235657
  • I have applied some changes that improve performance & optimize the resource usability of test code.

    Keeping it in review for further suggestions.

  • Status changed to Needs work 4 months ago
  • The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • Pipeline finished with Failed
    4 months ago
    Total: 116s
    #235771
  • Pipeline finished with Success
    4 months ago
    Total: 516s
    #235775
  • Status changed to Needs review 4 months ago
  • 🇧🇪Belgium herved

    If my understanding is correct, any AJAX request inside the modal was causing the form ID to change resulting in an HTML response instead of JSON which JS can't process and we get AJAX "parsererror". Some more details here: #3142777-22: Media Library view widgets do not support contextual filters
    A quick way I used to debug was to click "Apply filters", then "Insert selected" -> Uncaught Drupal.AjaxError in console

    Should we add something like this to the test for good measure?

    // Submit exposed views filters.
    $this->getSession()->getPage()->find('css', '#media-library-view')->pressButton('Apply filters');
    $this->assertSession()->assertWaitOnAjaxRequest();
    $this->waitForElementsCount('css', '#media-library-view .views-row', 2);
    
    // Select and submit items.
    $this->selectMediaItem(0);
    $this->selectMediaItem(1);
    $this->pressInsertSelected();
    

    Besides that it looks great to me!

  • Pipeline finished with Success
    4 months ago
    Total: 479s
    #236371
  • I have reviewed the recent changes in test code adding views handler programmatically instead of making http request improved the resource usage from Time: 00:22.841, Memory: 6.00 MB to Time: 00:16.287, Memory: 6.00 MB

    Keeping it in review for further suggestions.

  • Status changed to Needs work 3 months ago
  • 🇺🇸United States smustgrave

    Seems the tests were skipped vs failing so wonder if they need some work.

  • 🇧🇪Belgium herved

    #21, I had the same in this issue: 🐛 JS errors from Drupal.behaviors.activeLinks (... is not a valid selector) Needs work
    Is the "Test-only changes" pipeline working correctly? Is it skipping FunctionalJavaScript tests for some reason?
    I'm 99% sure tests in both issues fail as expected...

  • Pipeline finished with Failed
    3 months ago
    Total: 127s
    #248027
  • Pipeline finished with Success
    3 months ago
    Total: 26959s
    #249387
  • Pipeline finished with Success
    3 months ago
    #248029
  • Pipeline finished with Failed
    3 months ago
    #249631
  • 🇨🇦Canada pavlosdan

    Asked in the #gitlab channel on Drupal slack and there was indeed a problem with the pipeline. See: 🐛 Test-only job cannot be run due to wrong dependency Fixed
    It is now resolved. Rebased the fork and the tests seem to be triggering now: https://git.drupalcode.org/issue/drupal-3401726/-/pipelines/249631

Production build 0.71.5 2024