You are here

class SearchApiSortsManager in Search API sorts 8

Manages search api sorts.

Hierarchy

Expanded class hierarchy of SearchApiSortsManager

1 file declares its use of SearchApiSortsManager
SearchApiSortsManagerTest.php in tests/src/Unit/SearchApiSortsManagerTest.php
1 string reference to 'SearchApiSortsManager'
search_api_sorts.services.yml in ./search_api_sorts.services.yml
search_api_sorts.services.yml
1 service uses SearchApiSortsManager
search_api_sorts.manager in ./search_api_sorts.services.yml
Drupal\search_api_sorts\SearchApiSortsManager

File

src/SearchApiSortsManager.php, line 15

Namespace

Drupal\search_api_sorts
View source
class SearchApiSortsManager implements SearchApiSortsManagerInterface {
  use ConfigIdEscapeTrait;

  /**
   * Current Request.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $currentRequest;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The search api sorts field storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $searchApiSortsFieldStorage;

  /**
   * The search api display manager.
   *
   * @var \Drupal\search_api\Display\DisplayPluginManagerInterface
   */
  protected $searchApiDisplayManager;

  /**
   * SearchApiSortsManager constructor.
   *
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack, containing the current request.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\search_api\Display\DisplayPluginManagerInterface $searchApiDisplayManager
   *   The search api display manager.
   */
  public function __construct(RequestStack $request_stack, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, DisplayPluginManagerInterface $searchApiDisplayManager) {
    $this->currentRequest = $request_stack
      ->getCurrentRequest();
    $this->searchApiSortsFieldStorage = $entity_type_manager
      ->getStorage('search_api_sorts_field');
    $this->moduleHandler = $module_handler;
    $this->searchApiDisplayManager = $searchApiDisplayManager;
  }

  /**
   * {@inheritdoc}
   */
  public function getActiveSort(DisplayInterface $display) {
    $order = strtolower($this->currentRequest
      ->get('order')) === 'desc' ? 'desc' : 'asc';
    $active_sort = new SortsField($this->currentRequest
      ->get('sort'), $order);

    // Allow altering the active sort (if there is an active sort).
    if ($active_sort
      ->getFieldName()) {
      $this->moduleHandler
        ->alter('search_api_sorts_active_sort', $active_sort, $display);
    }
    return $active_sort;
  }

  /**
   * {@inheritdoc}
   */
  public function getEnabledSorts(DisplayInterface $display) {
    return $this->searchApiSortsFieldStorage
      ->loadByProperties([
      'status' => TRUE,
      'display_id' => $this
        ->getEscapedConfigId($display
        ->getPluginId()),
    ]);
  }

  /**
   * Returns all sort fields for a given search api display.
   *
   * @param \Drupal\search_api\Display\DisplayInterface $display
   *   The display where the sorts should be returned for.
   *
   * @return \Drupal\Core\Entity\EntityInterface[]
   *   An array containing sort fields for the given search display/
   */
  protected function getSorts(DisplayInterface $display) {
    return $this->searchApiSortsFieldStorage
      ->loadByProperties([
      'display_id' => $this
        ->getEscapedConfigId($display
        ->getPluginId()),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function cleanupSortFields(IndexInterface $index) {
    foreach ($this->searchApiDisplayManager
      ->getInstances() as $display) {
      if ($display
        ->getIndex() instanceof IndexInterface && $index
        ->id() === $display
        ->getIndex()
        ->id()) {
        foreach ($this
          ->getSorts($display) as $search_api_sorts_field) {

          // Dummy field therefore the index has no field.
          if ($search_api_sorts_field
            ->getFieldIdentifier() === 'search_api_relevance') {
            continue;
          }
          $field = $index
            ->getField($search_api_sorts_field
            ->getFieldIdentifier());
          if ($field === NULL) {
            $search_api_sorts_field
              ->delete();
          }
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigIdEscapeTrait::getEscapedConfigId protected function Escape a config id which can be used to save as a config entity.
ConfigIdEscapeTrait::getOriginalConfigId protected function Get original config id after loading a config entity using an escaped id.
SearchApiSortsManager::$currentRequest protected property Current Request.
SearchApiSortsManager::$moduleHandler protected property The module handler.
SearchApiSortsManager::$searchApiDisplayManager protected property The search api display manager.
SearchApiSortsManager::$searchApiSortsFieldStorage protected property The search api sorts field storage.
SearchApiSortsManager::cleanupSortFields public function Remove all deleted sorts for the given index. Overrides SearchApiSortsManagerInterface::cleanupSortFields
SearchApiSortsManager::getActiveSort public function Returns the active sort field and order for a given search api display. Overrides SearchApiSortsManagerInterface::getActiveSort
SearchApiSortsManager::getDefaultSort public function Returns the default sort field and order. Overrides SearchApiSortsManagerInterface::getDefaultSort
SearchApiSortsManager::getEnabledSorts public function Returns all enabled sort fields for a given search api display. Overrides SearchApiSortsManagerInterface::getEnabledSorts
SearchApiSortsManager::getSorts protected function Returns all sort fields for a given search api display.
SearchApiSortsManager::__construct public function SearchApiSortsManager constructor.