Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\book;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Breadcrumb\Breadcrumb;
|
Chris@0
|
6 use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
|
Chris@18
|
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
|
Chris@0
|
8 use Drupal\Core\Link;
|
Chris@0
|
9 use Drupal\Core\Routing\RouteMatchInterface;
|
Chris@0
|
10 use Drupal\Core\Session\AccountInterface;
|
Chris@0
|
11 use Drupal\Core\StringTranslation\StringTranslationTrait;
|
Chris@0
|
12 use Drupal\node\NodeInterface;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Provides a breadcrumb builder for nodes in a book.
|
Chris@0
|
16 */
|
Chris@0
|
17 class BookBreadcrumbBuilder implements BreadcrumbBuilderInterface {
|
Chris@0
|
18 use StringTranslationTrait;
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * The node storage.
|
Chris@0
|
22 *
|
Chris@0
|
23 * @var \Drupal\Core\Entity\EntityStorageInterface
|
Chris@0
|
24 */
|
Chris@0
|
25 protected $nodeStorage;
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * The current user account.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @var \Drupal\Core\Session\AccountInterface
|
Chris@0
|
31 */
|
Chris@0
|
32 protected $account;
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * Constructs the BookBreadcrumbBuilder.
|
Chris@0
|
36 *
|
Chris@18
|
37 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
|
Chris@18
|
38 * The entity type manager service.
|
Chris@0
|
39 * @param \Drupal\Core\Session\AccountInterface $account
|
Chris@0
|
40 * The current user account.
|
Chris@0
|
41 */
|
Chris@18
|
42 public function __construct(EntityTypeManagerInterface $entity_type_manager, AccountInterface $account) {
|
Chris@18
|
43 $this->nodeStorage = $entity_type_manager->getStorage('node');
|
Chris@0
|
44 $this->account = $account;
|
Chris@0
|
45 }
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * {@inheritdoc}
|
Chris@0
|
49 */
|
Chris@0
|
50 public function applies(RouteMatchInterface $route_match) {
|
Chris@0
|
51 $node = $route_match->getParameter('node');
|
Chris@0
|
52 return $node instanceof NodeInterface && !empty($node->book);
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * {@inheritdoc}
|
Chris@0
|
57 */
|
Chris@0
|
58 public function build(RouteMatchInterface $route_match) {
|
Chris@0
|
59 $book_nids = [];
|
Chris@0
|
60 $breadcrumb = new Breadcrumb();
|
Chris@0
|
61
|
Chris@0
|
62 $links = [Link::createFromRoute($this->t('Home'), '<front>')];
|
Chris@0
|
63 $book = $route_match->getParameter('node')->book;
|
Chris@0
|
64 $depth = 1;
|
Chris@0
|
65 // We skip the current node.
|
Chris@0
|
66 while (!empty($book['p' . ($depth + 1)])) {
|
Chris@0
|
67 $book_nids[] = $book['p' . $depth];
|
Chris@0
|
68 $depth++;
|
Chris@0
|
69 }
|
Chris@0
|
70 $parent_books = $this->nodeStorage->loadMultiple($book_nids);
|
Chris@0
|
71 if (count($parent_books) > 0) {
|
Chris@0
|
72 $depth = 1;
|
Chris@0
|
73 while (!empty($book['p' . ($depth + 1)])) {
|
Chris@0
|
74 if (!empty($parent_books[$book['p' . $depth]]) && ($parent_book = $parent_books[$book['p' . $depth]])) {
|
Chris@0
|
75 $access = $parent_book->access('view', $this->account, TRUE);
|
Chris@0
|
76 $breadcrumb->addCacheableDependency($access);
|
Chris@0
|
77 if ($access->isAllowed()) {
|
Chris@0
|
78 $breadcrumb->addCacheableDependency($parent_book);
|
Chris@0
|
79 $links[] = Link::createFromRoute($parent_book->label(), 'entity.node.canonical', ['node' => $parent_book->id()]);
|
Chris@0
|
80 }
|
Chris@0
|
81 }
|
Chris@0
|
82 $depth++;
|
Chris@0
|
83 }
|
Chris@0
|
84 }
|
Chris@0
|
85 $breadcrumb->setLinks($links);
|
Chris@0
|
86 $breadcrumb->addCacheContexts(['route.book_navigation']);
|
Chris@0
|
87 return $breadcrumb;
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 }
|