Facets breaks all AJAX views that uses pagers even without facets

Created on 5 August 2023, 11 months ago
Updated 21 June 2024, 6 days ago

Problem/Motivation

The facets module breaks all AJAX views pagers even those that do not use facets.

Since the module overrides the base ajaxView method at

  public function onRouteAlter(RouteBuildEvent $event) {
    $collection = $event->getRouteCollection();
    if ($route = $collection->get('views.ajax')) {
      $route->setDefault('_controller', '\Drupal\facets\Controller\FacetsViewsAjaxController::ajaxView');
    }
  }

And the first line in the ajaxView method of the facets module is

$this->facetsRemoveQueryParams($request);

So it does not check if the view even has facets or not, just simply removes the "page" from all ajax views request.

This of course breaks all AJAX views that tries to paginate with AJAX.

Steps to reproduce

- Install the 3.x version of the facets module.
- Add any AJAX enabled view with a pager.
- The pager does not work.

(For example this breaks pagination on media library views as well)

Proposed resolution

Removing that one line
$this->facetsRemoveQueryParams($request);
fixes the issue, but I guess it was there for a reason.
So a better approach could be to detect if the views in question is a search api (or facet containing view) and then conditionally do the removal.
This is new in branch 3.x as I see so 2.x is not affected.

๐Ÿ› Bug report
Status

Fixed

Version

3.0

Component

Code

Created by

