TermBreadcrumbBuilder.php 3.54 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\taxonomy;

5
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
6
use Drupal\Core\Breadcrumb\Breadcrumb;
7 8 9
use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
10
use Drupal\Core\Link;
11
use Drupal\Core\Routing\RouteMatchInterface;
12
use Drupal\Core\StringTranslation\StringTranslationTrait;
13 14 15 16

/**
 * Provides a custom taxonomy breadcrumb builder that uses the term hierarchy.
 */
17 18
class TermBreadcrumbBuilder implements BreadcrumbBuilderInterface {
  use StringTranslationTrait;
19
  use DeprecatedServicePropertyTrait;
20

21
  /**
22 23 24 25 26 27
   * {@inheritdoc}
   */
  protected $deprecatedProperties = ['entityManager' => 'entity.manager'];

  /**
   * The entity repository manager.
28
   *
29
   * @var \Drupal\Core\Entity\EntityRepositoryInterface
30
   */
31 32 33 34 35 36 37 38
  protected $entityRepository;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;
39 40 41 42

  /**
   * The taxonomy storage.
   *
43
   * @var \Drupal\Taxonomy\TermStorageInterface
44 45 46 47 48 49
   */
  protected $termStorage;

  /**
   * Constructs the TermBreadcrumbBuilder.
   *
50 51 52 53
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
   *   The entity repository.
54
   */
55 56 57 58 59 60 61 62
  public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository = NULL) {
    $this->entityTypeManager = $entity_type_manager;
    $this->termStorage = $entity_type_manager->getStorage('taxonomy_term');
    if (!$entity_repository) {
      @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);
      $entity_repository = \Drupal::service('entity.repository');
    }
    $this->entityRepository = $entity_repository;
63 64
  }

65 66 67
  /**
   * {@inheritdoc}
   */
68
  public function applies(RouteMatchInterface $route_match) {
69
    return $route_match->getRouteName() == 'entity.taxonomy_term.canonical'
70
      && $route_match->getParameter('taxonomy_term') instanceof TermInterface;
71 72
  }

73 74 75
  /**
   * {@inheritdoc}
   */
76
  public function build(RouteMatchInterface $route_match) {
77 78
    $breadcrumb = new Breadcrumb();
    $breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '<front>'));
79
    $term = $route_match->getParameter('taxonomy_term');
80 81 82 83
    // 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);
84 85 86
    // @todo This overrides any other possible breadcrumb and is a pure
    //   hard-coded presumption. Make this behavior configurable per
    //   vocabulary or term.
87 88 89 90
    $parents = $this->termStorage->loadAllParents($term->id());
    // Remove current term being accessed.
    array_shift($parents);
    foreach (array_reverse($parents) as $term) {
91
      $term = $this->entityRepository->getTranslationFromContext($term);
92
      $breadcrumb->addCacheableDependency($term);
93
      $breadcrumb->addLink(Link::createFromRoute($term->getName(), 'entity.taxonomy_term.canonical', ['taxonomy_term' => $term->id()]));
94
    }
95 96 97

    // This breadcrumb builder is based on a route parameter, and hence it
    // depends on the 'route' cache context.
98
    $breadcrumb->addCacheContexts(['route']);
99 100

    return $breadcrumb;
101 102 103
  }

}