Facets doesn't work correctly with views infinite scroll

Created on 28 July 2021, about 3 years ago
Updated 9 May 2024, 5 months ago

- Selecting one checkbox facets that's checked.

- Using Views Infinite Scroll module, when you click 'load more' it just loads everything in the view and it doesn't respect the facet selection.

I suspect this is probably due to it using ajax and have seen there are various other ajax related issues but nothing about this specific item.

🐛 Bug report
Status

Fixed

Version

3.0

Component

Code

Created by

🇺🇸United States khadelk

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.

  • 🇮🇳India ankithashetty Karnataka, India

    +1 to this issue.

    Observations:

    Create a view + use <a href="https://www.drupal.org/project/views_infinite_scroll">views_infinite_scroll</a> module (this requires Ajax to be enabled on view) + <a href="https://www.drupal.org/project/facets">facets</a> module (even if the module is enabled on site, we need to not have to add a facet to this view)

    After deep analysis of this, found that this is happening due to this line https://git.drupalcode.org/project/facets/-/blob/3.0.x/src/Controller/FacetsViewsAjaxController.php?ref_type=heads#L51 from patch #3306634: Integrate with better_exposed_filters for AJAX .

    When the user clicks on 'load more' in the view:

    1. page key is stripped from query parameters in Controller/FacetsViewsAjaxController.php
    2. Because of this, we fail to get the 'page' data from .
    3. And due to this, current_page for the view will not get valid value here . It will always be 0
    4. And thus, these conditions will always fail

    This is even observed for a view that has no facets added. It's a simple view with 'load more'. The facets module is enabled on the site. Due to this, FacetsViewsAjaxController.php will get fired on click of 'load more'.

  • 🇮🇳India ankithashetty Karnataka, India
  • 🇮🇳India ankithashetty Karnataka, India

    Upgrading it to major to get some attention here as this could be a regression. We can downgrade it if it's a minor bug.

  • 🇮🇳India ankithashetty Karnataka, India

    Have another update here:

    CASE 1:
    Looks like it works perfectly fine with views_infinite_scroll module + facets module. The Ajax request is POST here.

    CASE 2:
    The problem is when we have views_infinite_scroll module (this requires Ajax to be enabled on view) + facets module (even if the module is enabled on site, we need to not have to add a facet to this view) + views_ajax_get module enabled & configured in the sites. Here, the views Ajax requests are changed to GET!

    Observations:

    • Including the steps mentioned in #3, noticed that all data like 'page' exists only as query param and will not be part of the post request data as the views Ajax requests are changed to GET by views_ajax_get module
    • This page will be removed from query param by facets.
    • With CASE 1, we don't find this problem, as the required details like page exist in POST request which gets added back in core here /views/src/Controller/ViewAjaxController.php#L171

    Tried and tested this on Drupal core: 9.5.11 and the latest compatible versions of all three modules.

  • 🇮🇳India ankithashetty Karnataka, India

    Suggestions/workaround:

    When you have a site with modules views_infinite_scroll, facets and views_ajax_get:

    1. Patch from here https://www.drupal.org/project/drupal/issues/956186#comment-14991435 📌 Allow AJAX to use GET requests Fixed for Drupal 9.5.x
    2. A small tweak in facets/src/Controller/FacetsViewsAjaxController.php file under function facetsRemoveQueryParams
      protected function facetsRemoveQueryParams(Request $request) {
        if ($request->isXmlHttpRequest()) {
          // @todo replace hardcoded 'f' by facet setting.
          foreach (['f', 'page', 'fulltext'] as $key) {
            if ($request->query->has($key)) {
              if ($key === 'page' && !$request->request->has($key)) {
                $page = $request->query->get($key);
                $request->request->add([$key => $page]);
              }
              $request->query->remove($key);
            }
          }
        }
      }
  • Status changed to Needs review 12 months ago
  • Open on Drupal.org →
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update 12 months ago
    Waiting for branch to pass
  • 🇮🇳India ankithashetty Karnataka, India

    Here is a patch for review.

    As mentioned above, to work with a site having modules views_infinite_scroll, facets and views_ajax_get:

    1. Patch from here https://www.drupal.org/project/drupal/issues/956186#comment-14991435 📌 Allow AJAX to use GET requests Fixed for Drupal 9.5.x
    2. Along with the patch shared here.

    Testing results:
    Without both patches: - error

    With just a core patch: - doesn't work
    https://www.drupal.org/files/issues/2023-10-05/Screen%20Recording%202023...

    With both the patches: - works as expected
    https://www.drupal.org/files/issues/2023-10-05/Screen%20Shot%202023-10-0...

  • Status changed to RTBC 12 months ago
  • 🇩🇪Germany mkalkbrenner 🇩🇪

    Even if I would prefer to rewrite the entire parameter handling according to the changes symfony introduced, the patch looks good for now.

  • Status changed to Fixed 12 months ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

  • Status changed to Fixed 5 months ago
  • 🇪🇸Spain david.carmona

    Hi there,

    I am reopening this issue because I have been able to verify that infinite scroll with facets using ajax in the view still does not work, even with the patch in #8. 🐛 Facets doesn't work correctly with views infinite scroll Active

    I have been testing the patch and debugging the code, after adding a key 'page' to the request with the page number, we are still removing the key 'page' from the query:$request->query->remove($key), this causes us to lose the results page that should be displayed from the query and does nothing after the ajax call is executed.

    Here I leave the refactored patch that I have been able to verify is working with:

Production build 0.71.5 2024