You are here

class SocialCommentBreadcrumbBuilder in Open Social 10.0.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_comment/src/SocialCommentBreadcrumbBuilder.php \Drupal\social_comment\SocialCommentBreadcrumbBuilder
  2. 8 modules/social_features/social_comment/src/SocialCommentBreadcrumbBuilder.php \Drupal\social_comment\SocialCommentBreadcrumbBuilder
  3. 8.2 modules/social_features/social_comment/src/SocialCommentBreadcrumbBuilder.php \Drupal\social_comment\SocialCommentBreadcrumbBuilder
  4. 8.3 modules/social_features/social_comment/src/SocialCommentBreadcrumbBuilder.php \Drupal\social_comment\SocialCommentBreadcrumbBuilder
  5. 8.4 modules/social_features/social_comment/src/SocialCommentBreadcrumbBuilder.php \Drupal\social_comment\SocialCommentBreadcrumbBuilder
  6. 8.5 modules/social_features/social_comment/src/SocialCommentBreadcrumbBuilder.php \Drupal\social_comment\SocialCommentBreadcrumbBuilder
  7. 8.6 modules/social_features/social_comment/src/SocialCommentBreadcrumbBuilder.php \Drupal\social_comment\SocialCommentBreadcrumbBuilder
  8. 8.7 modules/social_features/social_comment/src/SocialCommentBreadcrumbBuilder.php \Drupal\social_comment\SocialCommentBreadcrumbBuilder
  9. 8.8 modules/social_features/social_comment/src/SocialCommentBreadcrumbBuilder.php \Drupal\social_comment\SocialCommentBreadcrumbBuilder
  10. 10.3.x modules/social_features/social_comment/src/SocialCommentBreadcrumbBuilder.php \Drupal\social_comment\SocialCommentBreadcrumbBuilder
  11. 10.1.x modules/social_features/social_comment/src/SocialCommentBreadcrumbBuilder.php \Drupal\social_comment\SocialCommentBreadcrumbBuilder
  12. 10.2.x modules/social_features/social_comment/src/SocialCommentBreadcrumbBuilder.php \Drupal\social_comment\SocialCommentBreadcrumbBuilder

Class to define the comment breadcrumb builder.

Hierarchy

Expanded class hierarchy of SocialCommentBreadcrumbBuilder

1 string reference to 'SocialCommentBreadcrumbBuilder'
social_comment.services.yml in modules/social_features/social_comment/social_comment.services.yml
modules/social_features/social_comment/social_comment.services.yml
1 service uses SocialCommentBreadcrumbBuilder
social_comment.breadcrumb in modules/social_features/social_comment/social_comment.services.yml
Drupal\social_comment\SocialCommentBreadcrumbBuilder

File

modules/social_features/social_comment/src/SocialCommentBreadcrumbBuilder.php, line 17

Namespace

Drupal\social_comment
View source
class SocialCommentBreadcrumbBuilder implements BreadcrumbBuilderInterface {
  use StringTranslationTrait;

  /**
   * The comment storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $storage;

  /**
   * Constructs the SocialCommentBreadcrumbBuilder.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
   *   The entity manager.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function __construct(EntityTypeManagerInterface $entity_manager) {
    $this->storage = $entity_manager
      ->getStorage('comment');
  }

  /**
   * {@inheritdoc}
   */
  public function applies(RouteMatchInterface $route_match) {
    $comments_routes = [
      'comment.reply',
      'entity.comment.edit_form',
      'entity.comment.delete_form',
    ];
    return in_array($route_match
      ->getRouteName(), $comments_routes);
  }

  /**
   * {@inheritdoc}
   */
  public function build(RouteMatchInterface $route_match) {
    $breadcrumb = new Breadcrumb();
    $breadcrumb
      ->addCacheContexts([
      'route',
    ]);
    $breadcrumb
      ->addLink(Link::createFromRoute($this
      ->t('Home'), '<front>'));
    $breadcrumb
      ->addLink(Link::createFromRoute($this
      ->t('Home'), '<front>'));
    switch ($route_match
      ->getRouteName()) {
      case 'comment.reply':
        $page_title = $this
          ->t('Reply to Comment');
        $pid = $route_match
          ->getParameter('pid');
        $comment = $this->storage
          ->load($pid);
        break;
      case 'entity.comment.edit_form':
        $page_title = $this
          ->t('Edit Comment');
        $comment = $route_match
          ->getParameter('comment');
        break;
      case 'entity.comment.delete_form':
        $page_title = $this
          ->t('Delete Comment');
        $comment = $route_match
          ->getParameter('comment');
        break;
      default:
        $page_title = $this
          ->t('Comment');
    }

    // Add Entity path to Breadcrumb for Reply.
    if ($route_match
      ->getParameter('entity') && $route_match
      ->getParameter('entity') instanceof EntityInterface) {
      $entity = $route_match
        ->getParameter('entity');
      $breadcrumb
        ->addLink(new Link($entity
        ->label(), $entity
        ->toUrl()));
      $breadcrumb
        ->addCacheableDependency($entity);
    }

    // Add Caching.
    if ($comment) {
      $breadcrumb
        ->addCacheableDependency($comment);
    }

    // Display link to current page.
    $breadcrumb
      ->addLink(new Link($page_title, Url::fromRoute('<current>')));
    return $breadcrumb;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SocialCommentBreadcrumbBuilder::$storage protected property The comment storage.
SocialCommentBreadcrumbBuilder::applies public function Whether this breadcrumb builder should be used to build the breadcrumb. Overrides BreadcrumbBuilderInterface::applies
SocialCommentBreadcrumbBuilder::build public function Builds the breadcrumb. Overrides BreadcrumbBuilderInterface::build
SocialCommentBreadcrumbBuilder::__construct public function Constructs the SocialCommentBreadcrumbBuilder.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
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.