You are here

public function ThunderArticleBreadcrumbBuilder::build in Thunder 8.2

Same name and namespace in other branches
  1. 8.5 modules/thunder_article/src/Breadcrumb/ThunderArticleBreadcrumbBuilder.php \Drupal\thunder_article\Breadcrumb\ThunderArticleBreadcrumbBuilder::build()
  2. 8.3 modules/thunder_article/src/Breadcrumb/ThunderArticleBreadcrumbBuilder.php \Drupal\thunder_article\Breadcrumb\ThunderArticleBreadcrumbBuilder::build()
  3. 8.4 modules/thunder_article/src/Breadcrumb/ThunderArticleBreadcrumbBuilder.php \Drupal\thunder_article\Breadcrumb\ThunderArticleBreadcrumbBuilder::build()
  4. 6.2.x modules/thunder_article/src/Breadcrumb/ThunderArticleBreadcrumbBuilder.php \Drupal\thunder_article\Breadcrumb\ThunderArticleBreadcrumbBuilder::build()
  5. 6.0.x modules/thunder_article/src/Breadcrumb/ThunderArticleBreadcrumbBuilder.php \Drupal\thunder_article\Breadcrumb\ThunderArticleBreadcrumbBuilder::build()
  6. 6.1.x modules/thunder_article/src/Breadcrumb/ThunderArticleBreadcrumbBuilder.php \Drupal\thunder_article\Breadcrumb\ThunderArticleBreadcrumbBuilder::build()

Builds the breadcrumb.

Parameters

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

Return value

\Drupal\Core\Breadcrumb\Breadcrumb A breadcrumb.

Overrides BreadcrumbBuilderInterface::build

File

modules/thunder_article/src/Breadcrumb/ThunderArticleBreadcrumbBuilder.php, line 122

Class

ThunderArticleBreadcrumbBuilder
Class to define the menu_link breadcrumb builder.

Namespace

Drupal\thunder_article\Breadcrumb

Code

public function build(RouteMatchInterface $route_match) {
  $breadcrumb = new Breadcrumb();
  $breadcrumb
    ->addCacheContexts([
    'route',
  ]);

  // Add all parent forums to breadcrumbs.

  /** @var Node $node */
  $node = $route_match
    ->getParameter('node');
  $breadcrumb
    ->addCacheableDependency($node);

  // Add all parent forums to breadcrumbs.

  /** @var \Drupal\Taxonomy\TermInterface $term */
  $term = !empty($node->field_channel) ? $node->field_channel->entity : '';
  $links = [];
  if ($term) {
    $breadcrumb
      ->addCacheableDependency($term);
    $channels = $this->termStorage
      ->loadAllParents($term
      ->id());
    foreach (array_reverse($channels) as $term) {
      $term = $this->entityRepository
        ->getTranslationFromContext($term);
      $breadcrumb
        ->addCacheableDependency($term);
      $links[] = Link::createFromRoute($term
        ->getName(), 'entity.taxonomy_term.canonical', [
        'taxonomy_term' => $term
          ->id(),
      ]);
    }
  }
  if (!$links || '/' . $links[0]
    ->getUrl()
    ->getInternalPath() != $this->configFactory
    ->get('system.site')
    ->get('page.front')) {
    array_unshift($links, Link::createFromRoute($this
      ->t('Home'), '<front>'));
  }
  return $breadcrumb
    ->setLinks($links);
}