diff --git a/src/Plugin/views/query/SearchApiQuery.php b/src/Plugin/views/query/SearchApiQuery.php index 87d41697..09ea1297 100644 --- a/src/Plugin/views/query/SearchApiQuery.php +++ b/src/Plugin/views/query/SearchApiQuery.php @@ -4,6 +4,7 @@ namespace Drupal\search_api\Plugin\views\query; use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Cache\Cache; +use Drupal\Core\Cache\CacheableDependencyInterface; use Drupal\Core\Database\Query\ConditionInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; @@ -693,13 +694,12 @@ class SearchApiQuery extends QueryPluginBase { * {@inheritdoc} */ public function getCacheContexts() { - $contexts = []; - - foreach ($this->getIndex()->getDatasources() as $datasource) { - $contexts = Cache::mergeContexts($datasource->getListCacheContexts(), $contexts); + $query = $this->getSearchApiQuery(); + if ($query instanceof CacheableDependencyInterface) { + return $query->getCacheContexts(); } - return $contexts; + return []; } /** @@ -708,10 +708,10 @@ class SearchApiQuery extends QueryPluginBase { public function getCacheTags() { $tags = parent::getCacheTags(); - // If the configuration of the search index changes we should invalidate the - // views that show results from this index. - $index_tags = $this->getIndex()->getCacheTagsToInvalidate(); - $tags = Cache::mergeTags($index_tags, $tags); + $query = $this->getSearchApiQuery(); + if ($query instanceof CacheableDependencyInterface) { + $tags = Cache::mergeTags($query->getCacheTags(), $tags); + } return $tags; } diff --git a/src/Query/Query.php b/src/Query/Query.php index e415cc2c..18f545de 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -2,6 +2,9 @@ namespace Drupal\search_api\Query; +use Drupal\Core\Cache\Cache; +use Drupal\Core\Cache\RefinableCacheableDependencyInterface; +use Drupal\Core\Cache\RefinableCacheableDependencyTrait; use Drupal\Core\DependencyInjection\DependencySerializationTrait; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; @@ -15,9 +18,10 @@ use Drupal\search_api\Utility\QueryHelperInterface; /** * Provides a standard implementation for a Search API query. */ -class Query implements QueryInterface { +class Query implements QueryInterface, RefinableCacheableDependencyInterface { use StringTranslationTrait; + use RefinableCacheableDependencyTrait; use DependencySerializationTrait { __sleep as traitSleep; __wakeup as traitWakeup; @@ -696,6 +700,33 @@ class Query implements QueryInterface { return $this->originalQuery ?: clone $this; } + /** + * {@inheritdoc} + */ + public function getCacheContexts() { + $contexts = $this->cacheContexts; + + foreach ($this->getIndex()->getDatasources() as $datasource) { + $contexts = Cache::mergeContexts($datasource->getListCacheContexts(), $contexts); + } + + return $contexts; + } + + /** + * {@inheritdoc} + */ + public function getCacheTags() { + $tags = $this->cacheTags; + + // If the configuration of the search index changes we should invalidate the + // views that show results from this index. + $index_tags = $this->getIndex()->getCacheTagsToInvalidate(); + $tags = Cache::mergeTags($index_tags, $tags); + + return $tags; + } + /** * {@inheritdoc} */