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

Issue fork drupal-3401726

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

dieuwe created an issue. See original summary.

dieuwe’s picture

Status: Active » Needs review
StatusFileSize
new624 bytes
needs-review-queue-bot’s picture

Status: Needs review » Needs work

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.)

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. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

sime made their first commit to this issue’s fork.

sime’s picture

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

manthan.chauhan’s picture

Status: Needs work » Reviewed & tested by the community

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

erwangel’s picture

Confirmed working by me too (on D10.2.6).

sime’s picture

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

quietone’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

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.

herved’s picture

Amazing!
I debugged this for a while before commenting #3142777: Media Library view widgets do not support contextual filters 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

pavlosdan made their first commit to this issue’s fork.

pavlosdan’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests

Added test. Setting back to needs review.

pooja_sharma’s picture

Issue summary: View changes

I have updated IS w.r.t to standard issue template along with added details steps for better clarity, as remaining task - Add test addressed , move it from the remaining tasks section to the proposed resolution.

pooja_sharma’s picture

I have applied some changes along with perms one those 'll improve performance & optimize the resource usability of test code.

Keeping it in review for further suggestions.

needs-review-queue-bot’s picture

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

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.

pooja_sharma’s picture

Status: Needs work » Needs review
herved’s picture

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 these steps to the test for good measure?
Also, couldn't we create the views handler programmatically, to simplify and save some time.

PS: I tried to push some those changes but it seems I don't have access. Anyway I wouldn't want to interfere.
PS2: Right, I had to click on "get push access", let me know if you agree with the changes.

Besides that it looks great to me!
And I think we can close #3142777: Media Library view widgets do not support contextual filters as duplicate.

pooja_sharma’s picture

I have reviewed the recent changes looks good 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.

smustgrave’s picture

Status: Needs review » Needs work

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

herved’s picture

#21, I had the same in this issue: #3464340: JS errors from Drupal.behaviors.activeLinks (... is not a valid selector)
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...

pavlosdan’s picture

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

tobiasb made their first commit to this issue’s fork.

tobiasb’s picture

Status: Needs work » Reviewed & tested by the community

Rebased. Test-Only fails.

But I will also test the fix in a real use-case.

tobiasb’s picture

Status: Reviewed & tested by the community » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Test-only ran here https://git.drupalcode.org/issue/drupal-3401726/-/jobs/7322548

Rebased as it was several commits behind but still green and believe all feedback has been resolved.

longwave’s picture

Version: 11.x-dev » 10.6.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Backported to 11.3.x as an eligible bug fix. This is also eligible to go to 10.6.x but the tests will need reworking for the older version of PHPUnit, marking patch to be ported - to be honest given the one line fix I think we could just backport the fix here and be done, so if anyone wants to open a one-line MR for that then please do so.

Committed and pushed 3c10d8c26ce to main and aca36058964 to 11.x and 4f49a0d113a to 11.3.x. Thanks!

  • longwave committed 4f49a0d1 on 11.3.x
    fix: #3401726 MediaLibraryUiBuilder service does not properly allow...

  • longwave committed aca36058 on 11.x
    fix: #3401726 MediaLibraryUiBuilder service does not properly allow...

  • longwave committed 3c10d8c2 on main
    fix: #3401726 MediaLibraryUiBuilder service does not properly allow...
longwave’s picture

Version: 11.x-dev » 10.6.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

smustgrave’s picture

Status: Patch (to be ported) » Reviewed & tested by the community

Backported to 10.6.x

  • godotislate committed 42389669 on 10.6.x
    fix: #3401726 MediaLibraryUiBuilder service does not properly allow...
godotislate’s picture

Status: Reviewed & tested by the community » Fixed

Committed 4238966 and pushed to 10.6.x. Thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.