diff --git a/core/lib/Drupal/Core/Cache/Context/CacheContextInterface.php b/core/lib/Drupal/Core/Cache/Context/CacheContextInterface.php index 2ad8dcf..f7ce3ab 100644 --- a/core/lib/Drupal/Core/Cache/Context/CacheContextInterface.php +++ b/core/lib/Drupal/Core/Cache/Context/CacheContextInterface.php @@ -32,7 +32,7 @@ public static function getLabel(); public function getContext(); /** - * Gets the cacheable metadata for the context. + * Gets the cacheability metadata for the context. * * If the cache context is being optimized away, cache tags and max-age * provided by this method will be bubbled up into the cache item. diff --git a/core/lib/Drupal/Core/Cache/Context/CacheContextsManager.php b/core/lib/Drupal/Core/Cache/Context/CacheContextsManager.php index e158fdd..a0b3b4e 100644 --- a/core/lib/Drupal/Core/Cache/Context/CacheContextsManager.php +++ b/core/lib/Drupal/Core/Cache/Context/CacheContextsManager.php @@ -111,7 +111,7 @@ public function convertTokensToKeys(array $context_tokens) { $cacheable_metadata = new CacheableMetadata(); $optimized_tokens = $this->optimizeTokens($context_tokens); // Iterate over cache contexts that have been optimized away and get their - // cacheable metadata. + // cacheability metadata. foreach (static::parseTokens(array_diff($context_tokens, $optimized_tokens)) as $context_token) { list($context_id, $parameter) = $context_token; $context = $this->getService($context_id); @@ -125,10 +125,9 @@ public function convertTokensToKeys(array $context_tokens) { $keys[] = $this->getService($context_id)->getContext($parameter); } + // Create the returned object and merge in the cacheability metadata. $context_cache_keys = new ContextCacheKeys($keys); - return $context_cache_keys - ->addCacheTags($cacheable_metadata->getCacheTags()) - ->setCacheMaxAge($cacheable_metadata->getCacheMaxAge()); + return $context_cache_keys->merge($cacheable_metadata); } /** diff --git a/core/lib/Drupal/Core/Cache/Context/CalculatedCacheContextInterface.php b/core/lib/Drupal/Core/Cache/Context/CalculatedCacheContextInterface.php index 83f5472..5d38935 100644 --- a/core/lib/Drupal/Core/Cache/Context/CalculatedCacheContextInterface.php +++ b/core/lib/Drupal/Core/Cache/Context/CalculatedCacheContextInterface.php @@ -38,7 +38,7 @@ public static function getLabel(); public function getContext($parameter = NULL); /** - * Gets the cacheable metadata for the context based on the parameter value. + * Gets the cacheability metadata for the context based on the parameter value. * * If the cache context is being optimized away, cache tags and max-age * provided by this method will be bubbled up into the cache item. diff --git a/core/lib/Drupal/Core/Cache/Context/ContextCacheKeys.php b/core/lib/Drupal/Core/Cache/Context/ContextCacheKeys.php index 998645e..4b5593f 100644 --- a/core/lib/Drupal/Core/Cache/Context/ContextCacheKeys.php +++ b/core/lib/Drupal/Core/Cache/Context/ContextCacheKeys.php @@ -10,7 +10,7 @@ use Drupal\Core\Cache\CacheableMetadata; /** - * A value object to store generated cache keys with its cacheable metadata. + * A value object to store generated cache keys with its cacheability metadata. */ class ContextCacheKeys extends CacheableMetadata { @@ -22,13 +22,6 @@ class ContextCacheKeys extends CacheableMetadata { protected $keys; /** - * The cacheable metadata associated for the optimized cache contexts. - * - * @var \Drupal\Core\Cache\CacheableMetadata - */ - protected $cacheableMetadata; - - /** * Constructs a ContextCacheKeys object. * * @param string[] $keys diff --git a/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php b/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php index 39c2460..5e5f126 100644 --- a/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php +++ b/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php @@ -86,7 +86,7 @@ public function applyContextMapping(ContextAwarePluginInterface $plugin, $contex unset($mappings[$plugin_context_id]); // Plugins have their on context objects, only the value is applied. - // They also need to know about the cacheable metadata of where that + // They also need to know about the cacheability metadata of where that // value is coming from, so pass them through to those objects. $plugin_context = $plugin->getContext($plugin_context_id); if ($plugin_context instanceof ContextInterface && $contexts[$context_id] instanceof CacheableDependencyInterface) { diff --git a/core/modules/book/src/Cache/BookNavigationCacheContext.php b/core/modules/book/src/Cache/BookNavigationCacheContext.php index aa96af0..96e168a 100644 --- a/core/modules/book/src/Cache/BookNavigationCacheContext.php +++ b/core/modules/book/src/Cache/BookNavigationCacheContext.php @@ -75,8 +75,20 @@ public function getContext() { * {@inheritdoc} */ public function getCacheableMetadata() { + // The book active trail depends on the node and data attached to it. + // That information is however not stored as part of the node. $cacheable_metadata = new CacheableMetadata(); - return $cacheable_metadata->setCacheMaxAge(0); + if ($node = $this->requestStack->getCurrentRequest()->get('node')) { + // If the node is part of a book that we can use the cache tag for that + // book. If not, then we can't be optimized away. + if (!empty($node->book['bid'])) { + $cacheable_metadata->addCacheTags('bid:' . $node->book['bid']); + } + else { + $cacheable_metadata->setCacheMaxAge(0); + } + } + return $cacheable_metadata; } } diff --git a/core/modules/system/src/Tests/Cache/CacheContextOptimizationTest.php b/core/modules/system/src/Tests/Cache/CacheContextOptimizationTest.php index 7f84b57..050ef4a 100644 --- a/core/modules/system/src/Tests/Cache/CacheContextOptimizationTest.php +++ b/core/modules/system/src/Tests/Cache/CacheContextOptimizationTest.php @@ -69,8 +69,8 @@ public function testUserPermissionCacheContextOptimization() { // Even though the cache contexts have been optimized to only include 'user' // cache context, the element should have been changed because // 'user.permissions' cache context defined a cache tags for permission - // changes, which should have bubbled up for the element when it was optimized - // away. + // changes, which should have bubbled up for the element when it was + // optimized away. Role::load($role) ->revokePermission('administer permissions') ->save();