Reset checkbox conflicts with views exposed filters/sort.

Created on 10 January 2023, almost 2 years ago
Updated 2 May 2023, over 1 year ago

Problem/Motivation

Under a certain set of conditions (listed below), when you select an exposed sort on the search api results view, no results are shown, due to the presence of {facet-condition}=reset_all in the search query.

Steps to reproduce

  1. Create a search API page view.
  2. Add two or more sort criteria to the view, making each one exposed.
  3. Make sure that "Exposed form in block" = No.
  4. Save the view.
  5. Add a facet to the view using the widget "List of checkboxes".
  6. Enable "Show reset link".
  7. Disable "Hide reset link when no facet item is selected".
  8. Disable "Ensure that only one result can be displayed" (I'm not sure if this makes a difference).
  9. Save the facet.
  10. Go to the page view of the search.
  11. You should see "All" at the top of the facet values and some values for the view.
  12. On the exposed sort form, choose a different sort order.
  13. The view will no longer show any results.

Proposed resolution

The problem here is in the Build function of the LinksWidget class, at:

$result_item = new Result($facet, 'reset_all', $this->getConfiguration()['reset_text'], $max_items);
$result_item->setActiveState(FALSE);
$result_item->setUrl($url);

The widget is adding {facet-condition}=reset_all to the query for the given facet condition, whenever the exposed search is selected (assuming you didn't already select a value for the facet). Since no values for the field meet that condition, the results are empty.

I tried fixing this function myself by working with those lines of code, to no avail. In particular, removing the last line that sets the URL works insofar as the query is no longer set with the the reset_all value, but then the "ALL" checkbox becomes incorrectly styled / handled by JS.

The request here is for this code to be fixed to not alter the query itself, but still let the ALL checkbox get styled and handled correctly.

Workaround

I am altering the search api query to alter the filter condition where the condition value is set to "reset_all" as follows:

/**
 * Implements hook_search_api_query_alter().
 */
function teradici_core_search_api_query_alter(QueryInterface $query) {
  $condition_groups = &$query->getConditionGroup()->getConditions();
  foreach ($condition_groups as &$condition_group) {
    if ($condition_group instanceof \Drupal\search_api\Query\ConditionGroupInterface) {
      foreach ( $condition_group->getConditions() as $condition) {
        $condition_value = $condition->getValue();
        if ($condition_value == 'reset_all') {
          $condition->setOperator('NOT IN'); // This is easier than trying to remove the condition altogether.
        }
      }
    }
  }
}

This works, but we are still left with condition in the query that ideally is not inserted in the first place. In my case this is worsened by the fact that the query parameters are shown in the URL to the user via the "Views AJAX history" module.

Thank you!

🐛 Bug report
Status

Needs review

Version

3.0

Component

Code

Created by

🇨🇦Canada Shiraz Dindar Sooke, BC

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.

  • First commit to issue fork.
  • Open on Drupal.org →
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update over 1 year ago
    Waiting for branch to pass
  • @andrewbelcher opened merge request.
  • Status changed to Needs review over 1 year ago
  • 🇬🇧United Kingdom andrewbelcher

    Have opened a MR with a fix for this. The issue is actually due to the checkbox that gets exposed and therefore submits a value when you submit the views exposed form. To fix this, I am excluding the filter value from the checkbox, which acts as a reset in the URL.

  • 🇬🇧United Kingdom maseyuk

    I was getting the same under a similar situation.

    I've got a fulltext search field along with some facets on a view. When doing a search via the fulltext search field it was populating all facets without a value set into the URl with "reset_all" causing no results to be found

    The MR change seems to fix it

  • Open on Drupal.org →
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update over 1 year ago
    Waiting for branch to pass
  • 🇬🇧United Kingdom maseyuk

    And this is a patch based from it

Production build 0.71.5 2024