You are here

public function HierarchyBreadcrumbBuilder::build in Entity Reference Hierarchy 8

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/HierarchyBreadcrumbBuilder.php, line 43
Contains \Drupal\entity_hierarchy\HierarchyBreadcrumbBuilder.

Class

HierarchyBreadcrumbBuilder
Provides a breadcrumb builder for nodes in a book.

Namespace

Drupal\entity_hierarchy

Code

public function build(RouteMatchInterface $route_match) {

  // Get all the possible breadcrumbs for the node.
  $node = $route_match
    ->getParameter('node');
  $nid = $node->nid->value;
  $trail = $this
    ->hierarchyGetNodePrimaryAncestorNodes($nid);
  if ($trail) {
    $breadcrumb = new Breadcrumb();
    $links = [
      Link::createFromRoute($this
        ->t('Home'), '<front>'),
    ];
    foreach ($trail as $node) {
      $options = array();
      $links[] = $node
        ->toLink($node
        ->getTitle(), 'canonical', $options);
    }
    $breadcrumb
      ->addCacheableDependency($node);
    $breadcrumb
      ->setLinks($links);
    $breadcrumb
      ->addCacheContexts([
      'route',
    ]);
    return $breadcrumb;
  }
  else {
    return;
  }
}