Skip to content
Snippets Groups Projects
Commit db469e4c authored by Jimmy Henderickx's avatar Jimmy Henderickx
Browse files

Issue #3487002 by strykaizer: Facets Exposed Filters - Support different query types

parent 2474e5ed
Branches
Tags
1 merge request!292Issue #3210353 by shkiper, mkalkbrenner, Yujiman85: Issue with browser Back...
Pipeline #342462 canceled
......@@ -57,7 +57,7 @@ function facets_exposed_filters_search_api_query_alter(QueryInterface $query) {
'operator' => $filter->options["facet"]["query_operator"],
'min_count' => $filter->options["facet"]["min_count"],
'missing' => FALSE,
'query_type' => 'search_api_string',
'query_type' => 'search_api_string', // TODO: investigate if this property (query_type) is used.
];
}
}
......
......@@ -117,32 +117,23 @@ class FacetsFilter extends FilterPluginBase {
/** @var \Drupal\facets\FacetInterface $facet */
$facet = $this->getFacet();
$results = [];
$active_facet_values = $this->getActiveFacetValues();
$unprocessed_active_values = $active_facet_values;
foreach ($this->view->filter[$this->options["id"]]->facet_results as $facet_result) {
$val = substr($facet_result["filter"], 1);
$val = substr($val, 0, -1);
$result = new Result($facet, $val, $val, $facet_result["count"]);
if (in_array($val, $active_facet_values)) {
$result->setActiveState(in_array($val, $active_facet_values));
$facet->setActiveItem($val);
if (($key = array_search($val, $unprocessed_active_values)) !== false) {
unset($unprocessed_active_values[$key]);
}
}
$results[] = $result;
}
// Add unprocessed active values to the result. These are selected items that do not match the results anymore.
foreach ($unprocessed_active_values as $val) {
$result = new Result($facet, $val, $val, 0);
$result->setActiveState(TRUE);
$results[] = $result;
}
$facet->setResults($results);
// Load the query_type plugin and execute build.
$qtpm = \Drupal::service('plugin.manager.facets.query_type');
/** @var \Drupal\facets\QueryType\QueryTypeInterface $query_type_plugin */
$query_type_plugin = $qtpm->createInstance(
$facet->getQueryType(),
[
'query' => $this->query->getSearchApiQuery(),
'facet' => $facet,
'results' => $this->view->filter[$this->options["id"]]->facet_results ?? [],
]
);
$query_type_plugin->build();
// When no results are available, we do not need to process the facet or render the form item.
if(!$results) {
if(!$facet->getResults()) {
return;
}
......@@ -248,19 +239,20 @@ class FacetsFilter extends FilterPluginBase {
// Tag query
$this->query->setOption('uses_facets_exposed_filters', TRUE);
// Add any active filters to the query.
$facet = $this->getFacet();
$active_values = $this->getActiveFacetValues();
if ($active_values) {
$field_identifier = $this->configuration["search_api_field_identifier"];
$operator = $this->options["facet"]["query_operator"] ?? 'or';
$filter = $this->query->createConditionGroup($operator, ['facet:' . $field_identifier]);
foreach ($active_values as $active_value) {
$filter->addCondition($field_identifier, $active_value, '=');
}
// We specify the group to be 99, as no default group clashes with
// contextual filters, which are added to group 0.
$this->query->addConditionGroup($filter, 99);
}
$facet->setActiveItems($active_values);
$qtpm = \Drupal::service('plugin.manager.facets.query_type');
/** @var \Drupal\facets\QueryType\QueryTypeInterface $query_type_plugin */
$query_type_plugin = $qtpm->createInstance(
$facet->getQueryType(),
[
'query' => $this->query->getSearchApiQuery(),
'facet' => $facet,
]
);
$query_type_plugin->execute();
}
/**
......@@ -529,6 +521,7 @@ class FacetsFilter extends FilterPluginBase {
'use_hierarchy' => isset($this->options["facet"]["processor_configs"]["hierarchy_processor"]),
'expand_hierarchy' => $this->options["facet"]["expand_hierarchy"] ?? FALSE,
'min_count' => $this->options["facet"]["min_count"] ?? 1,
'widget' => '<nowidget>',
]);
if ($facet->getUseHierarchy()) {
$facet->setHierarchy($this->options["facet"]["hierarchy"], []);
......
......@@ -487,14 +487,18 @@ class Facet extends ConfigEntityBase implements FacetInterface {
$query_types = $facet_source->getQueryTypesForFacet($this);
// Get the widget configured for this facet.
/** @var \Drupal\facets\Widget\WidgetPluginInterface $widget */
$widget = $this->getWidgetInstance();
// Give the widget the chance to select a preferred query type. This is
// needed for widget that have different query type. For example the need
// for a range query.
$widgetQueryType = $widget->getQueryType();
// Allow Facets without widgets (e.g. for facets exposed filters, where views handles the widget part).
$widgetQueryType = NULL;
if($this->widget != "<nowidget>") {
// Get the widget configured for this facet.
/** @var \Drupal\facets\Widget\WidgetPluginInterface $widget */
$widget = $this->getWidgetInstance();
// Give the widget the chance to select a preferred query type. This is
// needed for widget that have different query type. For example the need
// for a range query.
$widgetQueryType = $widget->getQueryType();
}
// Allow widgets to also specify a query type.
$processorQueryTypes = [];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment