Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\taxonomy;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
|
Chris@0
|
6 use Drupal\Core\Breadcrumb\Breadcrumb;
|
Chris@18
|
7 use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
|
Chris@18
|
8 use Drupal\Core\Entity\EntityRepositoryInterface;
|
Chris@18
|
9 use Drupal\Core\Entity\EntityTypeManagerInterface;
|
Chris@0
|
10 use Drupal\Core\Link;
|
Chris@0
|
11 use Drupal\Core\Routing\RouteMatchInterface;
|
Chris@0
|
12 use Drupal\Core\StringTranslation\StringTranslationTrait;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Provides a custom taxonomy breadcrumb builder that uses the term hierarchy.
|
Chris@0
|
16 */
|
Chris@0
|
17 class TermBreadcrumbBuilder implements BreadcrumbBuilderInterface {
|
Chris@0
|
18 use StringTranslationTrait;
|
Chris@18
|
19 use DeprecatedServicePropertyTrait;
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@18
|
22 * {@inheritdoc}
|
Chris@18
|
23 */
|
Chris@18
|
24 protected $deprecatedProperties = ['entityManager' => 'entity.manager'];
|
Chris@18
|
25
|
Chris@18
|
26 /**
|
Chris@18
|
27 * The entity repository manager.
|
Chris@0
|
28 *
|
Chris@18
|
29 * @var \Drupal\Core\Entity\EntityRepositoryInterface
|
Chris@0
|
30 */
|
Chris@18
|
31 protected $entityRepository;
|
Chris@18
|
32
|
Chris@18
|
33 /**
|
Chris@18
|
34 * The entity type manager.
|
Chris@18
|
35 *
|
Chris@18
|
36 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
|
Chris@18
|
37 */
|
Chris@18
|
38 protected $entityTypeManager;
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * The taxonomy storage.
|
Chris@0
|
42 *
|
Chris@0
|
43 * @var \Drupal\Taxonomy\TermStorageInterface
|
Chris@0
|
44 */
|
Chris@0
|
45 protected $termStorage;
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * Constructs the TermBreadcrumbBuilder.
|
Chris@0
|
49 *
|
Chris@18
|
50 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
|
Chris@18
|
51 * The entity type manager.
|
Chris@18
|
52 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
|
Chris@18
|
53 * The entity repository.
|
Chris@0
|
54 */
|
Chris@18
|
55 public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository = NULL) {
|
Chris@18
|
56 $this->entityTypeManager = $entity_type_manager;
|
Chris@18
|
57 $this->termStorage = $entity_type_manager->getStorage('taxonomy_term');
|
Chris@18
|
58 if (!$entity_repository) {
|
Chris@18
|
59 @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
|
60 $entity_repository = \Drupal::service('entity.repository');
|
Chris@18
|
61 }
|
Chris@18
|
62 $this->entityRepository = $entity_repository;
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 /**
|
Chris@0
|
66 * {@inheritdoc}
|
Chris@0
|
67 */
|
Chris@0
|
68 public function applies(RouteMatchInterface $route_match) {
|
Chris@0
|
69 return $route_match->getRouteName() == 'entity.taxonomy_term.canonical'
|
Chris@0
|
70 && $route_match->getParameter('taxonomy_term') instanceof TermInterface;
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 /**
|
Chris@0
|
74 * {@inheritdoc}
|
Chris@0
|
75 */
|
Chris@0
|
76 public function build(RouteMatchInterface $route_match) {
|
Chris@0
|
77 $breadcrumb = new Breadcrumb();
|
Chris@0
|
78 $breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '<front>'));
|
Chris@0
|
79 $term = $route_match->getParameter('taxonomy_term');
|
Chris@0
|
80 // Breadcrumb needs to have terms cacheable metadata as a cacheable
|
Chris@0
|
81 // dependency even though it is not shown in the breadcrumb because e.g. its
|
Chris@0
|
82 // parent might have changed.
|
Chris@0
|
83 $breadcrumb->addCacheableDependency($term);
|
Chris@0
|
84 // @todo This overrides any other possible breadcrumb and is a pure
|
Chris@0
|
85 // hard-coded presumption. Make this behavior configurable per
|
Chris@0
|
86 // vocabulary or term.
|
Chris@0
|
87 $parents = $this->termStorage->loadAllParents($term->id());
|
Chris@0
|
88 // Remove current term being accessed.
|
Chris@0
|
89 array_shift($parents);
|
Chris@0
|
90 foreach (array_reverse($parents) as $term) {
|
Chris@18
|
91 $term = $this->entityRepository->getTranslationFromContext($term);
|
Chris@0
|
92 $breadcrumb->addCacheableDependency($term);
|
Chris@0
|
93 $breadcrumb->addLink(Link::createFromRoute($term->getName(), 'entity.taxonomy_term.canonical', ['taxonomy_term' => $term->id()]));
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@0
|
96 // This breadcrumb builder is based on a route parameter, and hence it
|
Chris@0
|
97 // depends on the 'route' cache context.
|
Chris@0
|
98 $breadcrumb->addCacheContexts(['route']);
|
Chris@0
|
99
|
Chris@0
|
100 return $breadcrumb;
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 }
|