nodeStorage = $entity_manager->getStorage('node'); $this->accessManager = $access_manager; $this->account = $account; } /** * {@inheritdoc} */ public function applies(RouteMatchInterface $route_match) { $node = $route_match->getParameter('node'); return $node instanceof NodeInterface && !empty($node->book); } /** * {@inheritdoc} */ public function build(RouteMatchInterface $route_match) { $book_nids = array(); $breadcrumb = new Breadcrumb(); $links = array(Link::createFromRoute($this->t('Home'), '')); $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]])) { if ($parent_book->access('view', $this->account)) { $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; } }