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.

Comments

dan2k3k4 created an issue. See original summary.

oknate’s picture

Version: 9.0.0-rc1 » 9.0.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.

phenaproxima’s picture

Priority: Normal » Major
Issue tags: +Media Initiative, +Triaged Media Initiative issue

On the face of it, this sounds like a major bug. Escalating accordingly until we determine it's not as big of a deal.

xld’s picture

I've come across this issue in a context where I need to filter what users can choose from the media library based on the page they are on.
For the sake of debugging I have tried to remove all filters and the sorting only leaving my one and only contextual filter which I really need but with no luck.

Haven't had a chance to look into the code to see where my contextual gets lost between the /admin/structure/views/view/media_library/edit/widget_table where it works and the select widget on the node page where it doesn't.

Any idea on how to overcome this until it gets fixed would be much appreciated.

gregroper’s picture

I am currently experiencing a similar issue since 8.7.14 and have subsequently updated to 8.9.6 where the issue still happens:

I have a contextual filter configured for the widget display in the media library views, whereby the media is filtered by Authored By and the User ID is provided as a default value (User ID from logged in user) not the URL.

When this contextual filter is applied I cannot use any exposed filters, sort or pagination, as on reloading the form after applying filters (or paging to 2nd page) the selected media image can't be inserted and an uncaught AJAX parse error is thrown (not logged in Drupal):

An AJAX HTTP error occurred.
HTTP Result Code: 200
Debugging information follows.
Path: /media-library?name=&sort_by=created&destination=/admin/content&media_library_opener_id=media_library.opener.field_widget&media_library_allowed_types%5Bportal_image%5D=portal_image&media_library_selected_type=portal_image&media_library_remaining=1&media_library_opener_parameters%5Bfield_widget_id%5D=field_image_s_%3A-field_portal_fields-0-subform&media_library_opener_parameters%5Bentity_type_id%5D=paragraph&media_library_opener_parameters%5Bbundle%5D=image_only&media_library_opener_parameters%5Bfield_name%5D=field_image_s_&media_library_opener_parameters%5Bentity_id%5D=74&hash=F61Z_kJE84DnIJZNRHoZ6dtSPFWMJLzL0SsTksSg8_s&ajax_form=1&views_display_id=widget
StatusText: OK

This issue occurs when adding media to a content type field on a node or even when trying to add default image for the paragraph type that uses the Media Entity Reference field type.

However, without the contextual filter applied, everything works as expected - exposed sorts, filters and pagination all work fine and any image can be selected and successfully inserted.

core 8.9.6
nginx/1.16.1
php7.3.17
5.5.5-10.3.25-MariaDB-1:10.3.25+maria~focal

dystopianblue’s picture

Encountering the same problem as noted by gregroper above. I added the "Media: Authored by" contextual filters to both media widget views and set them to "User ID from logged in user". Initially, the media library displayed correctly on first load, but after toggling between the grid and table views, the views displayed without the contextual filters enforced. I wonder if it's because the contextual filters aren't being passed into the url parameters of the grid/table links, eg. admin/content/media-widget-table/image/all?media_library_opener_id=media_library.opener.field_widget&media_library_allowed_types%5Bimage%5D=image&media_library_selected_type=image&media_library_remaining=10...

Version: 9.0.x-dev » 9.1.x-dev

Drupal 9.0.10 was released on December 3, 2020 and is the final full bugfix release for the Drupal 9.0.x series. Drupal 9.0.x will not receive any further development aside from security fixes. Sites should update to Drupal 9.1.0 to continue receiving regular bugfixes.

Drupal-9-only bug reports should be targeted for the 9.1.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.2.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

chike’s picture

I have same setup as @dystopianblue, I added the 'Media: Authored by' contextual filter with setting 'Provide default value' and 'User ID from logged in user'.

When an authenticated user opens the Library the contextual filter is respected in the default 'Grid' view but if you click on the 'Table' view the filter is ignored and if you click back to the Grid it will loose the initial respect and will also ignore the filter. At this point the filter is totally ignored.

To get going in the meantime I removed the headers.

core 8.9.7

weseze’s picture

Having the same issue...

@chike: Could you explain "To get going in the meantime I removed the headers." What exactly did you do?

chike’s picture

@weseze I removed the HEADER section from the view, entirely, thereby removing the Grid and Table buttons.

weseze’s picture

Also did that, did not work for me... (core 8.9.14)

chike’s picture

You should add a contextual filter 'Authored by' to filter 'User ID from logged in user', for the 'Widget' and 'Widget (table) displays.

Version: 9.1.x-dev » 9.3.x-dev

Drupal 9.1.10 (June 4, 2021) and Drupal 9.2.10 (November 24, 2021) were the last bugfix releases of those minor version series. Drupal 9 bug reports should be targeted for the 9.3.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

akafitty’s picture

This exactly identifies the problem we are experiencing with the Media module. In our situation, we added a contextual filter to only display images that have been added by the logged in user.

On a node edit page, when you click 'Add media' the insert button does nothing and the browser reports AJAX errors if you're selecting media after performing a search or after you apply any filters inside the Media widget. You can select and insert media on page 1 of the widget but the minute you modify the search results the insert fails.

Removing the contextual filters from the widget view solves the problem but then all media results are shown and in our situation that is not ideal.

Any help is greatly appreciated.

akafitty’s picture

Quick update on this… I believe I've found a workable solution here: https://www.drupal.org/project/drupal/issues/3283692

Inside that thread is some custom code that, when added to a custom module, will only display media uploaded by the current logged in user, in all media library widgets. Seems to work so far from the limited testing I've done. I've removed previously added contextual filters.

cheers,
ev

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

anna d’s picture

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.
    if (empty($media_ids)) {
        $media_ids = [0];
    }
    $media_content_data = $query->setWhereGroup('AND');
    $query->addWhere($media_content_data, 'media_field_data.mid', $media_ids, 'IN');
  }
}
solimanharkas’s picture

#19 works for me! Thanks

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

herved’s picture

I think I just stumbled on this issue.

It looks like the form ID changes because it is built in \Drupal\Core\Form\FormBuilder::getFormId according to the views arguments being passed, We get the following form IDs:
- 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?

Edit: I think I will solve it with a service decorator for now (extending MediaLibraryUiBuilder, overriding buildMediaLibraryView).

herved’s picture

Update: #3401726: MediaLibraryUiBuilder service does not properly allow additional contextual filter arguments 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

pavlosdan’s picture

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

tobiasb’s picture

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.