entityTypeManager = $entity_type_manager; $this->entityRepository = $entity_repository; } /** * {@inheritdoc} */ 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']); return $route_match->getRouteName() == 'entity.taxonomy_term.canonical' && $route_match->getParameter('taxonomy_term') instanceof TermInterface; } /** * {@inheritdoc} */ public function build(RouteMatchInterface $route_match) { $breadcrumb = new Breadcrumb(); $breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '')); $term = $route_match->getParameter('taxonomy_term'); // Breadcrumb needs to have terms cacheable metadata as a cacheable // dependency even though it is not shown in the breadcrumb because e.g. its // parent might have changed. $breadcrumb->addCacheableDependency($term); // @todo This overrides any other possible breadcrumb and is a pure // hard-coded presumption. Make this behavior configurable per // vocabulary or term. $parents = $this->entityTypeManager->getStorage('taxonomy_term')->loadAllParents($term->id()); // Remove current term being accessed. array_shift($parents); foreach (array_reverse($parents) as $term) { $term = $this->entityRepository->getTranslationFromContext($term); $breadcrumb->addCacheableDependency($term); $breadcrumb->addLink(Link::createFromRoute($term->getName(), 'entity.taxonomy_term.canonical', ['taxonomy_term' => $term->id()])); } // @todo Remove in Drupal 12.0.0, will be added from ::applies(). See // https://www.drupal.org/project/drupal/issues/3459277 $breadcrumb->addCacheContexts(['route']); return $breadcrumb; } }