Facets are lost when using ajax

Created on 25 November 2021, almost 3 years ago
Updated 14 June 2024, 3 months 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.
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.

  • 🇬🇧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 11 months 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 8 months ago
    52 pass
  • Status changed to Needs review 7 months ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & SQLite 3.27
    last update 7 months 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

Production build 0.71.5 2024