You are here

protected static function SavedSearch::getCleanQueryForStorage in Search API Saved Searches 8

Cleans up a search query prior to storing it in a saved search.

Parameters

\Drupal\search_api\Query\QueryInterface $query: The search query.

Return value

\Drupal\search_api\Query\QueryInterface A cleaned-up copy of the search query.

1 call to SavedSearch::getCleanQueryForStorage()
SavedSearch::preCreate in src/Entity/SavedSearch.php
Changes the values of an entity before it is created.

File

src/Entity/SavedSearch.php, line 310

Class

SavedSearch
Provides an entity type for saved searches.

Namespace

Drupal\search_api_saved_searches\Entity

Code

protected static function getCleanQueryForStorage(QueryInterface $query) {

  // Clone the query to not mess with the original.
  $query = clone $query;

  // Search queries created via Views will have a
  // \Drupal\views\ViewExecutable object in the "search_api_view" option
  // as possibly useful metadata for alter hooks, etc. The big problem
  // with that is that those objects will automatically re-execute the
  // view when they are unserialized, which is a huge, completely
  // unnecessary overhead in our case (which might furthermore confuse
  // modules reacting to searches, like Facets – or this one). It's hard
  // to tell what a "proper" solution for this problem would look like,
  // but probably just unsetting this option in the query we save will
  // work well enough in almost all cases.
  $options =& $query
    ->getOptions();
  unset($options['search_api_view']);

  // Reset the result set to its original state (except for warnings and
  // ignored keywords which will usually be set during pre-execute).
  $results = $query
    ->getResults();
  $results
    ->setResultCount(0);
  $results
    ->setResultItems([]);
  $data =& $results
    ->getAllExtraData();
  $data = [];
  unset($data);
  return $query;
}