core/core.services.yml | 7 +++- core/lib/Drupal/Core/Cache/PagerCacheContext.php | 8 +++- core/lib/Drupal/Core/Cache/RouteCacheContext.php | 48 ++++++++++++++++++++++ core/modules/book/book.services.yml | 2 +- .../book/src/Plugin/Block/BookNavigationBlock.php | 2 +- .../system/src/Plugin/Block/SystemMenuBlock.php | 2 +- .../Tests/Cache/PageCacheTagsIntegrationTest.php | 8 ++-- 7 files changed, 67 insertions(+), 10 deletions(-) diff --git a/core/core.services.yml b/core/core.services.yml index 2eb4b93..ee0d125 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -26,7 +26,12 @@ services: arguments: ['@request_stack'] tags: - { name: cache.context} - cache_context.url.menu_active_trail: + cache_context.url.route: + class: Drupal\Core\Cache\RouteCacheContext + arguments: ['@current_route_match'] + tags: + - { name: cache.context} + cache_context.url.route.menu_active_trail: class: Drupal\Core\Cache\MenuActiveTrailCacheContext calls: - [setContainer, ['@service_container']] diff --git a/core/lib/Drupal/Core/Cache/PagerCacheContext.php b/core/lib/Drupal/Core/Cache/PagerCacheContext.php index a4c308c..f9d6c3a 100644 --- a/core/lib/Drupal/Core/Cache/PagerCacheContext.php +++ b/core/lib/Drupal/Core/Cache/PagerCacheContext.php @@ -10,7 +10,7 @@ /** * Defines a cache context for "per page in a pager" caching. */ -class PagerCacheContext implements CalculatedCacheContextInterface { +class PagerCacheContext extends RequestStackCacheContextBase implements CalculatedCacheContextInterface { /** * {@inheritdoc} @@ -22,7 +22,11 @@ public static function getLabel() { /** * {@inheritdoc} */ - public function getContext($pager_id) { + public function getContext($pager_id = NULL) { + if ($pager_id === NULL) { + return 'pager' . $this->requestStack->getCurrentRequest()->query->get('page', ''); + } + return 'pager.' . $pager_id . '.' . pager_find_page($pager_id); } diff --git a/core/lib/Drupal/Core/Cache/RouteCacheContext.php b/core/lib/Drupal/Core/Cache/RouteCacheContext.php new file mode 100644 index 0000000..f944f12 --- /dev/null +++ b/core/lib/Drupal/Core/Cache/RouteCacheContext.php @@ -0,0 +1,48 @@ +routeMatch = $route_match; + } + + /** + * {@inheritdoc} + */ + public static function getLabel() { + return t('Route'); + } + + /** + * {@inheritdoc} + */ + public function getContext() { + return $this->routeMatch->getRouteName() . serialize($this->routeMatch->getRawParameters()->all()); + } + +} diff --git a/core/modules/book/book.services.yml b/core/modules/book/book.services.yml index 440d021..179c924 100644 --- a/core/modules/book/book.services.yml +++ b/core/modules/book/book.services.yml @@ -23,7 +23,7 @@ services: arguments: ['@book.manager'] tags: - { name: access_check, applies_to: _access_book_removable } - cache_context.book.navigation: + cache_context.url.route.book_navigation: class: Drupal\book\Cache\BookNavigationCacheContext arguments: ['@request_stack'] tags: diff --git a/core/modules/book/src/Plugin/Block/BookNavigationBlock.php b/core/modules/book/src/Plugin/Block/BookNavigationBlock.php index 4ce4c3f..7fce677 100644 --- a/core/modules/book/src/Plugin/Block/BookNavigationBlock.php +++ b/core/modules/book/src/Plugin/Block/BookNavigationBlock.php @@ -186,7 +186,7 @@ protected function getRequiredCacheContexts() { // context. return [ 'user.roles', - 'book.navigation', + 'url.route.book_navigation', ]; } diff --git a/core/modules/system/src/Plugin/Block/SystemMenuBlock.php b/core/modules/system/src/Plugin/Block/SystemMenuBlock.php index 0d9ab37..f5713f1 100644 --- a/core/modules/system/src/Plugin/Block/SystemMenuBlock.php +++ b/core/modules/system/src/Plugin/Block/SystemMenuBlock.php @@ -201,7 +201,7 @@ protected function getRequiredCacheContexts() { $menu_name = $this->getDerivativeId(); return [ 'user.roles', - 'url.menu_active_trail:' . $menu_name, + 'url.route.menu_active_trail:' . $menu_name, ]; } diff --git a/core/modules/system/src/Tests/Cache/PageCacheTagsIntegrationTest.php b/core/modules/system/src/Tests/Cache/PageCacheTagsIntegrationTest.php index 3fb4ea6..d2e4ac9 100644 --- a/core/modules/system/src/Tests/Cache/PageCacheTagsIntegrationTest.php +++ b/core/modules/system/src/Tests/Cache/PageCacheTagsIntegrationTest.php @@ -72,10 +72,10 @@ function testPageCacheTags() { $cache_contexts = [ 'language', - 'url.menu_active_trail:account', - 'url.menu_active_trail:footer', - 'url.menu_active_trail:main', - 'url.menu_active_trail:tools', + 'url.route.menu_active_trail:account', + 'url.route.menu_active_trail:footer', + 'url.route.menu_active_trail:main', + 'url.route.menu_active_trail:tools', 'theme', 'timezone', 'user.roles',