Media Library filtering by user not possible

Created on 1 June 2022, over 2 years ago
Updated 20 February 2024, 10 months ago

Problem/Motivation

Users can see all media files from all other users when creating content that has a 'Media' field using the Media Library widget.
The issue is this, my current project is expecting a little over 100 users at launch, and if things go well, it could go into the thousands.
When user 101 goes to add content and the select the field to upload the Media Library will show all images/files from all of the other users, it could be over 500 items to scroll through. Now, all of the images/files will all be public, the issue is that the user should only see/work with their files. If the site does reach 1000+ users, it will be an absolute nightmare for a user to find their content. Plus, I would not want a user to reuse someone else's content/media as a rule.

Also, the overhead of having to load all of those images seems to me would be an issue as the site user count increases.

Fixes tried:
Create content by three different users
Added a contextual filter of 'Author by' to (Media Library) views on all pages, this will filter the view on /admin/content/media but in the content type it shows no results when browsing media. Removing the contextual filters will show all content from all users.

Steps to reproduce

  1. Create a content type
  2. Add 'Media' field
  3. Set 'Manage Form Display' widget for Media field to "Media Library"
  4. Create content for new content type, add media files to field.
  5. Do this for two other users, the content from all three users will be available to each one.

Proposed resolution

The default behavior should be for the Media Library browser to auto filter by user logged in.
Only the content the logged in user has uploaded should be visible to the user.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Feature request
Status

Closed: duplicate

Version

11.0 🔥

Component
Media 

Last updated 4 days ago

Created by

🇵🇹Portugal joaomachado

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.

  • 🇺🇦Ukraine Anna D

    Thanks @Antoniya. I was trying to implement #6 code. And it works but not fully as espected.
    Result is checked for media items per page. And if you don't have needed medias for current user on first page you will have empty page, on the next page you can have few medias, on next page another amout of medias... etc. I hope I described well.
    But your code give me an idea I'm attaching. Hope it'll help to somebody.

    /**
     * Implements hook_views_query_alter().
     */
    function mymodule_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
        if ($view->id() == 'media_library') {
        $media_ids  = [..Get needed ids..];
        // If media ids is empty provide 0 id.
        $media_ids = $media_ids ?? [0];
        $media_content_data = $query->setWhereGroup('AND');
        $query->addWhere($media_content_data, 'media_field_data.mid', $media_ids, 'IN');
      }
    }
    
  • 🇩🇪Germany antoniya

    Ahh, of course! I guess I did my manual testing only with 3-4 media items. 🤦‍♀️ Thank you @Anna D!

  • 🇫🇷France benjbmc

    Thank you for the code @Anna D, it gave me an idea too. I needed to limit media access per user except for the administrator role. And indeed the contextual filter works well for the view list but not for the widget. Here is what I did, it seems to work:

    function mymodule_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
      if ($view->id() == 'media_library') {
        if (!in_array('administrator', \Drupal::currentUser()->getRoles())) {
          $media_content_data = $query->setWhereGroup('AND');
          $query->addWhere($media_content_data, 'media_field_data.uid', \Drupal::currentUser()->id(), 'IN');
        }
      }
    }
    
  • 🇮🇳India Akhil Babu Chengannur

    Implemeted hook as in #17 and it worked. Thanks.

  • Status changed to Needs work over 1 year ago
  • 🇮🇳India shashank5563 New Delhi

    I have tested #17 on local and found it is working as expected.

  • I have also tested #17, it does exactly what I need. Thanks.

  • 🇳🇱Netherlands ecvandenberg

    For those who look for this solution, I have tried the function in #17 but that didn't work. Now, I'm not a good module developer, but with some help from the chatbot I came to this module. It also adds a permission setting View all media.

    my_module.module:

    
    use Drupal\views\ViewExecutable;
    use Drupal\views\Plugin\views\query\Sql;
    
    /**
     * Implements hook_views_query_alter().
     */
    
    function my_module_views_query_alter(ViewExecutable $view, Sql $query) {
      if ($view->id() == 'media_library') {
        $user = \Drupal::currentUser();
        $user_roles = $user->getRoles();
    
        // Check if the user has "View all media" permission.
        if (in_array('administrator', $user_roles) || $user->hasPermission('View all media')) {
          return;
        }
    
        $query->addWhereExpression('AND', 'media_field_data.uid = :current_user_id', [':current_user_id' => $user->id()]);
      }
    }
    
    

    And my_module.permissions.yml:

    View all media:
      title: 'View all media'
      description: 'View all media in the media library widget'
    
    
  • Status changed to Active about 1 year ago
  • ivnish Kazakhstan

    #19 why status is "needs work"?

  • 🇻🇳Vietnam phannphong Ho Chi Minh City

    I can confirm that #17 is working as expected.

  • Status changed to Closed: duplicate 10 months ago
  • 🇦🇺Australia sime Melbourne

    There are some great workarounds here, but I believe this is a duplicate of https://www.drupal.org/project/drupal/issues/3401726 🐛 MediaLibraryUiBuilder service does not properly allow additional contextual filter arguments Needs work where there is a patch that solves the problem in the original issue for me - it will allow you to add a contextual filter to the media_library widget displays which uses the current logged in user as the media author.

Production build 0.71.5 2024