You are here

class TermBreadcrumbBuilder in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/taxonomy/src/TermBreadcrumbBuilder.php \Drupal\taxonomy\TermBreadcrumbBuilder
  2. 10 core/modules/taxonomy/src/TermBreadcrumbBuilder.php \Drupal\taxonomy\TermBreadcrumbBuilder

Provides a custom taxonomy breadcrumb builder that uses the term hierarchy.

Hierarchy

Expanded class hierarchy of TermBreadcrumbBuilder

1 string reference to 'TermBreadcrumbBuilder'
taxonomy.services.yml in core/modules/taxonomy/taxonomy.services.yml
core/modules/taxonomy/taxonomy.services.yml
1 service uses TermBreadcrumbBuilder
taxonomy_term.breadcrumb in core/modules/taxonomy/taxonomy.services.yml
Drupal\taxonomy\TermBreadcrumbBuilder

File

core/modules/taxonomy/src/TermBreadcrumbBuilder.php, line 17

Namespace

Drupal\taxonomy
View source
class TermBreadcrumbBuilder implements BreadcrumbBuilderInterface {
  use StringTranslationTrait;
  use DeprecatedServicePropertyTrait;

  /**
   * {@inheritdoc}
   */
  protected $deprecatedProperties = [
    'entityManager' => 'entity.manager',
  ];

  /**
   * The entity repository manager.
   *
   * @var \Drupal\Core\Entity\EntityRepositoryInterface
   */
  protected $entityRepository;

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

  /**
   * The taxonomy storage.
   *
   * @var \Drupal\taxonomy\TermStorageInterface
   */
  protected $termStorage;

  /**
   * Constructs the TermBreadcrumbBuilder.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
   *   The entity repository.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function applies(RouteMatchInterface $route_match) {
    return $route_match
      ->getRouteName() == 'entity.taxonomy_term.canonical' && $route_match
      ->getParameter('taxonomy_term') instanceof TermInterface;
  }

  /**
   * {@inheritdoc}
   */
  public function build(RouteMatchInterface $route_match) {
    $breadcrumb = new Breadcrumb();
    $breadcrumb
      ->addLink(Link::createFromRoute($this
      ->t('Home'), '<front>'));
    $term = $route_match
      ->getParameter('taxonomy_term');

    // 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);

    // @todo This overrides any other possible breadcrumb and is a pure
    //   hard-coded presumption. Make this behavior configurable per
    //   vocabulary or term.
    $parents = $this->termStorage
      ->loadAllParents($term
      ->id());

    // Remove current term being accessed.
    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(),
      ]));
    }

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

}

Members

Namesort descending Modifiers Type Description Overrides
DeprecatedServicePropertyTrait::__get public function Allows to access deprecated/removed properties.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TermBreadcrumbBuilder::$deprecatedProperties protected property
TermBreadcrumbBuilder::$entityRepository protected property The entity repository manager.
TermBreadcrumbBuilder::$entityTypeManager protected property The entity type manager.
TermBreadcrumbBuilder::$termStorage protected property The taxonomy storage.
TermBreadcrumbBuilder::applies public function Whether this breadcrumb builder should be used to build the breadcrumb. Overrides BreadcrumbBuilderInterface::applies
TermBreadcrumbBuilder::build public function Builds the breadcrumb. Overrides BreadcrumbBuilderInterface::build
TermBreadcrumbBuilder::__construct public function Constructs the TermBreadcrumbBuilder.