91ea1a01c36c20d441affebc10d8ba3770950ef2
[yaffs-website] / src / Breadcrumb / ForumListingBreadcrumbBuilder.php
1 <?php
2
3 namespace Drupal\forum\Breadcrumb;
4
5 use Drupal\Core\Link;
6 use Drupal\Core\Routing\RouteMatchInterface;
7
8 /**
9  * Provides a breadcrumb builder base class for forum listing pages.
10  */
11 class ForumListingBreadcrumbBuilder extends ForumBreadcrumbBuilderBase {
12
13   /**
14    * {@inheritdoc}
15    */
16   public function applies(RouteMatchInterface $route_match) {
17     return $route_match->getRouteName() == 'forum.page' && $route_match->getParameter('taxonomy_term');
18   }
19
20   /**
21    * {@inheritdoc}
22    */
23   public function build(RouteMatchInterface $route_match) {
24     $breadcrumb = parent::build($route_match);
25     $breadcrumb->addCacheContexts(['route']);
26
27     // Add all parent forums to breadcrumbs.
28     /** @var \Drupal\Taxonomy\TermInterface $term */
29     $term = $route_match->getParameter('taxonomy_term');
30     $term_id = $term->id();
31     $breadcrumb->addCacheableDependency($term);
32
33     $parents = $this->termStorage->loadAllParents($term_id);
34     if ($parents) {
35       foreach (array_reverse($parents) as $parent) {
36         if ($parent->id() != $term_id) {
37           $breadcrumb->addCacheableDependency($parent);
38           $breadcrumb->addLink(Link::createFromRoute($parent->label(), 'forum.page', [
39             'taxonomy_term' => $parent->id(),
40           ]));
41         }
42       }
43     }
44
45     return $breadcrumb;
46   }
47
48 }