04ce07ab3a7b23c17295b6c9af497c54db7e410a
[yaffs-website] / src / Breadcrumb / ForumBreadcrumbBuilderBase.php
1 <?php
2
3 namespace Drupal\forum\Breadcrumb;
4
5 use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
6 use Drupal\Core\Breadcrumb\Breadcrumb;
7 use Drupal\Core\Config\ConfigFactoryInterface;
8 use Drupal\Core\Entity\EntityManagerInterface;
9 use Drupal\Core\Link;
10 use Drupal\Core\Routing\RouteMatchInterface;
11 use Drupal\Core\StringTranslation\StringTranslationTrait;
12 use Drupal\Core\StringTranslation\TranslationInterface;
13 use Drupal\forum\ForumManagerInterface;
14
15 /**
16  * Provides a forum breadcrumb base class.
17  *
18  * This just holds the dependency-injected config, entity manager, and forum
19  * manager objects.
20  */
21 abstract class ForumBreadcrumbBuilderBase implements BreadcrumbBuilderInterface {
22   use StringTranslationTrait;
23
24   /**
25    * Configuration object for this builder.
26    *
27    * @var \Drupal\Core\Config\Config
28    */
29   protected $config;
30
31   /**
32    * The entity manager.
33    *
34    * @var \Drupal\Core\Entity\EntityManagerInterface
35    */
36   protected $entityManager;
37
38   /**
39    * The forum manager service.
40    *
41    * @var \Drupal\forum\ForumManagerInterface
42    */
43   protected $forumManager;
44
45   /**
46    * The taxonomy term storage.
47    *
48    * @var \Drupal\taxonomy\TermStorageInterface
49    */
50   protected $termStorage;
51
52   /**
53    * Constructs a forum breadcrumb builder object.
54    *
55    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
56    *   The entity manager.
57    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
58    *   The configuration factory.
59    * @param \Drupal\forum\ForumManagerInterface $forum_manager
60    *   The forum manager service.
61    * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
62    *   The string translation service.
63    */
64   public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, ForumManagerInterface $forum_manager, TranslationInterface $string_translation) {
65     $this->entityManager = $entity_manager;
66     $this->config = $config_factory->get('forum.settings');
67     $this->forumManager = $forum_manager;
68     $this->setStringTranslation($string_translation);
69     $this->termStorage = $entity_manager->getStorage('taxonomy_term');
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public function build(RouteMatchInterface $route_match) {
76     $breadcrumb = new Breadcrumb();
77     $breadcrumb->addCacheContexts(['route']);
78
79     $links[] = Link::createFromRoute($this->t('Home'), '<front>');
80
81     $vocabulary = $this->entityManager
82       ->getStorage('taxonomy_vocabulary')
83       ->load($this->config->get('vocabulary'));
84     $breadcrumb->addCacheableDependency($vocabulary);
85     $links[] = Link::createFromRoute($vocabulary->label(), 'forum.index');
86
87     return $breadcrumb->setLinks($links);
88   }
89
90 }