Commit 9f0db326 authored by catch's avatar catch
Browse files

Issue #2429261 by Wim Leers: Replace the hardcoded cache key on the book...

Issue #2429261 by Wim Leers: Replace the hardcoded cache key on the book navigation block with a 'book navigation' cache context
parent 94266ed9
...@@ -23,3 +23,8 @@ services: ...@@ -23,3 +23,8 @@ services:
arguments: ['@book.manager'] arguments: ['@book.manager']
tags: tags:
- { name: access_check, applies_to: _access_book_removable } - { name: access_check, applies_to: _access_book_removable }
cache_context.book.navigation:
class: Drupal\book\Cache\BookNavigationCacheContext
arguments: ['@request_stack']
tags:
- { name: cache.context}
<?php
/**
* @file
* Contains \Drupal\book\Cache\BookNavigationCacheContext.
*/
namespace Drupal\book\Cache;
use Drupal\Core\Cache\CacheContextInterface;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Defines the book navigation cache context service.
*
* This allows for book navigation location-aware caching. It depends on:
* - whether the current route represents a book node at all
* - and if so, where in the book hierarchy we are
*
* This class is container-aware to avoid initializing the 'book.manager'
* service when it is not necessary.
*/
class BookNavigationCacheContext extends ContainerAware implements CacheContextInterface {
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* Constructs a new BookNavigationCacheContext service.
*
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
*/
public function __construct(RequestStack $request_stack) {
$this->requestStack = $request_stack;
}
/**
* {@inheritdoc}
*/
public static function getLabel() {
return t("Book navigation");
}
/**
* {@inheritdoc}
*/
public function getContext() {
// Find the current book's ID.
$current_bid = 0;
if ($node = $this->requestStack->getCurrentRequest()->get('node')) {
$current_bid = empty($node->book['bid']) ? 0 : $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->container->get('book.manager')
->getActiveTrailIds($node->book['bid'], $node->book);
return 'book.' . implode('|', $active_trail);
}
}
...@@ -178,30 +178,16 @@ public function build() { ...@@ -178,30 +178,16 @@ public function build() {
return array(); return array();
} }
/**
* {@inheritdoc}
*/
public function getCacheKeys() {
// Add a key for the active book trail.
$current_bid = 0;
if ($node = $this->requestStack->getCurrentRequest()->get('node')) {
$current_bid = empty($node->book['bid']) ? 0 : $node->book['bid'];
}
if ($current_bid === 0) {
return parent::getCacheKeys();
}
$active_trail = $this->bookManager->getActiveTrailIds($node->book['bid'], $node->book);
$active_trail_key = 'trail.' . implode('|', $active_trail);
return array_merge(parent::getCacheKeys(), array($active_trail_key));
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function getRequiredCacheContexts() { protected function getRequiredCacheContexts() {
// The "Book navigation" block must be cached per role: different roles may // The "Book navigation" block must be cached per role and book navigation
// have access to different menu links. // context.
return array('user.roles'); return [
'user.roles',
'book.navigation',
];
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment