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