- 🇺🇦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 1:04pm 22 May 2023 - 🇮🇳India shashank5563 New Delhi
I have tested #17 on local and found it is working as expected.
- 🇳🇱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 7:59am 25 October 2023 - 🇻🇳Vietnam phannphong Ho Chi Minh City
I can confirm that #17 is working as expected.
- Status changed to Closed: duplicate
10 months ago 4:12am 20 February 2024 - 🇦🇺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.