TermBreadcrumbBuilder.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace Drupal\taxonomy;
  3. use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
  4. use Drupal\Core\Breadcrumb\Breadcrumb;
  5. use Drupal\Core\Entity\EntityManagerInterface;
  6. use Drupal\Core\Link;
  7. use Drupal\Core\Routing\RouteMatchInterface;
  8. use Drupal\Core\StringTranslation\StringTranslationTrait;
  9. /**
  10. * Provides a custom taxonomy breadcrumb builder that uses the term hierarchy.
  11. */
  12. class TermBreadcrumbBuilder implements BreadcrumbBuilderInterface {
  13. use StringTranslationTrait;
  14. /**
  15. * The entity manager.
  16. *
  17. * @var \Drupal\Core\Entity\EntityManagerInterface
  18. */
  19. protected $entityManager;
  20. /**
  21. * The taxonomy storage.
  22. *
  23. * @var \Drupal\Taxonomy\TermStorageInterface
  24. */
  25. protected $termStorage;
  26. /**
  27. * Constructs the TermBreadcrumbBuilder.
  28. *
  29. * @param \Drupal\Core\Entity\EntityManagerInterface $entityManager
  30. * The entity manager.
  31. */
  32. public function __construct(EntityManagerInterface $entityManager) {
  33. $this->entityManager = $entityManager;
  34. $this->termStorage = $entityManager->getStorage('taxonomy_term');
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function applies(RouteMatchInterface $route_match) {
  40. return $route_match->getRouteName() == 'entity.taxonomy_term.canonical'
  41. && $route_match->getParameter('taxonomy_term') instanceof TermInterface;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function build(RouteMatchInterface $route_match) {
  47. $breadcrumb = new Breadcrumb();
  48. $breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '<front>'));
  49. $term = $route_match->getParameter('taxonomy_term');
  50. // Breadcrumb needs to have terms cacheable metadata as a cacheable
  51. // dependency even though it is not shown in the breadcrumb because e.g. its
  52. // parent might have changed.
  53. $breadcrumb->addCacheableDependency($term);
  54. // @todo This overrides any other possible breadcrumb and is a pure
  55. // hard-coded presumption. Make this behavior configurable per
  56. // vocabulary or term.
  57. $parents = $this->termStorage->loadAllParents($term->id());
  58. // Remove current term being accessed.
  59. array_shift($parents);
  60. foreach (array_reverse($parents) as $term) {
  61. $term = $this->entityManager->getTranslationFromContext($term);
  62. $breadcrumb->addCacheableDependency($term);
  63. $breadcrumb->addLink(Link::createFromRoute($term->getName(), 'entity.taxonomy_term.canonical', ['taxonomy_term' => $term->id()]));
  64. }
  65. // This breadcrumb builder is based on a route parameter, and hence it
  66. // depends on the 'route' cache context.
  67. $breadcrumb->addCacheContexts(['route']);
  68. return $breadcrumb;
  69. }
  70. }