Media Library view widgets do not support contextual filters

Created on 25 May 2020, over 4 years ago
Updated 29 July 2024, 5 months ago

Problem/Motivation

I'm not sure if this is a known bug since some time or not, but it seems that by adding any additional Contextual Filter to the "Media Library" widgets (Grid and Table), then the Table mode breaks and you can no longer insert a selected image if you use a frontend theme for your node content form.

Steps to reproduce the issue

  • Install Drupal (e.g. with: drush si -y demo_umami --sites-subdir default --account-name admin --account-pass admin )* so that you end up with the default "Media Library" view from say the Demo Umami profile
  • Edit the "Media Libary" view: Widget and Widget (table) displays
  • Add a contextual filter to both displays
  • Allow node creation for authenticated users without giving them access to the backend theme, or unset "force backend theme" for node creation/edit in Theme Appearance settings
  • Login as an authenticated user (i.e. not admin user account)
  • Go to add media via the media library popup, select "Table" and try to select a media item from the library then "insert selected"
  • Nothing should happen, it should stay on the media popup, and an Drupal.Ajax error should appear in console
  • There are no log errors... this makes it hard to debug.

* You can skip the install if you have a working Drupal site, as long as you have the default "Media Library" view.

πŸ› Bug report
Status

Active

Version

11.0 πŸ”₯

Component
Media  β†’

Last updated 1 day ago

Created by

πŸ‡¨πŸ‡­Switzerland dan2k3k4 Zurich

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

    I had issue with contextual filter on media library view.
    Was solved with adding query condition in custom module.

    /**
     * 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 solimanharkas Hamburg

    #19 πŸ› Media Library view widgets do not support contextual filters Active works for me! Thanks

  • πŸ‡§πŸ‡ͺBelgium herved

    I think I just stumbled on this issue.

    It looks like the form ID changes because it is built according to the views arguments being passed, e.g:
    - initially (modal loaded): views_form_media_library_widget_image
    - click apply filters or pager: views_form_media_library_widget_image_[some additional args]
    - click insert selected: views_form_media_library_widget_image

    FormBuilder::buildForm checks the form_id to build the ajax response and throw FormAjaxException.
    But since the form_id doesn't match, drupal sends an HTML response instead of JSON, which results in a "parsererror" in JS.

    The main suspect here to me is MediaLibraryUiBuilder::buildMediaLibraryView which passes only 1 argument to the view:
    $args = [$state->getSelectedTypeId()];

    Just unsure what to do/how to solve at this point.
    Any thoughts?

  • πŸ‡§πŸ‡ͺBelgium herved

    Update: πŸ› MediaLibraryUiBuilder service does not properly allow additional contextual filter arguments Needs work sounds like the proper fix, works perfect for me.
    Could some of you test as well so we could close this issue here as duplicate?
    Thanks

  • πŸ‡¨πŸ‡¦Canada pavlosdan

    I tested the MR in #3401726 as well and it seems to resolve the issue.

Production build 0.71.5 2024