Same filename and directory in other branches
- 8.9.x core/modules/taxonomy/src/TermBreadcrumbBuilder.php
- 9 core/modules/taxonomy/src/TermBreadcrumbBuilder.php
Namespace
Drupal\taxonomy
File
core/modules/taxonomy/src/TermBreadcrumbBuilder.php
View source
<?php
namespace Drupal\taxonomy;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class TermBreadcrumbBuilder implements BreadcrumbBuilderInterface {
use StringTranslationTrait;
protected $entityRepository;
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository) {
$this->entityTypeManager = $entity_type_manager;
$this->entityRepository = $entity_repository;
}
public function applies(RouteMatchInterface $route_match) {
return $route_match
->getRouteName() == 'entity.taxonomy_term.canonical' && $route_match
->getParameter('taxonomy_term') instanceof TermInterface;
}
public function build(RouteMatchInterface $route_match) {
$breadcrumb = new Breadcrumb();
$breadcrumb
->addLink(Link::createFromRoute($this
->t('Home'), '<front>'));
$term = $route_match
->getParameter('taxonomy_term');
$breadcrumb
->addCacheableDependency($term);
$parents = $this->entityTypeManager
->getStorage('taxonomy_term')
->loadAllParents($term
->id());
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(),
]));
}
$breadcrumb
->addCacheContexts([
'route',
]);
return $breadcrumb;
}
}
Classes