Same name and namespace in other branches
  1. 8.9.x core/modules/book/src/BookBreadcrumbBuilder.php \Drupal\book\BookBreadcrumbBuilder
  2. 9 core/modules/book/src/BookBreadcrumbBuilder.php \Drupal\book\BookBreadcrumbBuilder

Provides a breadcrumb builder for nodes in a book.

Hierarchy

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 20

Namespace

Drupal\book
View source
class BookBreadcrumbBuilder implements BreadcrumbBuilderInterface {
  use StringTranslationTrait;

  /**
   * The node storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $nodeStorage;

  /**
   * The current user account.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $account;

  /**
   * The entity repository service.
   *
   * @var \Drupal\Core\Entity\EntityRepositoryInterface
   */
  protected $entityRepository;

  /**
   * The language manager service.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface|null
   */
  protected $languageManager;

  /**
   * Constructs the BookBreadcrumbBuilder.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The current user account.
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
   *   The entity repository service.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, AccountInterface $account, EntityRepositoryInterface $entity_repository, LanguageManagerInterface $language_manager) {
    $this->nodeStorage = $entity_type_manager
      ->getStorage('node');
    $this->account = $account;
    $this->entityRepository = $entity_repository;
    $this->languageManager = $language_manager;
  }

  /**
   * {@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 = [];
    $breadcrumb = new Breadcrumb();
    $links = [
      Link::createFromRoute($this
        ->t('Home'), '<front>', [], [
        'language' => $this->languageManager
          ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT),
      ]),
    ];
    $breadcrumb
      ->addCacheContexts([
      'languages:' . LanguageInterface::TYPE_CONTENT,
    ]);
    $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++;
    }

    /** @var \Drupal\node\NodeInterface[] $parent_books */
    $parent_books = $this->nodeStorage
      ->loadMultiple($book_nids);
    $parent_books = array_map([
      $this->entityRepository,
      'getTranslationFromContext',
    ], $parent_books);
    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]])) {
          $access = $parent_book
            ->access('view', $this->account, TRUE);
          $breadcrumb
            ->addCacheableDependency($access);
          if ($access
            ->isAllowed()) {
            $breadcrumb
              ->addCacheableDependency($parent_book);
            $links[] = $parent_book
              ->toLink();
          }
        }
        $depth++;
      }
    }
    $breadcrumb
      ->setLinks($links);
    $breadcrumb
      ->addCacheContexts([
      'route.book_navigation',
    ]);
    return $breadcrumb;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BookBreadcrumbBuilder::$account protected property The current user account.
BookBreadcrumbBuilder::$entityRepository protected property The entity repository service.
BookBreadcrumbBuilder::$languageManager protected property The language manager service.
BookBreadcrumbBuilder::$nodeStorage protected property The node storage.
BookBreadcrumbBuilder::applies public function Whether this breadcrumb builder should be used to build the breadcrumb. Overrides BreadcrumbBuilderInterface::applies
BookBreadcrumbBuilder::build public function Builds the breadcrumb. Overrides BreadcrumbBuilderInterface::build
BookBreadcrumbBuilder::__construct public function Constructs the BookBreadcrumbBuilder.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 1
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.