Facets are lost when using ajax

Created on 25 November 2021, over 3 years ago
Updated 30 May 2023, almost 2 years ago

Problem/Motivation

If the view has ajax enabled, any newly applied (i.e. since the page was reloaded) facets are lost in the saved search. This is because \Drupal\search_api_saved_searches\Plugin\Block\SaveSearch::getCurrentPath() uses the request URI which doesn't include the ajax-added information.

Steps to reproduce

  • Create a view with ajax enabled
  • Add the 'save search' block
  • Add a facet
  • Select the facet to filter the view
  • Save the search and when you later visit it the facet will not be selected

Proposed resolution

Account for ajax views in getCurrentPath by getting the URI in an alternate way. Using the referer works for my case, but open to suggestions.

Remaining tasks

Tests.

User interface changes

None.

API changes

None.

Data model changes

None.

🐛 Bug report
Status

Needs work

Version

1.0

Component

Code

Created by

🇬🇧United Kingdom kimberleycgm

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇬🇧United Kingdom joekers UK

    The query is also affected by this issue - when the saved search is created, the query doesn't always have the conditions applied when using a view that has AJAX enabled.

    As per the comment on [3315790-4] the active query doesn't have the filter applied from the AJAX call.

  • Updated the patch to apply with the latest version.

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.1.4 + Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    47 pass
  • Updated patch to fix deprecated function log.

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.1.4 + Environment: PHP 8.1 & MySQL 8
    last update about 1 year ago
    52 pass
  • Status changed to Needs review about 1 year ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & SQLite 3.27
    last update about 1 year ago
    52 pass, 1 fail
  • 🇦🇹Austria drunken monkey Vienna, Austria

    Thanks a lot!
    I managed to write a test for this, which confirms both the problem and the solution. However, it unfortunately also demonstrates that the problem is even more severe than previously thought: not only is the URL saved for the search incorrect, but also the underlying search query is missing the facet filter. This means the results reported for the saved search will be wrong – and, unfortunately, the proposed patch doesn’t seem to fix this problem. It appears we’d have to construct the query object some other way in this case – though I don’t really know how, as we completely rely on the search query already executed on the current page for the whole “Save search” functionality.

    Anyways, still posting the patch with the failing test. I don’t think I’d commit this in the current form (after commenting out the failing test, of course), though, as it seems to me that this would actually make the situation worse: currently, users might at least notice that the search query is incorrect, when they follow the link. With the proposed patch, the link would point to the correct page, but the reported results would still be wrong. (Though, at least it will usually be more results than desired, not less, which is probably a bit less bad.)

    Any input would be much appreciated.

  • 🇺🇦Ukraine khiminrm

    couldn't apply the patch to latest dev. re-created it

  • 🇺🇦Ukraine khiminrm

    I've noticed that current solution doesn't work when we have initial value for the exposed form's param in a URL. For example we have redirected to the ajax views page from search form on a site from another page. And then we change search keyword which have been submitted by ajax. So I've improved the patch by using query params from the AJax request, but path from the referrer.

  • 🇦🇹Austria drunken monkey Vienna, Austria

    Created an MR (since we now use those for development) and removed some left-over debugging code I spotted.
    Let’s see what the tests say, whether the improvements from #14 fix the problems. However, to me it looks like they might actually make the problem worse at least in some cases, since it’s my understanding that we do want to use the GET params from the referrer at least in some scenarios. If you’re right that we sometimes don’t want to use them, this gets even more complicated, as I’m not sure how we could determine which of them is the case in any given instance.

  • Merge request !21Resolve #3251247 "Fix ajax view v2" → (Open) created by drunken monkey
  • 🇦🇹Austria drunken monkey Vienna, Austria

    Test bot seems to confirm my feeling, let’s see how early it fails without the changes in #14.

  • 🇦🇹Austria drunken monkey Vienna, Austria

    Seems the test fails later without the changes in #14, so keeping the MR like that.
    Still far from a complete solution, though.

Production build 0.71.5 2024