public function SearchApiSortsQueryPreExecute::onQueryPreExecute in Search API sorts 8
Alter the search api query and add our sorting.
Parameters
\Drupal\search_api\Event\QueryPreExecuteEvent $event: The search api sorts manager.
File
- src/
EventSubscriber/ SearchApiSortsQueryPreExecute.php, line 39
Class
- SearchApiSortsQueryPreExecute
- Class that adds the sorting logic to the search api query.
Namespace
Drupal\search_api_sorts\EventSubscriberCode
public function onQueryPreExecute(QueryPreExecuteEvent $event) {
$display = $event
->getQuery()
->getDisplayPlugin();
if (!$display instanceof DisplayInterface) {
// Display for current search page not implemented. To fix this, implement
// the search api display plugin. See ViewsPageDisplay.php for an example.
return;
}
$active_sort = $this->searchApiSortsManager
->getActiveSort($display);
$field = $active_sort
->getFieldName();
$order = $active_sort
->getOrder();
if ($field === NULL) {
// If no field provided, use default field and default order.
$default_sort = $this->searchApiSortsManager
->getDefaultSort($display);
$field = $default_sort
->getFieldName();
$order = $default_sort
->getOrder();
}
$event
->getQuery()
->sort($field, $order);
}