Problem/Motivation

Some background first, since this issue is nuanced:

In Views, there's a Drupal setting "views.ajax_path" that stores the path to the "views.ajax" route. In "views/js/ajax_view.es6.js" the current URL's query parameters are appended to the "views.ajax_path", and that is used to construct the Drupal.ajax settings that elements like pagers use.

This works great if an AJAX view exists on a page on load, i.e. If you have a View that uses the "Page" display and exists on /my-view, visiting /my-view?bar=baz will mean that all subsequent Views AJAX requests will include "?bar=baz".

The problem here is that if you're at another path like /my-route, and load the View via AJAX with a path like"/my-view?bar=baz", the "bar=baz" parameters will be lost, because /my-route does not include those query parameters.

This negatively affected #2962525: Create a field widget for the Media library module because we wanted to pass query parameters to the View when loading it in a modal, but realized that those query parameters were lost on subsequent AJAX requests.

Proposed resolution

When the view is initially constructed, store the query parameters in "drupalSettings.ajaxViews.views_dom_id:*", maybe in a property like "ajax_path". Then use this in "Drupal.views.ajaxView" when constructing the Drupal.ajax settings for each view.

Remaining tasks

Write a patch.

User interface changes

None.

API changes

None.

Data model changes

None.

Comments

samuel.mortenson created an issue. See original summary.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

suit4’s picture

I just have to report, that the function
media_library_views_post_render()
creates a wrong ajax_path if the Drupal installation is running on http://localhost/my-site-name

This setup leads to an ajax action path similar to this one:
/my-site-name/my-site-name/admin/content/media-widget?media_library_widget_id=field_hero_media-&media_library_allowed_types%5Bimage%5D=image&media_library_allowed_types%5Bremote_video%5D=remote_video&media_library_allowed_types%5Bvideo%5D=video&media_library_remaining=1&_wrapper_format=drupal_modal&ajax_form=1&_wrapper_format=drupal_ajax

Obviously, this results in an 404 ajax response.

Maybe this can be fixed when working on this block of code:

 // If the current query contains any parameters we use to contextually
      // filter the view, ensure they persist across AJAX rebuilds.
      // The ajax_path is shared for all AJAX views on the page, but our query
      // parameters are prefixed and should not interfere with any other views.
      // @todo Rework or remove this in https://www.drupal.org/node/2983451
      if (!empty($query)) {
        $ajax_path = &$output['#attached']['drupalSettings']['views']['ajax_path'];
        $parsed_url = UrlHelper::parse($ajax_path);
        $query = array_merge($query, $parsed_url['query']);
        $ajax_path = $parsed_url['path'] . '?' . UrlHelper::buildQuery($query);
        if (isset($query['media_library_remaining'])) {
          $output['#attached']['drupalSettings']['media_library']['selection_remaining'] = (int) $query['media_library_remaining'];
        }
      }

A workaround for the time being:
add local domain, so you can access your Drupal installation via http://my-site-name.local

seanb’s picture

We actually don't need this anymore now the approach for the media library has changed in #3020716: Add vertical tabs style menu to media library.
The AJAX path code is removed in #2981044-37: Unify the grid/table views of the media library. I think we can close this issue when #2981044: Unify the grid/table views of the media library lands.

seanb’s picture

Issue tags: +Media Initiative
pancho’s picture

I think we should leave the issue open. Even if media library won't need it anymore, contrib might still hit this issue. Don't know if it's a bug though or a missing, yet expected feature.

seanb’s picture

Turns out I was wrong, we did actually still need/use that code and did not remove it in #2981044: Unify the grid/table views of the media library.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

vflirt’s picture

Hey, can you help me out to get local setup with the issue and I can work on it.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

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

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

berdir’s picture

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

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.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.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now 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.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now 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.

azinck’s picture

FYI the workaround in media_library_views_post_render introduced this bug: #3308719: Media Library views fail to reset pager when filters are applied

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

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now 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.

Version: 10.1.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, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.