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
- Create a search API page view.
- Add two or more sort criteria to the view, making each one exposed.
- Make sure that "Exposed form in block" = No.
- Save the view.
- Add a facet to the view using the widget "List of checkboxes".
- Enable "Show reset link".
- Disable "Hide reset link when no facet item is selected".
- Disable "Ensure that only one result can be displayed" (I'm not sure if this makes a difference).
- Save the facet.
- Go to the page view of the search.
- You should see "All" at the top of the facet values and some values for the view.
- On the exposed sort form, choose a different sort order.
- 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!