diff --git a/src/Plugin/views/cache/SearchApiCachePluginTrait.php b/src/Plugin/views/cache/SearchApiCachePluginTrait.php index 9f6e7100..6e056206 100644 --- a/src/Plugin/views/cache/SearchApiCachePluginTrait.php +++ b/src/Plugin/views/cache/SearchApiCachePluginTrait.php @@ -4,6 +4,7 @@ namespace Drupal\search_api\Plugin\views\cache; use Drupal\Component\Utility\Crypt; use Drupal\Core\Cache\Cache; +use Drupal\Core\Cache\CacheableDependencyInterface; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Cache\Context\CacheContextsManager; use Drupal\search_api\Plugin\views\query\SearchApiQuery; @@ -188,6 +189,9 @@ trait SearchApiCachePluginTrait { ], ]; + // Vary the results key by the cache contexts of the display handler. + // These cache contexts are calculated when the view is saved in the Views + // UI and stored in the view config entity. $display_handler_cache_contexts = $this->displayHandler ->getCacheMetadata() ->getCacheContexts(); diff --git a/src/Plugin/views/query/SearchApiQuery.php b/src/Plugin/views/query/SearchApiQuery.php index 09ea1297..8eb7cd9d 100644 --- a/src/Plugin/views/query/SearchApiQuery.php +++ b/src/Plugin/views/query/SearchApiQuery.php @@ -261,7 +261,7 @@ class SearchApiQuery extends QueryPluginBase { if ($display_type === 'rest_export') { $display_type = 'rest'; } - $this->query->setSearchId("views_$display_type:" . $view->id() . '__' . $view->current_display); + $this->query->setSearchId("views_$display_type:" . $view->id() . '__' . $display->display['id']); $this->query->setOption('search_api_view', $view); } catch (\Exception $e) { diff --git a/src/Query/Query.php b/src/Query/Query.php index 18f545de..138b9969 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -704,6 +704,10 @@ class Query implements QueryInterface, RefinableCacheableDependencyInterface { * {@inheritdoc} */ public function getCacheContexts() { + // Call the pre-execute method to ensure that processors and modules have + // had the chance to alter the query and modify the cacheability metadata. + $this->preExecute(); + $contexts = $this->cacheContexts; foreach ($this->getIndex()->getDatasources() as $datasource) { @@ -717,6 +721,10 @@ class Query implements QueryInterface, RefinableCacheableDependencyInterface { * {@inheritdoc} */ public function getCacheTags() { + // Call the pre-execute method to ensure that processors and modules have + // had the chance to alter the query and modify the cacheability metadata. + $this->preExecute(); + $tags = $this->cacheTags; // If the configuration of the search index changes we should invalidate the @@ -727,6 +735,17 @@ class Query implements QueryInterface, RefinableCacheableDependencyInterface { return $tags; } + /** + * {@inheritdoc} + */ + public function getCacheMaxAge() { + // Call the pre-execute method to ensure that processors and modules have + // had the chance to alter the query and modify the cacheability metadata. + $this->preExecute(); + + return $this->cacheMaxAge; + } + /** * {@inheritdoc} */