function BookNavigationCacheContext::getCacheableMetadata

Same name in other branches
  1. 9 core/modules/book/src/Cache/BookNavigationCacheContext.php \Drupal\book\Cache\BookNavigationCacheContext::getCacheableMetadata()
  2. 8.9.x core/modules/book/src/Cache/BookNavigationCacheContext.php \Drupal\book\Cache\BookNavigationCacheContext::getCacheableMetadata()
  3. 10 core/modules/book/src/Cache/BookNavigationCacheContext.php \Drupal\book\Cache\BookNavigationCacheContext::getCacheableMetadata()

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\Cache

Code

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.