Newer
Older
<?php
/**
* @file
* Contains \Drupal\book\BookBreadcrumbBuilder.
*/
namespace Drupal\book;
use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
Angie Byron
committed
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Routing\LinkGeneratorTrait;
Angie Byron
committed
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\node\NodeInterface;
/**
* Provides a breadcrumb builder for nodes in a book.
*/
class BookBreadcrumbBuilder implements BreadcrumbBuilderInterface {
use StringTranslationTrait;
use LinkGeneratorTrait;
/**
catch
committed
* The node storage.
catch
committed
* @var \Drupal\Core\Entity\EntityStorageInterface
protected $nodeStorage;
/**
* The access manager.
*
* @var \Drupal\Core\Access\AccessManagerInterface
*/
protected $accessManager;
/**
* The current user account.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $account;
/**
* Constructs the BookBreadcrumbBuilder.
*
Angie Byron
committed
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager service.
* @param \Drupal\Core\Access\AccessManagerInterface $access_manager
* The access manager.
* @param \Drupal\Core\Session\AccountInterface $account
* The current user account.
public function __construct(EntityManagerInterface $entity_manager, AccessManagerInterface $access_manager, AccountInterface $account) {
catch
committed
$this->nodeStorage = $entity_manager->getStorage('node');
$this->accessManager = $access_manager;
$this->account = $account;
}
Dries Buytaert
committed
/**
* {@inheritdoc}
*/
Angie Byron
committed
public function applies(RouteMatchInterface $route_match) {
$node = $route_match->getParameter('node');
return $node instanceof NodeInterface && !empty($node->book);
Dries Buytaert
committed
}
/**
* {@inheritdoc}
*/
Angie Byron
committed
public function build(RouteMatchInterface $route_match) {
$book_nids = array();
Dries Buytaert
committed
$links = array($this->l($this->t('Home'), '<front>'));
Angie Byron
committed
$book = $route_match->getParameter('node')->book;
Dries Buytaert
committed
$depth = 1;
// We skip the current node.
while (!empty($book['p' . ($depth + 1)])) {
$book_nids[] = $book['p' . $depth];
Dries Buytaert
committed
$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[] = $this->l($parent_book->label(), 'node.view', array('node' => $parent_book->id()));
}
}
Dries Buytaert
committed
$depth++;
}
}
Dries Buytaert
committed
return $links;