annotate core/modules/taxonomy/src/TermBreadcrumbBuilder.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 4c8ae668cc8c
children af1871eacc83
rev   line source
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@0 7 use Drupal\Core\Entity\EntityManagerInterface;
Chris@0 8 use Drupal\Core\Link;
Chris@0 9 use Drupal\Core\Routing\RouteMatchInterface;
Chris@0 10 use Drupal\Core\StringTranslation\StringTranslationTrait;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Provides a custom taxonomy breadcrumb builder that uses the term hierarchy.
Chris@0 14 */
Chris@0 15 class TermBreadcrumbBuilder implements BreadcrumbBuilderInterface {
Chris@0 16 use StringTranslationTrait;
Chris@0 17
Chris@0 18 /**
Chris@0 19 * The entity manager.
Chris@0 20 *
Chris@0 21 * @var \Drupal\Core\Entity\EntityManagerInterface
Chris@0 22 */
Chris@0 23 protected $entityManager;
Chris@0 24
Chris@0 25 /**
Chris@0 26 * The taxonomy storage.
Chris@0 27 *
Chris@0 28 * @var \Drupal\Taxonomy\TermStorageInterface
Chris@0 29 */
Chris@0 30 protected $termStorage;
Chris@0 31
Chris@0 32 /**
Chris@0 33 * Constructs the TermBreadcrumbBuilder.
Chris@0 34 *
Chris@0 35 * @param \Drupal\Core\Entity\EntityManagerInterface $entityManager
Chris@0 36 * The entity manager.
Chris@0 37 */
Chris@0 38 public function __construct(EntityManagerInterface $entityManager) {
Chris@0 39 $this->entityManager = $entityManager;
Chris@0 40 $this->termStorage = $entityManager->getStorage('taxonomy_term');
Chris@0 41 }
Chris@0 42
Chris@0 43 /**
Chris@0 44 * {@inheritdoc}
Chris@0 45 */
Chris@0 46 public function applies(RouteMatchInterface $route_match) {
Chris@0 47 return $route_match->getRouteName() == 'entity.taxonomy_term.canonical'
Chris@0 48 && $route_match->getParameter('taxonomy_term') instanceof TermInterface;
Chris@0 49 }
Chris@0 50
Chris@0 51 /**
Chris@0 52 * {@inheritdoc}
Chris@0 53 */
Chris@0 54 public function build(RouteMatchInterface $route_match) {
Chris@0 55 $breadcrumb = new Breadcrumb();
Chris@0 56 $breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '<front>'));
Chris@0 57 $term = $route_match->getParameter('taxonomy_term');
Chris@0 58 // Breadcrumb needs to have terms cacheable metadata as a cacheable
Chris@0 59 // dependency even though it is not shown in the breadcrumb because e.g. its
Chris@0 60 // parent might have changed.
Chris@0 61 $breadcrumb->addCacheableDependency($term);
Chris@0 62 // @todo This overrides any other possible breadcrumb and is a pure
Chris@0 63 // hard-coded presumption. Make this behavior configurable per
Chris@0 64 // vocabulary or term.
Chris@0 65 $parents = $this->termStorage->loadAllParents($term->id());
Chris@0 66 // Remove current term being accessed.
Chris@0 67 array_shift($parents);
Chris@0 68 foreach (array_reverse($parents) as $term) {
Chris@0 69 $term = $this->entityManager->getTranslationFromContext($term);
Chris@0 70 $breadcrumb->addCacheableDependency($term);
Chris@0 71 $breadcrumb->addLink(Link::createFromRoute($term->getName(), 'entity.taxonomy_term.canonical', ['taxonomy_term' => $term->id()]));
Chris@0 72 }
Chris@0 73
Chris@0 74 // This breadcrumb builder is based on a route parameter, and hence it
Chris@0 75 // depends on the 'route' cache context.
Chris@0 76 $breadcrumb->addCacheContexts(['route']);
Chris@0 77
Chris@0 78 return $breadcrumb;
Chris@0 79 }
Chris@0 80
Chris@0 81 }