diff --cc core/modules/views/src/Plugin/views/display/DisplayPluginBase.php index 22e8d16,526014b..0000000 --- a/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php +++ b/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php @@@ -13,7 -13,7 +13,8 @@@ use Drupal\Core\Form\FormStateInterface use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Theme\Registry; +use Drupal\views\Form\ViewsForm; + use Drupal\views\Plugin\CacheablePluginInterface; use Drupal\views\Plugin\views\area\AreaPluginBase; use Drupal\views\ViewExecutable; use Drupal\views\Plugin\views\PluginBase; @@@ -2287,6 -2214,45 +2288,50 @@@ abstract class DisplayPluginBase extend } /** + * Determines the available cache metadata from each handler/plugin. + * + * @return array + * Returns an array: - * - first value: (boolean) Is the display actually cacheable. - * - second value: An array of cache contexts the view output varies by. ++ * - first value: An array of cache contexts the view output varies by. ++ * - second value: (boolean) Is the display actually cacheable. + */ + public function getCacheMetadata () { + $cacheable = TRUE; + $cache_contexts = []; + + // Iterate over ordinary views plugins. + foreach (Views::getPluginTypes('plugin') as $plugin_type) { + $plugin = $this->getPlugin($plugin_type); + if ($plugin instanceof CacheablePluginInterface) { + $cache_contexts = array_merge($cache_contexts, $plugin->getCacheContexts()); + $cacheable &= $plugin->isCacheable(); + } + else { + $cacheable = FALSE; + } + } + + // Iterate over all handlers. Note that at least the argument handler will + // need to ask all its subplugins. + foreach (array_keys(Views::getHandlerTypes()) as $handler_type) { + $handlers = $this->getHandlers($handler_type); + foreach ($handlers as $handler) { + if ($handler instanceof CacheablePluginInterface) { + $cache_contexts = array_merge($cache_contexts, $handler->getCacheContexts()); + $cacheable &= $handler->isCacheable(); + } + } + } + ++ /** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache_plugin */ ++ if ($cache_plugin = $this->getPlugin('cache')) { ++ $cache_plugin->alterCacheMetadata($cache_contexts, $cacheable); ++ } ++ + return [$cache_contexts, $cacheable]; + } + + /** * When used externally, this is how a view gets run and returns * data in the format required. * diff --git a/core/modules/views/src/Plugin/views/argument/ArgumentPluginBase.php b/core/modules/views/src/Plugin/views/argument/ArgumentPluginBase.php index f02dd4e..9b0285b 100644 --- a/core/modules/views/src/Plugin/views/argument/ArgumentPluginBase.php +++ b/core/modules/views/src/Plugin/views/argument/ArgumentPluginBase.php @@ -1189,6 +1189,7 @@ public function isCacheable() { $result &= $plugin->isCacheable(); } + // Summaries use style plugins. if (($plugin = $this->getPlugin('style')) && $plugin instanceof CacheablePluginInterface) { $result &= $plugin->isCacheable(); } diff --git a/core/modules/views/src/Plugin/views/argument_default/QueryParameter.php b/core/modules/views/src/Plugin/views/argument_default/QueryParameter.php index f6d1bba..337b722 100644 --- a/core/modules/views/src/Plugin/views/argument_default/QueryParameter.php +++ b/core/modules/views/src/Plugin/views/argument_default/QueryParameter.php @@ -95,9 +95,7 @@ public function isCacheable() { * {@inheritdoc} */ public function getCacheContexts() { - $contexts = ['cache.context.url']; - - return $contexts; + return ['cache.context.url']; } } diff --git a/core/modules/views/src/Plugin/views/argument_default/Raw.php b/core/modules/views/src/Plugin/views/argument_default/Raw.php index 6151277..3572790 100644 --- a/core/modules/views/src/Plugin/views/argument_default/Raw.php +++ b/core/modules/views/src/Plugin/views/argument_default/Raw.php @@ -114,9 +114,7 @@ public function isCacheable() { * {@inheritdoc} */ public function getCacheContexts() { - $contexts = ['cache.context.url']; - - return $contexts; + return ['cache.context.url']; } } diff --git a/core/modules/views/src/Plugin/views/cache/CachePluginBase.php b/core/modules/views/src/Plugin/views/cache/CachePluginBase.php index d747283..95e5a29 100644 --- a/core/modules/views/src/Plugin/views/cache/CachePluginBase.php +++ b/core/modules/views/src/Plugin/views/cache/CachePluginBase.php @@ -367,6 +367,17 @@ protected function getCacheTags() { return $tags; } + /** + * Change the determined cache metadata on saving of the view. + * + * @param array $cache_contexts + * An array of cache contexts the view output varies by. + * @param bool $cacheable + * Is the display actually cacheable. + */ + public function alterCacheMetadata(array &$cache_contexts, &$cacheable) { + } + } /** diff --git a/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php b/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php index 315772e..25b4ce3 100644 --- a/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php +++ b/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php @@ -1469,7 +1469,7 @@ protected static function arrayFilterZero($var) { } /** - * @return string[] + * {@inheritdoc} */ public function getCacheContexts() { $cache_contexts = []; @@ -1483,13 +1483,12 @@ public function getCacheContexts() { } /** - * @return bool + * {@inheritdoc} */ public function isCacheable() { - return !$this->isExposed(); + return TRUE; } - } /** diff --git a/core/modules/views/src/Plugin/views/sort/Random.php b/core/modules/views/src/Plugin/views/sort/Random.php index 6846f36..9501f79 100644 --- a/core/modules/views/src/Plugin/views/sort/Random.php +++ b/core/modules/views/src/Plugin/views/sort/Random.php @@ -37,13 +37,11 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { * {@inheritdoc} */ public function getCacheContexts() { - // @todo Is there a cache context which specificies that something is not - // cacheable at all? return []; } /** - * @return bool + * {@inheritdoc} */ public function isCacheable() { return FALSE;