BookBreadcrumbBuilder::build

public BookBreadcrumbBuilder::build(RouteMatchInterface $route_match)

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

core/modules/book/src/BookBreadcrumbBuilder.php, line 58

Class

BookBreadcrumbBuilder
Provides a breadcrumb builder for nodes in a book.

Namespace

Drupal\book

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public function build(RouteMatchInterface $route_match) {
  $book_nids = array();
  $breadcrumb = new Breadcrumb();
 
  $links = array(Link::createFromRoute($this->t('Home'), '<front>'));
  $book = $route_match->getParameter('node')->book;
  $depth = 1;
  // We skip the current node.
  while (!empty($book['p' . ($depth + 1)])) {
    $book_nids[] = $book['p' . $depth];
    $depth++;
  }
  $parent_books = $this->nodeStorage->loadMultiple($book_nids);
  if (count($parent_books) > 0) {
    $depth = 1;
    while (!empty($book['p' . ($depth + 1)])) {
      if (!empty($parent_books[$book['p' . $depth]]) && ($parent_book = $parent_books[$book['p' . $depth]])) {
        $access = $parent_book->access('view', $this->account, TRUE);
        $breadcrumb->addCacheableDependency($access);
        if ($access->isAllowed()) {
          $breadcrumb->addCacheableDependency($parent_book);
          $links[] = Link::createFromRoute($parent_book->label(), 'entity.node.canonical', array('node' => $parent_book->id()));
        }
      }
      $depth++;
    }
  }
  $breadcrumb->setLinks($links);
  $breadcrumb->addCacheContexts(['route.book_navigation']);
  return $breadcrumb;
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.