Chris@0: entityManager = $entityManager; Chris@0: $this->termStorage = $entityManager->getStorage('taxonomy_term'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function applies(RouteMatchInterface $route_match) { Chris@0: return $route_match->getRouteName() == 'entity.taxonomy_term.canonical' Chris@0: && $route_match->getParameter('taxonomy_term') instanceof TermInterface; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function build(RouteMatchInterface $route_match) { Chris@0: $breadcrumb = new Breadcrumb(); Chris@0: $breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '')); Chris@0: $term = $route_match->getParameter('taxonomy_term'); Chris@0: // Breadcrumb needs to have terms cacheable metadata as a cacheable Chris@0: // dependency even though it is not shown in the breadcrumb because e.g. its Chris@0: // parent might have changed. Chris@0: $breadcrumb->addCacheableDependency($term); Chris@0: // @todo This overrides any other possible breadcrumb and is a pure Chris@0: // hard-coded presumption. Make this behavior configurable per Chris@0: // vocabulary or term. Chris@0: $parents = $this->termStorage->loadAllParents($term->id()); Chris@0: // Remove current term being accessed. Chris@0: array_shift($parents); Chris@0: foreach (array_reverse($parents) as $term) { Chris@0: $term = $this->entityManager->getTranslationFromContext($term); Chris@0: $breadcrumb->addCacheableDependency($term); Chris@0: $breadcrumb->addLink(Link::createFromRoute($term->getName(), 'entity.taxonomy_term.canonical', ['taxonomy_term' => $term->id()])); Chris@0: } Chris@0: Chris@0: // This breadcrumb builder is based on a route parameter, and hence it Chris@0: // depends on the 'route' cache context. Chris@0: $breadcrumb->addCacheContexts(['route']); Chris@0: Chris@0: return $breadcrumb; Chris@0: } Chris@0: Chris@0: }