You are here

public function QueryHelper::addResults in Search API 8

Adds a result set to the cache.

Parameters

\Drupal\search_api\Query\ResultSetInterface $results: The search results to cache.

Overrides QueryHelperInterface::addResults

File

src/Utility/QueryHelper.php, line 86

Class

QueryHelper
Provides methods for creating search queries and statically caching results.

Namespace

Drupal\search_api\Utility

Code

public function addResults(ResultSetInterface $results) {
  $search_id = $results
    ->getQuery()
    ->getSearchId();
  $request = $this
    ->getCurrentRequest();
  if (!isset($this->results[$request])) {
    $this->results[$request] = [
      $search_id => $results,
    ];
  }
  else {

    // It's not possible to directly assign array values to an array inside of
    // a \SplObjectStorage object. So we have to first retrieve the array,
    // then add the results to it, then store it again.
    $cache = $this->results[$request];
    $cache[$search_id] = $results;
    $this->results[$request] = $cache;
  }
}