๐Ÿ‡ญ๐Ÿ‡บHungary nagy.balint

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @nagy.balint
  • ๐Ÿ‡ญ๐Ÿ‡บHungary nagy.balint
  • Status changed to Needs work 11 months ago
  • ๐Ÿ‡ญ๐Ÿ‡บHungary nagy.balint

    Here is a quickfix for the problem.

    Of course this will not make it work when facets are present on the view.

    But it will fix normal views like the media library which normally has no facets but has to work with ajax most of the time.

  • Open on Drupal.org โ†’
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update 11 months ago
    Waiting for branch to pass
  • ๐Ÿ‡ง๐Ÿ‡ชBelgium herved

    I'm facing the same issue.
    I believe this especially shows on Drupal 10.1 as it now passes the page parameter directly (possibly from ๐Ÿ“Œ Allow AJAX to use GET requests Fixed ?)

    I would like to understand why that code in facets is there and find a proper fix but I cannot even get ajax facets to work at all...
    The only ajax-related test in \Drupal\Tests\facets\FunctionalJavascript\AjaxBehaviorTest doesn't seem to actually use ajax...
    Things do not make any sense to me TBH...
    PS: I created ๐Ÿ› How to make AJAX work with facets 3? (issue with isRenderedInCurrentRequest) Active

  • ๐Ÿ‡จ๐Ÿ‡ดColombia Freddy Rodriguez Bogotรก

    +1

  • ๐Ÿ‡ง๐Ÿ‡ชBelgium herved

    I found an issue with patch #3 when accessing /views/ajax:

    Warning: array_flip(): Can only flip string and integer values, entry skipped in Drupal\Core\Entity\EntityStorageBase->loadMultiple() (line 278 of core/lib/Drupal/Core/Entity/EntityStorageBase.php).

    Drupal\Core\Entity\EntityStorageBase->loadMultiple(Array) (Line: 262)
    Drupal\Core\Entity\EntityStorageBase->load(NULL) (Line: 26)
    Drupal\facets\Controller\FacetsViewsAjaxController->ajaxView(Object)
    call_user_func_array(Array, Array) (Line: 123)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 583)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 166)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 74)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

    It needs to check if the view_name param is present.

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany Kevin Hoffmann Berlin

    +1 for the patch

    Also as a side note facetsRemoveQueryParams() contains

    This only applies when the method is POST.

    in its docstring. That being said its not even checking that condition itself and applies also for GET requests on my side. Aside from the fact that we remove keys from $request->query which afaik is related to GET requests to begin with.

  • ๐Ÿ‡ง๐Ÿ‡ชBelgium herved

    @7 Indeed I also noticed this. This commit changed $request->getMethod() === Request::METHOD_POST to $request->isXmlHttpRequest(). Perhaps it wasn't intentional ?
    Again, I have no clue how AJAX facets are supposed to work since I can't make them work myself despite trying so I can't even debug or understand what this code is all about.

    In the mean time, here is a fix for #6.

  • Open on Drupal.org โ†’
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update 9 months ago
    Waiting for branch to pass
  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 10.1.x + Environment: PHP 8.1 & MariaDB 10.3.22
    last update 9 months ago
    420 pass, 2 fail
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom jefflogan

    Thanks for the patch, this fixed my issue with the broken pager on ajax views.
    +1 RTBC

  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 10.1.x + Environment: PHP 8.1 & MariaDB 10.3.22
    last update 9 months ago
    428 pass
  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 10.1.x + Environment: PHP 8.2 & MySQL 8
    last update 9 months ago
    428 pass
  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update 9 months ago
    428 pass
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany mkalkbrenner ๐Ÿ‡ฉ๐Ÿ‡ช

    AJAX in facets 3.0.x is not yet finished. We waited for https://www.drupal.org/node/3193798 โ†’

    And Drupal now uses a newer symfony version which changed the request parameter handling.

  • Status changed to RTBC 9 months ago
  • ๐Ÿ‡ท๐Ÿ‡ดRomania claudiu.cristea Arad ๐Ÿ‡ท๐Ÿ‡ด

    Tested and the error is fixed now.

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany mkalkbrenner ๐Ÿ‡ฉ๐Ÿ‡ช

    Tested and the error is fixed now

    Do you mean, that the code in 3.0.x dev works now since due to latest commits and that no further patch is needed. Or did you RTBC this patch here?

    Did you test the patch with the latest dev version?

  • ๐Ÿ‡ท๐Ÿ‡ดRomania claudiu.cristea Arad ๐Ÿ‡ท๐Ÿ‡ด

    @mkalkbrenner,

    We've applied the patch from #8 against 3.0.0-beta1 and that fixed the bug described in this issue (i.e. all ajax pagers are broken). I've RTBCed this patch because it fixes this issue.

  • Status changed to Needs work 8 months ago
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany mkalkbrenner ๐Ÿ‡ฉ๐Ÿ‡ช

    The issue description seems to redundant to ๐Ÿ› Facets doesn't work correctly with views infinite scroll Active . That issue has been fixed and is already committed to the 3.0.x branch.
    So I'm not sure if the patch proposed here is still required or if it needs adjustments due to the changes in 3.0.x-dev. So I won't commit it as it is.

  • ๐Ÿ‡ง๐Ÿ‡ชBelgium herved

    Hello, thanks for the update.

    For me it doesn't work, I tried both the latest 3.0.x (b71823b), and 3.0-beta1 + patch #8 from #3225764.
    I'm on core 10.1.4, without views_ajax_get.

    It looks like views attempts to get the current page from \Drupal\Core\Pager\PagerParameters::getPagerParameter which retrieves it from $request->query->get('page', ''); while \Drupal\facets\Controller\FacetsViewsAjaxController::facetsRemoveQueryParams removes the page param from $request->query.

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany mkalkbrenner ๐Ÿ‡ฉ๐Ÿ‡ช

    FacetsViewsAjaxController has been introduced by older versions of facets. I wonder if it should be removed entirely.

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany mkalkbrenner ๐Ÿ‡ฉ๐Ÿ‡ช

    The code is about having and remove these GET parameters because Views AJAX based on POST. But nowadays Views AJAX bases on GET.
    3.0.x already requires Drupal 10.1. So should not care about POST anymore.

  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands undersound3

    FYI

    After upgrading from drupal 9.5.10 to 10.1.3 facet items were not showing up anymore in the Facet Summary block (not sure if related but writing down here for completeness sake).

    I was then updating this module from 2.0.6 to 3.0.0-beta1 to see if it fixed that. It did. But then pagination in a "Media Browser Entity" browser did not work anymore. Applying this patch fixed that problem and now pagination works again.

  • ๐Ÿ‡ง๐Ÿ‡ชBelgium StryKaizer Belgium

    Attached patch will remove all leftover ajax stuff specific for facet blocks.

    The way to go when you want facets + ajax is using the new better exposed filters way, see #3394955

  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update 8 months ago
    425 pass
  • Status changed to Needs review 8 months ago
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany mkalkbrenner ๐Ÿ‡ฉ๐Ÿ‡ช

    Does the commit of ๐Ÿ› Facets doesn't work correctly with views infinite scroll Active be revised somehow?

  • Status changed to RTBC 8 months ago
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany mkalkbrenner ๐Ÿ‡ฉ๐Ÿ‡ช
  • Status changed to Fixed 8 months ago
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany mkalkbrenner ๐Ÿ‡ฉ๐Ÿ‡ช
  • Automatically closed - issue fixed for 2 weeks with no activity.

  • Status changed to Fixed 8 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States energee

    Rolled against 3.0.0-beta1

  • ๐Ÿ‡ง๐Ÿ‡ทBrazil wfraga

    @energee

    I'm getting this error when applying 3379445-25.patch

    Drupal core: 10.1.6
    PHP: 8.1.2
    Facets: 3.0.0-beta1

    Drupal\Component\Plugin\Exception\PluginNotFoundException: The "facets" plugin does not exist. Valid plugin IDs for Drupal\better_exposed_filters\Plugin\BetterExposedFiltersWidgetManager are: default, bef_links, bef_sliders, bef_datepicker, bef_single, bef_hidden, bef in Drupal\Core\Plugin\DefaultPluginManager->doGetDefinition() (line 53 of core/lib/Drupal/Component/Plugin/Discovery/DiscoveryTrait.php).

  • ๐Ÿ‡ง๐Ÿ‡ชBelgium bramvandenbulcke

    Patch 3379445-25.patch is working fine for me.

    @wfraga: is Facets 3.x enabled (installed) on your website?

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada joseph.olstad

    Friendly nudge to please tag a release.

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada Noregrebt

    Patch 25 as well as dev release fixes the problem for me but breaks the actual facets.

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada Noregrebt

    Resaving each facet has now fixed it. Thanks for the patch!

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada joseph.olstad

    @mkalkbrenner , it would really help us all if you could please tag and release.

  • ๐Ÿ‡ง๐Ÿ‡ชBelgium bramvandenbulcke

    A follow up from my side: I used patch 3379445-25.patch and thought everything was working fine but the count of the pager wasn't working with the patch installed. For example: select a facet selection with a result of 3 pages and when selecting the next page the pager goes to 9 pages.

    As a temporary fix I disabled AJAX on these facet views.

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada joseph.olstad

    ok, thanks for the followup @bramvandenbulcke
    I postponed :
    ๐ŸŒฑ Tag and release to fix - Facets breaks all AJAX views that uses pagers even without facets RTBC
    Patch #25 and the pager, some remaining issue to work out.

  • ๐Ÿ‡ซ๐Ÿ‡ทFrance Soubi

    With the patch #25 and views_infinite_scroll installed, I had to make my own url_processor plugin QueryString because the Request object used in the initializeActiveFilters function doesn't have any data to get the active filters when Ajax is enabled.

        if ($this->request->isXmlHttpRequest()) {
          // $url_parameters = $this->request->request;
          $url_parameters = new InputBag($_GET);
        }
        else {
          $url_parameters = $this->request->query;
        }
    

    $url_parameters was always empty.

    I replace it with

        if ($this->request->isXmlHttpRequest()) {
          $url_parameters = new InputBag($_GET);
        }
        else {
          $url_parameters = $this->request->query;
        }

    in my custom url_processor plugin.

  • ๐Ÿ‡ต๐Ÿ‡ฑPoland Graber

    This is not released yet? Seriously?
    No further comments.

  • ๐Ÿ‡ง๐Ÿ‡ชBelgium BramDriesen Belgium ๐Ÿ‡ง๐Ÿ‡ช

    Facets 3.0 is undergoing a major overhaul. What @StryKaizer is working on is not yet releasable.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia chakkche

    +1 for #35.

    Filters are not being respected with latest dev or patch from #25. I have to overwrite initializeActiveFilters in my custom url proccessor. i have made similar change mentioned in #35

  • I'm currently using version 3.0.0-beta1 and encountered an issue with broken AJAX pagination.
    Applying Patch #25 resolved the problem.
    Thank you for this patch!

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States tyler.hastain

    I'm also currently using version 3.0.0-beta1 and was having the broken pagination issue when AJAX was turned on.
    Applying Patch #25 resolved the problem for me as well.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States tyler.hastain

    We currently use version 3.0.0-beta1 and ran into the same AJAX pagination issue.
    Applying Patch #25 resolved the problem for us as well.

  • ๐Ÿ‡บ๐Ÿ‡ฆUkraine paulrad

    Here's the patch #25 with the Facets plugin still available for the Better Exposed Filters.
    Version - 3.0.0-beta1

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada joseph.olstad

    3.0.0-beta1 is far behind

    I recommend someone tag 3.0.0-beta2 off of 3.0.x-dev

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada liquidcms

    To fix facets break ajax paging (since upgrading core from 10.0 to 10.2); i upgraded Facets fro 3.0.0-beta1 to today's 3.0-dev.

    Had to remove patch from here: ๐Ÿ› Disable updating the facets after each facet selection for Apply/Reset facet buttons block. Needs work as it no longer applies. Dev release does fix the issue with ajax paging however i no longer have a Views facet plugin. All views which had facet filters now show broken/missing handler.

    I'll try going back to -beta1 and just using latest patch from here.

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada liquidcms

    Patch from #41 on -beta1 rather than latest -dev fixes pager as well as facet exposed filters. Still doesn't apply with ๐Ÿ› Disable updating the facets after each facet selection for Apply/Reset facet buttons block. Needs work but i'll redo that patch.

Production build 0.69.0 2024