ForumListingBreadcrumbBuilder.php

Same filename and directory in other branches
  1. 9 core/modules/forum/src/Breadcrumb/ForumListingBreadcrumbBuilder.php
  2. 8.9.x core/modules/forum/src/Breadcrumb/ForumListingBreadcrumbBuilder.php
  3. 10 core/modules/forum/src/Breadcrumb/ForumListingBreadcrumbBuilder.php

Namespace

Drupal\forum\Breadcrumb

File

core/modules/forum/src/Breadcrumb/ForumListingBreadcrumbBuilder.php

View source
<?php

namespace Drupal\forum\Breadcrumb;

use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Provides a breadcrumb builder base class for forum listing pages.
 */
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;
    }

}

Classes

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

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