You are here

public function ContextBreadcrumbBuilder::build in Context Active Trail 8

Same name and namespace in other branches
  1. 8.2 src/ContextBreadcrumbBuilder.php \Drupal\context_active_trail\ContextBreadcrumbBuilder::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

src/ContextBreadcrumbBuilder.php, line 99

Class

ContextBreadcrumbBuilder
Build breadcrumbs based on active trail from context.

Namespace

Drupal\context_active_trail

Code

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

  // Start with home page.
  $breadcrumb
    ->addLink(Link::createFromRoute(t('Home'), '<front>'));

  // Add links from menu.
  $link_ids = array_filter($this->activeTrail
    ->getActiveTrailIds(NULL));
  foreach (array_reverse($link_ids) as $link_id) {
    $link = $this->linkManager
      ->getInstance([
      'id' => $link_id,
    ]);
    $breadcrumb
      ->addLink(Link::fromTextAndUrl($link
      ->getTitle(), $link
      ->getUrlObject()));
  }

  // Include current page title.
  if ($this->configuration['breadcrumb_title']) {
    $title = $this->titleResolver
      ->getTitle($this->request, $route_match
      ->getRouteObject());
    $breadcrumb
      ->addLink(Link::createFromRoute($title, '<none>'));
  }
  return $breadcrumb;
}