public function build()

in modules/quanthub_breadcrumbs/src/BooksBreadcrumbBuilder.php [64:114]


  public function build(RouteMatchInterface $route_match): Breadcrumb {
    $breadcrumb = new Breadcrumb();
    $currentLanguage = $this->languageManager
      ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
      ->getId();

    /** @var \Drupal\node\Entity\Node $node */
    $node = $route_match->getParameter('node');
    $breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '<front>'));

    if ($node->bundle() == 'book') {
      // Book Name breadcrumb item for all children elements.
      $bookId = $node->book['bid'];
      $bookNode = $this->entityTypeManager->load($bookId);

      if ($bookNode instanceof NodeInterface && $bookNode->hasTranslation($currentLanguage)) {
        $breadcrumb->addLink($bookNode->getTranslation($currentLanguage)->toLink());
      }
      $breadcrumb->addCacheableDependency($bookNode);
    }

    if ($node->bundle() == 'book_content') {
      // Get first referenced node.
      $referencedPageId = $node->field_book_page_ref->first()->getString();
      $referencedNode = $this->entityTypeManager->load($referencedPageId);

      // Get book node by id from referenced node.
      $bookId = $referencedNode->book['bid'];
      if (!empty($bookId)) {
        $bookNode = $this->entityTypeManager->load($bookId);
      }
      // Book Name item.
      if ($bookNode instanceof NodeInterface && $bookNode->hasTranslation($currentLanguage)) {
        $breadcrumb->addLink($bookNode->getTranslation($currentLanguage)->toLink());
      }
      // Book content node Title item.
      $breadcrumb->addLink(Link::createFromRoute($node->getTitle(), '<none>'));
      $breadcrumb->addCacheableDependency($bookNode);
    }

    $parameters = $route_match->getParameters();
    foreach ($parameters as $parameter) {
      if ($parameter instanceof CacheableDependencyInterface) {
        $breadcrumb->addCacheableDependency($parameter);
      }
    }

    $breadcrumb->addCacheContexts(['route', 'url.path', 'languages']);

    return $breadcrumb;
  }