You are here

public function SavedSearchType::getActiveQuery in Search API Saved Searches 8

Retrieves an active search query that can be saved with this type.

Parameters

\Drupal\search_api\Utility\QueryHelperInterface|null $query_helper: (optional) The query helper service to use. Otherwise, it will be retrieved from the container.

Return value

\Drupal\search_api\Query\QueryInterface|null A search query that was executed in this page request and which can be saved with this saved search type. Or NULL if no such query could be found.

Overrides SavedSearchTypeInterface::getActiveQuery

File

src/Entity/SavedSearchType.php, line 451

Class

SavedSearchType
Provides an entity type for configuring how searches can be saved.

Namespace

Drupal\search_api_saved_searches\Entity

Code

public function getActiveQuery(QueryHelperInterface $query_helper = NULL) {
  if (!$query_helper) {
    $query_helper = \Drupal::service('search_api.query_helper');
  }
  foreach ($query_helper
    ->getAllResults() as $result) {

    // Only match queries with attached search display.
    $query = $result
      ->getQuery();
    $display = $query
      ->getDisplayPlugin();
    if (!$display) {
      continue;
    }

    // Check whether the display matches the ones selected in the options.
    // @todo Replace with \Drupal\search_api\Utility\Utility::matches() once
    //   we can use it (Search API 1.8 dependency).
    $display_id = $display
      ->getPluginId();
    $selected = $this
      ->getOption('displays.selected', []);
    $default = $this
      ->getOption('displays.default', TRUE);
    if (in_array($display_id, $selected) != $default) {
      return $query;
    }
  }
  return NULL;
}