Taxonomy filtering on media library form upload

Created on 4 July 2022, over 2 years ago
Updated 5 December 2023, about 1 year ago

Problem/Motivation

When adding new media via the media library (media / media library / add media) taxonomies that the account does not have access to are filtered out and cannot be selected. However if I create a new node and use the media library widget to add a new file then the taxonomies do not seem to get filtered at all.

Steps to reproduce

1. Download Drupal 9.4.1 install using the Standard install profile, and log in as an administrator.
2. Install workbench access composer require 'drupal/workbench_access:^1.0@beta'
3. Enable workbench access
4. Create a new vocabulary add some test terms
5. Add a new taxonomy term field to a media type, e.g. Image
6. Add a media field to a content type, e.g. basic page ensure it's configured to accept the media type you've configured the taxonomy term field on
7. Create a new role, assign it permissions to create content and the media type as well as permission to be assigned to access scheme sections
8. Create a new account with the role
9. Create a taxonomy access scheme configured to use the vocabulary and the media entity type you configured
10. Assign the role to one of the sections
11. Login with the new account, visit /media/add and select the media type you added the taxonomy field to
12. Note how the taxonomy list is limited to the assigned section(s) for the account
13. Visit the add content page for the content type you configured the media field on
14. Upload a new file, note how the taxonomy list is not restricted and shows all available taxonomies

Proposed resolution

I'm not too sure where to start but if someone can try to point me in the right direction I can have a go at resolving this. From a quick look it seems to that in workbench_access_form_alter the form object is a type of AddFormBase so immediately returns out - also in FormAlterHelper the access scheme does not apply because we're working with a node rather than media.

✨ Feature request
Status

Active

Version

2.0

Component

Code

Created by

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.

  • πŸ‡ΊπŸ‡ΈUnited States jalite1991

    Can confirm this is still an issue. The taxonomy terms do not get filtered when trying to add media through the "Media Library add form upload" component and the media types are associated with the workbench schemes.

  • πŸ‡ΊπŸ‡ΈUnited States jonraedeke

    It seems like a bug if a user can assign new media to a section they don't have access to.

  • πŸ‡ΊπŸ‡ΈUnited States aarontocenter

    I can concur with @jonraedeke that this is a bug, @agentrickard can you change this request from Feature, to bug.

    Recently have a client that needs users to be able to upload to directories they are part of as a "group" associated by the "area" of ownership.

    Because of this taxonomy issue, they are able to upload files, to areas they themselves have no access too, and the only way to give them access, is to open up areas they should be restricted from.

  • πŸ‡ΊπŸ‡ΈUnited States bkosborne New Jersey, USA
  • πŸ‡ΊπŸ‡ΈUnited States bkosborne New Jersey, USA

    Confirmed this is an issue as well.

    The form_alters that Workbench Access performs assume that the form a property entity form:

    function workbench_access_form_alter(&$form, FormStateInterface $form_state) {
      $form_object = $form_state->getFormObject();
      if (!$form_object instanceof EntityForm) {
        return;
      }
      $entity = $form_object->getEntity();
      if (!$entity instanceof ContentEntityInterface) {
        return;
      }
      \Drupal::classResolver()->getInstanceFromDefinition(FormAlterHelper::class)->alterForm($form, $form, $form_state, $entity);
    }
    

    But the form that the Media Library module uses is actually a custom form called Drupal\media_library\Form\FileUploadForm. That form embeds an entity form within it, but doing so does not invoke form alter hooks for the entity form.

    I think Workbench Access needs to add some special handling for these media library forms. I'll add a merge request.

  • πŸ‡ΊπŸ‡ΈUnited States bkosborne New Jersey, USA

    Okay, this will need tests obviously, but I think this approach could work. I'm a little uncertain on if I should be passing the complete form in or not as the second argument to FormAlterHelper::alterForm. But in my brief testing this works.

  • Pipeline finished with Failed
    5 days ago
    Total: 133s
    #371542
Production build 0.71.5 2024