Breadcrumb example

/** * {@inheritdoc} */
  public function applies(RouteMatchInterface $route_match) {
    return TRUE;
  }

  /** * {@inheritdoc} */
  public function build(RouteMatchInterface $route_match) {
    $breadcrumb = new Breadcrumb();
    $context = ['builder' => NULL];
    // Call the build method of registered breadcrumb builders,     // until one of them returns an array.     foreach ($this->getSortedBuilders() as $builder) {
      if (!$builder->applies($route_match)) {
        // The builder does not apply, so we continue with the other builders.         continue;
      }

      $breadcrumb = $builder->build($route_match);

      
/** * @coversDefaultClass \Drupal\Core\Breadcrumb\Breadcrumb * @group Breadcrumb */
class BreadcrumbTest extends UnitTestCase {

  /** * @covers ::setLinks */
  public function testSetLinks() {
    $breadcrumb = new Breadcrumb();
    $breadcrumb->setLinks([new Link('Home', Url::fromRoute('<front>'))]);
    $this->expectException(\LogicException::class);
    $this->expectExceptionMessage('Once breadcrumb links are set, only additional breadcrumb links can be added.');
    $breadcrumb->setLinks([new Link('None', Url::fromRoute('<none>'))]);
  }

}
$this->entityTypeManager = $entity_type_manager;
    $this->config = $config_factory->get('forum.settings');
    $this->forumManager = $forum_manager;
    $this->setStringTranslation($string_translation);
    $this->termStorage = $entity_type_manager->getStorage('taxonomy_term');
  }

  /** * {@inheritdoc} */
  public function build(RouteMatchInterface $route_match) {
    $breadcrumb = new Breadcrumb();
    $breadcrumb->addCacheContexts(['route']);

    $links[] = Link::createFromRoute($this->t('Home'), '<front>');

    $vocabulary = $this->entityTypeManager
      ->getStorage('taxonomy_vocabulary')
      ->load($this->config->get('vocabulary'));
    $breadcrumb->addCacheableDependency($vocabulary);
    $links[] = Link::createFromRoute($vocabulary->label(), 'forum.index');

    return $breadcrumb->setLinks($links);
  }
/** * {@inheritdoc} */
  public function applies(RouteMatchInterface $route_match) {
    return TRUE;
  }

  /** * {@inheritdoc} */
  public function build(RouteMatchInterface $route_match) {
    $breadcrumb = new Breadcrumb();
    $links = [];

    // Add the url.path.parent cache context. This code ignores the last path     // part so the result only depends on the path parents.     $breadcrumb->addCacheContexts(['url.path.parent', 'url.path.is_front']);

    // Do not display a breadcrumb on the frontpage.     if ($this->pathMatcher->isFrontPage()) {
      return $breadcrumb;
    }

    

  public function applies(RouteMatchInterface $route_match) {
    return $route_match->getRouteName() == 'entity.taxonomy_term.canonical'
      && $route_match->getParameter('taxonomy_term') instanceof TermInterface;
  }

  /** * {@inheritdoc} */
  public function build(RouteMatchInterface $route_match) {
    $breadcrumb = new Breadcrumb();
    $breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '<front>'));
    $term = $route_match->getParameter('taxonomy_term');
    // Breadcrumb needs to have terms cacheable metadata as a cacheable     // dependency even though it is not shown in the breadcrumb because e.g. its     // parent might have changed.     $breadcrumb->addCacheableDependency($term);
    // @todo This overrides any other possible breadcrumb and is a pure     // hard-coded presumption. Make this behavior configurable per     // vocabulary or term.     $parents = $this->entityTypeManager->getStorage('taxonomy_term')->loadAllParents($term->id());
    // Remove current term being accessed.
/** * {@inheritdoc} */
  public function applies(RouteMatchInterface $route_match) {
    return $route_match->getRouteName() == 'comment.reply' && $route_match->getParameter('entity');
  }

  /** * {@inheritdoc} */
  public function build(RouteMatchInterface $route_match) {
    $breadcrumb = new Breadcrumb();
    $breadcrumb->addCacheContexts(['route']);
    $breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '<front>'));

    $entity = $route_match->getParameter('entity');
    $breadcrumb->addLink(new Link($entity->label()$entity->toUrl()));
    $breadcrumb->addCacheableDependency($entity);

    if (($pid = $route_match->getParameter('pid')) && ($comment = $this->entityTypeManager->getStorage('comment')->load($pid))) {
      /** @var \Drupal\comment\CommentInterface $comment */
      $breadcrumb->addCacheableDependency($comment);
      // Display link to parent comment.

  protected $moduleHandler;

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    $this->moduleHandler = $this->createMock('Drupal\Core\Extension\ModuleHandlerInterface');
    $this->breadcrumbManager = new BreadcrumbManager($this->moduleHandler);
    $this->breadcrumb = new Breadcrumb();

    $this->container = new ContainerBuilder();
    $cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
    $cache_contexts_manager->assertValidTokens()->willReturn(TRUE);
    $cache_contexts_manager->reveal();
    $this->container->set('cache_contexts_manager', $cache_contexts_manager);
    \Drupal::setContainer($this->container);
  }

  /** * Tests the breadcrumb manager without any set breadcrumb. */

  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];
      
/** * {@inheritdoc} */
  public function applies(RouteMatchInterface $route_match) {
    return $route_match->getRouteName() == 'help.help_topic';
  }

  /** * {@inheritdoc} */
  public function build(RouteMatchInterface $route_match) {
    $breadcrumb = new Breadcrumb();
    $breadcrumb->addCacheContexts(['url.path.parent']);
    $breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '<front>'));
    $breadcrumb->addLink(Link::createFromRoute($this->t('Administration'), 'system.admin'));
    $breadcrumb->addLink(Link::createFromRoute($this->t('Help'), 'help.main'));

    return $breadcrumb;
  }

}

  protected static $modules = [
    'layout_builder',
    'layout_discovery',
  ];

  /** * Check that there are no errors when alter called with null route match. */
  public function testBreadcrumbAlterNullRouteMatch() {
    $breadcrumb = new Breadcrumb();
    $route_match = new NullRouteMatch();
    layout_builder_system_breadcrumb_alter($breadcrumb$route_match[]);
  }

}
Home | Imprint | This part of the site doesn't use cookies.