/** * {@inheritdoc} */ public function build(RouteMatchInterface $route_match) { $breadcrumb = new Breadcrumb(); $breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '<front>')); $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->termStorage->loadAllParents($term->id()); // Remove current term being accessed. array_shift($parents); foreach (array_reverse($parents) as $term) { $term = $this->entityManager->getTranslationFromContext($term); $breadcrumb->addCacheableDependency($term); $breadcrumb->addLink(Link::createFromRoute($term->getName(), 'entity.taxonomy_term.canonical', array('taxonomy_term' => $term->id()))); } // This breadcrumb builder is based on a route parameter, and hence it // depends on the 'route' cache context. $breadcrumb->addCacheContexts(['route']); return $breadcrumb; }
/** * {@inheritdoc} */ public function build(RouteMatchInterface $route_match) { $breadcrumb = new Breadcrumb(); $breadcrumb->addCacheContexts(['route']); // Add all parent forums to breadcrumbs. /** @var Node $node */ $node = $route_match->getParameter('node'); $breadcrumb->addCacheableDependency($node); // Add all parent forums to breadcrumbs. /** @var \Drupal\Taxonomy\TermInterface $term */ $term = !empty($node->field_channel) ? $node->field_channel->entity : ''; $links = []; if ($term) { $breadcrumb->addCacheableDependency($term); $channels = $this->termStorage->loadAllParents($term->id()); foreach (array_reverse($channels) as $term) { $term = $this->entityManager->getTranslationFromContext($term); $breadcrumb->addCacheableDependency($term); $links[] = Link::createFromRoute($term->getName(), 'entity.taxonomy_term.canonical', array('taxonomy_term' => $term->id())); } } if (!$links || '/' . $links[0]->getUrl()->getInternalPath() != $this->configFactory->get('system.site')->get('page.front')) { array_unshift($links, Link::createFromRoute($this->t('Home'), '<front>')); } return $breadcrumb->setLinks($links); }