function CommentBreadcrumbBuilder::build

Same name in other branches
  1. 9 core/modules/comment/src/CommentBreadcrumbBuilder.php \Drupal\comment\CommentBreadcrumbBuilder::build()
  2. 8.9.x core/modules/comment/src/CommentBreadcrumbBuilder.php \Drupal\comment\CommentBreadcrumbBuilder::build()
  3. 11.x core/modules/comment/src/CommentBreadcrumbBuilder.php \Drupal\comment\CommentBreadcrumbBuilder::build()

Overrides BreadcrumbBuilderInterface::build

File

core/modules/comment/src/CommentBreadcrumbBuilder.php, line 45

Class

CommentBreadcrumbBuilder
Class to define the comment breadcrumb builder.

Namespace

Drupal\comment

Code

public function build(RouteMatchInterface $route_match) {
    $breadcrumb = new Breadcrumb();
    $breadcrumb->addCacheContexts([
        'route',
    ]);
    $breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '<front>'));
    $entity = $route_match->getParameter('entity');
    $breadcrumb->addLink(new Link($entity->label(), $entity->toUrl()));
    $breadcrumb->addCacheableDependency($entity);
    if (($pid = $route_match->getParameter('pid')) && ($comment = $this->entityTypeManager
        ->getStorage('comment')
        ->load($pid))) {
        
        /** @var \Drupal\comment\CommentInterface $comment */
        $breadcrumb->addCacheableDependency($comment);
        // Display link to parent comment.
        // @todo Clean-up permalink in https://www.drupal.org/node/2198041
        $breadcrumb->addLink(new Link($comment->getSubject(), $comment->toUrl()));
    }
    return $breadcrumb;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.