TermBreadcrumbBuilder.php

Same filename and directory in other branches
  1. 9 core/modules/taxonomy/src/TermBreadcrumbBuilder.php
  2. 8.9.x core/modules/taxonomy/src/TermBreadcrumbBuilder.php
  3. 10 core/modules/taxonomy/src/TermBreadcrumbBuilder.php

Namespace

Drupal\taxonomy

File

core/modules/taxonomy/src/TermBreadcrumbBuilder.php

View source
<?php

namespace Drupal\taxonomy;

use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
 * Provides a custom taxonomy breadcrumb builder that uses the term hierarchy.
 */
class TermBreadcrumbBuilder implements BreadcrumbBuilderInterface {
    use StringTranslationTrait;
    
    /**
     * The entity repository manager.
     *
     * @var \Drupal\Core\Entity\EntityRepositoryInterface
     */
    protected $entityRepository;
    
    /**
     * The entity type manager.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * 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) {
        $this->entityTypeManager = $entity_type_manager;
        $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->entityTypeManager
            ->getStorage('taxonomy_term')
            ->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;
    }

}

Classes

Title Deprecated Summary
TermBreadcrumbBuilder Provides a custom taxonomy breadcrumb builder that uses the term hierarchy.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.