function BookBreadcrumbBuilder::build
Same name in other branches
- 9 core/modules/book/src/BookBreadcrumbBuilder.php \Drupal\book\BookBreadcrumbBuilder::build()
- 8.9.x core/modules/book/src/BookBreadcrumbBuilder.php \Drupal\book\BookBreadcrumbBuilder::build()
- 11.x core/modules/book/src/BookBreadcrumbBuilder.php \Drupal\book\BookBreadcrumbBuilder::build()
Overrides BreadcrumbBuilderInterface::build
File
-
core/
modules/ book/ src/ BookBreadcrumbBuilder.php, line 83
Class
- BookBreadcrumbBuilder
- Provides a breadcrumb builder for nodes in a book.
Namespace
Drupal\bookCode
public function build(RouteMatchInterface $route_match) {
$book_nids = [];
$breadcrumb = new Breadcrumb();
$links = [
Link::createFromRoute($this->t('Home'), '<front>', [], [
'language' => $this->languageManager
->getCurrentLanguage(LanguageInterface::TYPE_CONTENT),
]),
];
$breadcrumb->addCacheContexts([
'languages:' . LanguageInterface::TYPE_CONTENT,
]);
$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++;
}
/** @var \Drupal\node\NodeInterface[] $parent_books */
$parent_books = $this->nodeStorage
->loadMultiple($book_nids);
$parent_books = array_map([
$this->entityRepository,
'getTranslationFromContext',
], $parent_books);
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[] = $parent_book->toLink();
}
}
$depth++;
}
}
$breadcrumb->setLinks($links);
$breadcrumb->addCacheContexts([
'route.book_navigation',
]);
return $breadcrumb;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.