More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / modules / comment / src / CommentBreadcrumbBuilder.php
1 <?php
2
3 namespace Drupal\comment;
4
5 use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
6 use Drupal\Core\Breadcrumb\Breadcrumb;
7 use Drupal\Core\Entity\EntityManagerInterface;
8 use Drupal\Core\Link;
9 use Drupal\Core\Routing\RouteMatchInterface;
10 use Drupal\Core\StringTranslation\StringTranslationTrait;
11
12 /**
13  * Class to define the comment breadcrumb builder.
14  */
15 class CommentBreadcrumbBuilder implements BreadcrumbBuilderInterface {
16   use StringTranslationTrait;
17
18   /**
19    * The comment storage.
20    *
21    * @var \Drupal\Core\Entity\EntityStorageInterface
22    */
23   protected $storage;
24
25   /**
26    * Constructs the CommentBreadcrumbBuilder.
27    *
28    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
29    *   The entity manager.
30    */
31   public function __construct(EntityManagerInterface $entity_manager) {
32     $this->storage = $entity_manager->getStorage('comment');
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public function applies(RouteMatchInterface $route_match) {
39     return $route_match->getRouteName() == 'comment.reply' && $route_match->getParameter('entity');
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function build(RouteMatchInterface $route_match) {
46     $breadcrumb = new Breadcrumb();
47     $breadcrumb->addCacheContexts(['route']);
48     $breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '<front>'));
49
50     $entity = $route_match->getParameter('entity');
51     $breadcrumb->addLink(new Link($entity->label(), $entity->urlInfo()));
52     $breadcrumb->addCacheableDependency($entity);
53
54     if (($pid = $route_match->getParameter('pid')) && ($comment = $this->storage->load($pid))) {
55       /** @var \Drupal\comment\CommentInterface $comment */
56       $breadcrumb->addCacheableDependency($comment);
57       // Display link to parent comment.
58       // @todo Clean-up permalink in https://www.drupal.org/node/2198041
59       $breadcrumb->addLink(new Link($comment->getSubject(), $comment->urlInfo()));
60     }
61
62     return $breadcrumb;
63   }
64
65 }