annotate core/modules/comment/src/CommentBreadcrumbBuilder.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\comment;
Chris@0 4
Chris@0 5 use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
Chris@0 6 use Drupal\Core\Breadcrumb\Breadcrumb;
Chris@18 7 use Drupal\Core\Entity\EntityTypeManagerInterface;
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 * Class to define the comment breadcrumb builder.
Chris@0 14 */
Chris@0 15 class CommentBreadcrumbBuilder implements BreadcrumbBuilderInterface {
Chris@0 16 use StringTranslationTrait;
Chris@0 17
Chris@0 18 /**
Chris@0 19 * The comment storage.
Chris@0 20 *
Chris@0 21 * @var \Drupal\Core\Entity\EntityStorageInterface
Chris@0 22 */
Chris@0 23 protected $storage;
Chris@0 24
Chris@0 25 /**
Chris@0 26 * Constructs the CommentBreadcrumbBuilder.
Chris@0 27 *
Chris@18 28 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
Chris@18 29 * The entity type manager.
Chris@0 30 */
Chris@18 31 public function __construct(EntityTypeManagerInterface $entity_type_manager) {
Chris@18 32 $this->storage = $entity_type_manager->getStorage('comment');
Chris@0 33 }
Chris@0 34
Chris@0 35 /**
Chris@0 36 * {@inheritdoc}
Chris@0 37 */
Chris@0 38 public function applies(RouteMatchInterface $route_match) {
Chris@0 39 return $route_match->getRouteName() == 'comment.reply' && $route_match->getParameter('entity');
Chris@0 40 }
Chris@0 41
Chris@0 42 /**
Chris@0 43 * {@inheritdoc}
Chris@0 44 */
Chris@0 45 public function build(RouteMatchInterface $route_match) {
Chris@0 46 $breadcrumb = new Breadcrumb();
Chris@0 47 $breadcrumb->addCacheContexts(['route']);
Chris@0 48 $breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '<front>'));
Chris@0 49
Chris@0 50 $entity = $route_match->getParameter('entity');
Chris@18 51 $breadcrumb->addLink(new Link($entity->label(), $entity->toUrl()));
Chris@0 52 $breadcrumb->addCacheableDependency($entity);
Chris@0 53
Chris@0 54 if (($pid = $route_match->getParameter('pid')) && ($comment = $this->storage->load($pid))) {
Chris@0 55 /** @var \Drupal\comment\CommentInterface $comment */
Chris@0 56 $breadcrumb->addCacheableDependency($comment);
Chris@0 57 // Display link to parent comment.
Chris@0 58 // @todo Clean-up permalink in https://www.drupal.org/node/2198041
Chris@18 59 $breadcrumb->addLink(new Link($comment->getSubject(), $comment->toUrl()));
Chris@0 60 }
Chris@0 61
Chris@0 62 return $breadcrumb;
Chris@0 63 }
Chris@0 64
Chris@0 65 }