Restrict media types by permission for entity reference field widget

Created on 10 March 2023, about 2 years ago

The project I'm working on has a use case where they need both the media library via WYSIWYG to restrict media types available by permission and also media library via entity reference to restrict media types available by permission (they have a catch-all entity reference field that supports referencing all of the site's media types, but they only want certain roles to be able to add existing audio/video media into that field).

I took a first pass at decorating the MediaLibraryWidget field widget plugin to reset the list of allowed media types depending on if the user has the appropriate permission. Once I've more time to circle back, I can add tests but figured I'd put this patch out there in case anyone else has a similar use case.

✨ Feature request
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States cainaru Norwood, NY, USA

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

Comments & Activities

  • Issue created by @cainaru
  • πŸ‡ΊπŸ‡ΈUnited States brockfanning

    I don't know the internals enough to explain why, but one particular line in this patch is causing issues on our side. My wild guess is that maybe in previous versions of Drupal a particular array was associative, but now it's a flat array. Our project also has a ton of patches though, so it's also possible that some other patch is causing this problem. Anyway I'm attaching a version of the patch that seems to get around this problem for us. I changed this line:

    $filtered_media_type_ids = array_intersect_key($allowed_media_type_ids, array_flip($permitted_medias));
    

    To this:

    $filtered_media_type_ids = array_intersect($allowed_media_type_ids, $permitted_medias);
    
Production build 0.71.5 2024