diff --git a/core/modules/system/src/Cache/Context/PathBreadcrumbCacheContext.php b/core/modules/system/src/Cache/Context/PathBreadcrumbCacheContext.php new file mode 100644 index 0000000..80a4c52 --- /dev/null +++ b/core/modules/system/src/Cache/Context/PathBreadcrumbCacheContext.php @@ -0,0 +1,68 @@ +requestStack = $request_stack; + } + + /** + * {@inheritdoc} + */ + public static function getLabel() { + return t('Path-based breadcrumb'); + } + + /** + * {@inheritdoc} + */ + public function getContext() { + $request = $this->requestStack->getCurrentRequest(); + if ($exception = $request->attributes->get('exception')) { + if ($exception->getStatusCode() == 404) { + // If we're on a 404, the last part of the path cannot match a route, + // otherwise we wouldn't be on a 404, so remove it from the cache + // context. + $path_elements = explode('/', trim($request->getPathInfo(), '/')); + array_pop($path_elements); + return implode('/', $path_elements); + } + } + return $request->getBasePath() . $request->getPathInfo(); + } + + /** + * {@inheritdoc} + */ + public function getCacheableMetadata() { + return new CacheableMetadata(); + } + +} diff --git a/core/modules/system/src/PathBasedBreadcrumbBuilder.php b/core/modules/system/src/PathBasedBreadcrumbBuilder.php index 060f1b1..f148b45 100644 --- a/core/modules/system/src/PathBasedBreadcrumbBuilder.php +++ b/core/modules/system/src/PathBasedBreadcrumbBuilder.php @@ -136,9 +136,8 @@ public function build(RouteMatchInterface $route_match) { // /user is just a redirect, so skip it. // @todo Find a better way to deal with /user. $exclude['/user'] = TRUE; - // Because this breadcrumb builder is entirely path-based, vary by the - // 'url.path' cache context. - $breadcrumb->addCacheContexts(['url.path']); + // Add the url.breadcrumb cache context. + $breadcrumb->addCacheContexts(['url.breadcrumb']); while (count($path_elements) > 1) { array_pop($path_elements); // Copy the path elements for up-casting. diff --git a/core/modules/system/system.services.yml b/core/modules/system/system.services.yml index c70889d..f82997e 100644 --- a/core/modules/system/system.services.yml +++ b/core/modules/system/system.services.yml @@ -7,6 +7,11 @@ services: class: Drupal\system\Access\DbUpdateAccessCheck tags: - { name: access_check, applies_to: _access_system_update } + cache_context.url.breadcrumb: + class: Drupal\system\Cache\Context\PathBreadcrumbCacheContext + arguments: ['@request_stack'] + tags: + - { name: cache.context } system.manager: class: Drupal\system\SystemManager arguments: ['@module_handler', '@entity.manager', '@request_stack', '@menu.link_tree', '@menu.active_trail']