You are here

public function ForumListingBreadcrumbBuilder::build in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/forum/src/Breadcrumb/ForumListingBreadcrumbBuilder.php \Drupal\forum\Breadcrumb\ForumListingBreadcrumbBuilder::build()

Builds the breadcrumb.

Parameters

\Drupal\Core\Routing\RouteMatchInterface $route_match: The current route match.

Return value

\Drupal\Core\Breadcrumb\Breadcrumb A breadcrumb.

Overrides ForumBreadcrumbBuilderBase::build

File

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

Class

ForumListingBreadcrumbBuilder
Provides a breadcrumb builder base class for forum listing pages.

Namespace

Drupal\forum\Breadcrumb

Code

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;
}