|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\unity_breadcrumbs; |
| 4 | + |
| 5 | +/** |
| 6 | + * @file |
| 7 | + * Generates the breadcrumb trail for content including: |
| 8 | + * - Decision |
| 9 | + * |
| 10 | + * In the format: |
| 11 | + * > Home |
| 12 | + * > Documents |
| 13 | + * > current-page-title |
| 14 | + * |
| 15 | + * > <front> |
| 16 | + * > /documents |
| 17 | + * > /current-page-title |
| 18 | + */ |
| 19 | +use Drupal\Core\Breadcrumb\Breadcrumb; |
| 20 | +use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface; |
| 21 | +use Drupal\Core\Controller\TitleResolverInterface; |
| 22 | +use Drupal\Core\Entity\EntityTypeManagerInterface; |
| 23 | +use Drupal\Core\Routing\RouteMatchInterface; |
| 24 | +use Drupal\Core\Link; |
| 25 | +use Drupal\Core\Url; |
| 26 | +use Drupal\node\NodeInterface; |
| 27 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 28 | +use Symfony\Component\HttpFoundation\RequestStack; |
| 29 | + |
| 30 | +/** |
| 31 | + * {@inheritdoc} |
| 32 | + */ |
| 33 | +class DocumentBreadcrumb implements BreadcrumbBuilderInterface { |
| 34 | + |
| 35 | + /** |
| 36 | + * @var \Drupal\Core\Entity\EntityTypeManagerInterface |
| 37 | + */ |
| 38 | + protected $entityTypeManager; |
| 39 | + |
| 40 | + /** |
| 41 | + * Node object, or null if on a non-node page. |
| 42 | + * |
| 43 | + * @var \Drupal\node\Entity\Node |
| 44 | + */ |
| 45 | + protected $node; |
| 46 | + |
| 47 | + /** |
| 48 | + * The title resolver. |
| 49 | + * |
| 50 | + * @var \Drupal\Core\Controller\TitleResolverInterface |
| 51 | + */ |
| 52 | + protected $titleResolver; |
| 53 | + |
| 54 | + /** |
| 55 | + * RequestStack service object. |
| 56 | + * |
| 57 | + * @var \Symfony\Component\HttpFoundation\RequestStack |
| 58 | + */ |
| 59 | + protected $request; |
| 60 | + |
| 61 | + /** |
| 62 | + * Class constructor. |
| 63 | + */ |
| 64 | + public function __construct(EntityTypeManagerInterface $entity_type_manager, TitleResolverInterface $title_resolver, RequestStack $request) { |
| 65 | + $this->entityTypeManager = $entity_type_manager; |
| 66 | + $this->titleResolver = $title_resolver; |
| 67 | + $this->request = $request; |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * {@inheritdoc} |
| 73 | + */ |
| 74 | + public static function create(ContainerInterface $container) { |
| 75 | + return new static( |
| 76 | + $container->get('entity_type.manager'), |
| 77 | + $container->get('title_resolver'), |
| 78 | + $container->get('request_stack') |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * {@inheritdoc} |
| 84 | + */ |
| 85 | + public function applies(RouteMatchInterface $route_match) { |
| 86 | + $match = FALSE; |
| 87 | + $route_name = $route_match->getRouteName(); |
| 88 | + if ($route_name == 'entity.node.canonical') { |
| 89 | + $this->node = $route_match->getParameter('node'); |
| 90 | + } |
| 91 | + |
| 92 | + if ($route_name == 'entity.node.preview') { |
| 93 | + $this->node = $route_match->getParameter('node_preview'); |
| 94 | + } |
| 95 | + |
| 96 | + if (!empty($this->node)) { |
| 97 | + if ($this->node instanceof NodeInterface == FALSE) { |
| 98 | + $this->node = $this->entityTypeManager->getStorage('node'); |
| 99 | + } |
| 100 | + |
| 101 | + if ($this->node->bundle() == 'document') { |
| 102 | + $match = TRUE; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return $match; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * {@inheritdoc} |
| 111 | + */ |
| 112 | + public function build(RouteMatchInterface $route_match) { |
| 113 | + $breadcrumb = new Breadcrumb(); |
| 114 | + $title_resolver = $this->titleResolver->getTitle($this->request->getCurrentRequest(), $route_match->getRouteObject()); |
| 115 | + $links[] = Link::createFromRoute(t('Home'), '<front>'); |
| 116 | + $links[] = Link::fromTextandUrl(t('Documents'), Url::fromRoute('view.documents_search.documents_search_page')); |
| 117 | + $links[] = Link::createFromRoute($title_resolver, '<none>'); |
| 118 | + $breadcrumb->setLinks($links); |
| 119 | + $breadcrumb->addCacheContexts(['url.path']); |
| 120 | + return $breadcrumb; |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments