function BookNavigationCacheContext::getContext

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

Overrides CacheContextInterface::getContext

File

core/modules/book/src/Cache/BookNavigationCacheContext.php, line 55

Class

BookNavigationCacheContext
Defines the book navigation cache context service.

Namespace

Drupal\book\Cache

Code

public function getContext() {
    // Find the current book's ID.
    $current_bid = 0;
    $node = $this->routeMatch
        ->getParameter('node');
    if ($node instanceof NodeInterface && !empty($node->book['bid'])) {
        $current_bid = $node->book['bid'];
    }
    // If we're not looking at a book node, then we're not navigating a book.
    if ($current_bid === 0) {
        return 'book.none';
    }
    // If we're looking at a book node, get the trail for that node.
    $active_trail = $this->bookManagerService
        ->getActiveTrailIds($node->book['bid'], $node->book);
    return implode('|', $active_trail);
}

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