class BookBreadcrumbBuilder in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/book/src/BookBreadcrumbBuilder.php \Drupal\book\BookBreadcrumbBuilder
Provides a breadcrumb builder for nodes in a book.
Hierarchy
- class \Drupal\book\BookBreadcrumbBuilder implements BreadcrumbBuilderInterface uses StringTranslationTrait
Expanded class hierarchy of BookBreadcrumbBuilder
1 string reference to 'BookBreadcrumbBuilder'
- book.services.yml in core/
modules/ book/ book.services.yml - core/modules/book/book.services.yml
1 service uses BookBreadcrumbBuilder
- book.breadcrumb in core/
modules/ book/ book.services.yml - Drupal\book\BookBreadcrumbBuilder
File
- core/
modules/ book/ src/ BookBreadcrumbBuilder.php, line 23 - Contains \Drupal\book\BookBreadcrumbBuilder.
Namespace
Drupal\bookView source
class BookBreadcrumbBuilder implements BreadcrumbBuilderInterface {
use StringTranslationTrait;
/**
* The node storage.
*
* @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.
*
* @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) {
$this->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'), '<front>'),
);
$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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BookBreadcrumbBuilder:: |
protected | property | The access manager. | |
BookBreadcrumbBuilder:: |
protected | property | The current user account. | |
BookBreadcrumbBuilder:: |
protected | property | The node storage. | |
BookBreadcrumbBuilder:: |
public | function |
Whether this breadcrumb builder should be used to build the breadcrumb. Overrides BreadcrumbBuilderInterface:: |
|
BookBreadcrumbBuilder:: |
public | function |
Builds the breadcrumb. Overrides BreadcrumbBuilderInterface:: |
|
BookBreadcrumbBuilder:: |
public | function | Constructs the BookBreadcrumbBuilder. | |
StringTranslationTrait:: |
protected | property | The string translation service. | |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |