public function TermBreadcrumbBuilder::build in Drupal 8
Same name and namespace in other branches
- 9 core/modules/taxonomy/src/TermBreadcrumbBuilder.php \Drupal\taxonomy\TermBreadcrumbBuilder::build()
Builds the breadcrumb.
Parameters
\Drupal\Core\Routing\RouteMatchInterface $route_match: The current route match.
Return value
\Drupal\Core\Breadcrumb\Breadcrumb A breadcrumb.
Overrides BreadcrumbBuilderInterface::build
File
- core/
modules/ taxonomy/ src/ TermBreadcrumbBuilder.php, line 76
Class
- TermBreadcrumbBuilder
- Provides a custom taxonomy breadcrumb builder that uses the term hierarchy.
Namespace
Drupal\taxonomyCode
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->entityRepository
->getTranslationFromContext($term);
$breadcrumb
->addCacheableDependency($term);
$breadcrumb
->addLink(Link::createFromRoute($term
->getName(), 'entity.taxonomy_term.canonical', [
'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;
}