Skip to content
Snippets Groups Projects

Apply fix to applies and build in the builder to add route context to cache

@@ -6,6 +6,7 @@ use Drupal\Component\Utility\SortArray;
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\TitleResolverInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
@@ -169,7 +170,11 @@ class MenuBasedBreadcrumbBuilder implements BreadcrumbBuilderInterface {
/**
* {@inheritdoc}
*/
public function applies(RouteMatchInterface $route_match) {
public function applies(RouteMatchInterface $route_match, ?CacheableMetadata $cacheable_metadata = NULL) {
// @todo Remove null safe operator in Drupal 12.0.0, see
// https://www.drupal.org/project/drupal/issues/3459277.
$cacheable_metadata?->addCacheContexts(['route']);
// This may look heavyweight for applies() but we have to check all ways the
// current path could be attached to the selected menus before turning over
// breadcrumb building (and caching) to another builder. Generally this
@@ -292,6 +297,10 @@ class MenuBasedBreadcrumbBuilder implements BreadcrumbBuilderInterface {
$breadcrumb->addCacheContexts(['languages:language_content']);
}
// @todo Remove in Drupal 12.0.0, will be added from ::applies(). See
// https://www.drupal.org/project/drupal/issues/3459277
$breadcrumb->addCacheContexts(['route']);
// Changing the <front> page will invalidate any breadcrumb generated here:
$site_config = $this->configFactory->get('system.site');
$breadcrumb->addCacheableDependency($site_config);
Loading