You are here

class BlogBreadcrumbBuilder in Blog 3.x

Same name and namespace in other branches
  1. 8.2 src/BlogBreadcrumbBuilder.php \Drupal\blog\BlogBreadcrumbBuilder

Build blog-specific breadcrumb.

Hierarchy

Expanded class hierarchy of BlogBreadcrumbBuilder

1 string reference to 'BlogBreadcrumbBuilder'
blog.services.yml in ./blog.services.yml
blog.services.yml
1 service uses BlogBreadcrumbBuilder
blog.breadcrumb in ./blog.services.yml
Drupal\blog\BlogBreadcrumbBuilder

File

src/BlogBreadcrumbBuilder.php, line 15

Namespace

Drupal\blog
View source
class BlogBreadcrumbBuilder implements BreadcrumbBuilderInterface {
  use StringTranslationTrait;

  /**
   * @var \Drupal\blog\BlogListerInterface
   */
  protected $blogLister;

  /**
   * {@inheritdoc}
   */
  public function __construct(BlogListerInterface $blogLister) {
    $this->blogLister = $blogLister;
  }

  /**
   * {@inheritdoc}
   */
  public function applies(RouteMatchInterface $route_match) {
    if ($route_match
      ->getRouteName() == 'entity.node.canonical') {

      /**
      * @var \Drupal\node\NodeInterface $node
      */
      $node = $route_match
        ->getParameter('node');
      return $node
        ->bundle() == 'blog_post';
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function build(RouteMatchInterface $route_match) {

    /**
    * @var \Drupal\node\NodeInterface $node
    */
    $node = $route_match
      ->getParameter('node');
    $breadcrumb = new Breadcrumb();
    $breadcrumb
      ->addCacheContexts([
      'route',
    ]);
    $links[] = Link::createFromRoute($this
      ->t('Home'), '<front>');
    $links[] = Link::fromTextAndUrl($this
      ->t('Blogs'), Url::fromUri('internal:/blog'));
    $title_text = $this->blogLister
      ->userBlogTitle($node
      ->getOwner());
    $blog_url = Url::fromUri('internal:/blog/' . $node
      ->getOwnerId());
    $links[] = Link::fromTextAndUrl($title_text, $blog_url);
    return $breadcrumb
      ->setLinks($links);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlogBreadcrumbBuilder::$blogLister protected property
BlogBreadcrumbBuilder::applies public function Whether this breadcrumb builder should be used to build the breadcrumb. Overrides BreadcrumbBuilderInterface::applies
BlogBreadcrumbBuilder::build public function Builds the breadcrumb. Overrides BreadcrumbBuilderInterface::build
BlogBreadcrumbBuilder::__construct public function
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.