public function BookNavigationCacheContext::getCacheableMetadata in Drupal 10
Same name and namespace in other branches
- 8 core/modules/book/src/Cache/BookNavigationCacheContext.php \Drupal\book\Cache\BookNavigationCacheContext::getCacheableMetadata()
- 9 core/modules/book/src/Cache/BookNavigationCacheContext.php \Drupal\book\Cache\BookNavigationCacheContext::getCacheableMetadata()
Gets the cacheability metadata for the context.
There are three valid cases for the returned CacheableMetadata object:
- An empty object means this can be optimized away safely.
- A max-age of 0 means that this context can never be optimized away. It will never bubble up and cache tags will not be used.
- Any non-zero max-age and cache tags will bubble up into the cache item if this is optimized away to allow for invalidation if the context value changes.
Return value
\Drupal\Core\Cache\CacheableMetadata A cacheable metadata object.
Overrides CacheContextInterface::getCacheableMetadata
File
- core/
modules/ book/ src/ Cache/ BookNavigationCacheContext.php, line 77
Class
- BookNavigationCacheContext
- Defines the book navigation cache context service.
Namespace
Drupal\book\CacheCode
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();
$node = $this->routeMatch
->getParameter('node');
if ($node instanceof NodeInterface) {
// If the node is part of a book then we can use the cache tag for that
// book. If not, then it 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;
}