Chris@0: 'entity.manager']; Chris@18: Chris@18: /** Chris@18: * The entity repository manager. Chris@0: * Chris@18: * @var \Drupal\Core\Entity\EntityRepositoryInterface Chris@0: */ Chris@18: protected $entityRepository; Chris@18: Chris@18: /** Chris@18: * The entity type manager. Chris@18: * Chris@18: * @var \Drupal\Core\Entity\EntityTypeManagerInterface Chris@18: */ Chris@18: protected $entityTypeManager; Chris@0: Chris@0: /** Chris@0: * The taxonomy storage. Chris@0: * Chris@0: * @var \Drupal\Taxonomy\TermStorageInterface Chris@0: */ Chris@0: protected $termStorage; Chris@0: Chris@0: /** Chris@0: * Constructs the TermBreadcrumbBuilder. Chris@0: * Chris@18: * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager Chris@18: * The entity type manager. Chris@18: * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository Chris@18: * The entity repository. Chris@0: */ Chris@18: public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository = NULL) { Chris@18: $this->entityTypeManager = $entity_type_manager; Chris@18: $this->termStorage = $entity_type_manager->getStorage('taxonomy_term'); Chris@18: if (!$entity_repository) { Chris@18: @trigger_error('The entity.repository service must be passed to TermBreadcrumbBuilder::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); Chris@18: $entity_repository = \Drupal::service('entity.repository'); Chris@18: } Chris@18: $this->entityRepository = $entity_repository; 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@18: $term = $this->entityRepository->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: }