class CommentBreadcrumbBuilder

Same name and namespace in other branches
  1. 9 core/modules/comment/src/CommentBreadcrumbBuilder.php \Drupal\comment\CommentBreadcrumbBuilder
  2. 8.9.x core/modules/comment/src/CommentBreadcrumbBuilder.php \Drupal\comment\CommentBreadcrumbBuilder
  3. 11.x core/modules/comment/src/CommentBreadcrumbBuilder.php \Drupal\comment\CommentBreadcrumbBuilder

Class to define the comment breadcrumb builder.

Hierarchy

Expanded class hierarchy of CommentBreadcrumbBuilder

1 string reference to 'CommentBreadcrumbBuilder'
comment.services.yml in core/modules/comment/comment.services.yml
core/modules/comment/comment.services.yml
1 service uses CommentBreadcrumbBuilder
comment.breadcrumb in core/modules/comment/comment.services.yml
Drupal\comment\CommentBreadcrumbBuilder

File

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

Namespace

Drupal\comment
View source
class CommentBreadcrumbBuilder implements BreadcrumbBuilderInterface {
  use StringTranslationTrait;
  
  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;
  
  /**
   * Constructs the CommentBreadcrumbBuilder.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }
  
  /**
   * {@inheritdoc}
   */
  public function applies(RouteMatchInterface $route_match) {
    return $route_match->getRouteName() == 'comment.reply' && $route_match->getParameter('entity');
  }
  
  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
CommentBreadcrumbBuilder::$entityTypeManager protected property The entity type manager.
CommentBreadcrumbBuilder::applies public function Whether this breadcrumb builder should be used to build the breadcrumb. Overrides BreadcrumbBuilderInterface::applies
CommentBreadcrumbBuilder::build public function Builds the breadcrumb. Overrides BreadcrumbBuilderInterface::build
CommentBreadcrumbBuilder::__construct public function Constructs the CommentBreadcrumbBuilder.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.

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