class ForumListingBreadcrumbBuilder

Provides a breadcrumb builder base class for forum listing pages.

Hierarchy

Expanded class hierarchy of ForumListingBreadcrumbBuilder

1 file declares its use of ForumListingBreadcrumbBuilder
ForumListingBreadcrumbBuilderTest.php in core/modules/forum/tests/src/Unit/Breadcrumb/ForumListingBreadcrumbBuilderTest.php
1 string reference to 'ForumListingBreadcrumbBuilder'
forum.services.yml in core/modules/forum/forum.services.yml
core/modules/forum/forum.services.yml
1 service uses ForumListingBreadcrumbBuilder
forum.breadcrumb.listing in core/modules/forum/forum.services.yml
Drupal\forum\Breadcrumb\ForumListingBreadcrumbBuilder

File

core/modules/forum/src/Breadcrumb/ForumListingBreadcrumbBuilder.php, line 11

Namespace

Drupal\forum\Breadcrumb
View source
class ForumListingBreadcrumbBuilder extends ForumBreadcrumbBuilderBase {
  
  /**
   * {@inheritdoc}
   */
  public function applies(RouteMatchInterface $route_match) {
    return $route_match->getRouteName() == 'forum.page' && $route_match->getParameter('taxonomy_term');
  }
  
  /**
   * {@inheritdoc}
   */
  public function build(RouteMatchInterface $route_match) {
    $breadcrumb = parent::build($route_match);
    $breadcrumb->addCacheContexts([
      'route',
    ]);
    // Add all parent forums to breadcrumbs.
    /** @var \Drupal\taxonomy\TermInterface $term */
    $term = $route_match->getParameter('taxonomy_term');
    $term_id = $term->id();
    $breadcrumb->addCacheableDependency($term);
    $parents = $this->termStorage
      ->loadAllParents($term_id);
    if ($parents) {
      foreach (array_reverse($parents) as $parent) {
        if ($parent->id() != $term_id) {
          $breadcrumb->addCacheableDependency($parent);
          $breadcrumb->addLink(Link::createFromRoute($parent->label(), 'forum.page', [
            'taxonomy_term' => $parent->id(),
          ]));
        }
      }
    }
    return $breadcrumb;
  }

}

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