You are here

public function SearchApiSortsManager::getDefaultSort in Search API sorts 8

Returns the default sort field and order.

Parameters

\Drupal\search_api\Display\DisplayInterface $display: The display where the default sort should be returned for.

Return value

\Drupal\search_api_sorts\SortsField An object containing the field and order.

Overrides SearchApiSortsManagerInterface::getDefaultSort

File

src/SearchApiSortsManager.php, line 107

Class

SearchApiSortsManager
Manages search api sorts.

Namespace

Drupal\search_api_sorts

Code

public function getDefaultSort(DisplayInterface $display) {

  // By default use relevance, which will be overridden when defaults are set.
  $default_sort = new SortsField('search_api_relevance', 'desc');
  foreach ($this
    ->getEnabledSorts($display) as $enabled_sort) {
    if ($enabled_sort
      ->getDefaultSort()) {
      $default_sort = new SortsField($enabled_sort
        ->getFieldIdentifier(), $enabled_sort
        ->getDefaultOrder());
    }
  }

  // Allow altering the default sort.
  $this->moduleHandler
    ->alter('search_api_sorts_default_sort', $default_sort, $display);
  return $default_sort;
}