/** * {@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->termStorage->loadAllParents($term->id()); // Remove current term being accessed. array_shift($parents); foreach (array_reverse($parents) as $term) { $term = $this->entityManager->getTranslationFromContext($term); $breadcrumb->addCacheableDependency($term); $breadcrumb->addLink(Link::createFromRoute($term->getName(), 'entity.taxonomy_term.canonical', array('taxonomy_term' => $term->id()))); } // This breadcrumb builder is based on a route parameter, and hence it // depends on the 'route' cache context. $breadcrumb->setCacheContexts(['route']); return $breadcrumb; }
/** * {@inheritdoc} */ public function build(RouteMatchInterface $route_match) { $breadcrumb = new Breadcrumb(); $breadcrumb->setCacheContexts(['route']); $links[] = Link::createFromRoute($this->t('Home'), '<front>'); $vocabulary = $this->entityManager->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 build(RouteMatchInterface $route_match) { $breadcrumb = new Breadcrumb(); $breadcrumb->setCacheContexts(['route']); $breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '<front>')); $entity = $route_match->getParameter('entity'); $breadcrumb->addLink(new Link($entity->label(), $entity->urlInfo())); $breadcrumb->addCacheableDependency($entity); if (($pid = $route_match->getParameter('pid')) && ($comment = $this->storage->load($pid))) { /** @var \Drupal\comment\CommentInterface $comment */ $breadcrumb->addCacheableDependency($comment); // Display link to parent comment. // @todo Clean-up permalink in https://www.drupal.org/node/2198041 $breadcrumb->addLink(new Link($comment->getSubject(), $comment->urlInfo())); } return $breadcrumb; }
/** * Tests multiple breadcrumb builders of which one returns NULL. */ public function testBuildWithOneNotApplyingBuilders() { $builder1 = $this->getMock('Drupal\\Core\\Breadcrumb\\BreadcrumbBuilderInterface'); $builder1->expects($this->once())->method('applies')->will($this->returnValue(FALSE)); $builder1->expects($this->never())->method('build'); $builder2 = $this->getMock('Drupal\\Core\\Breadcrumb\\BreadcrumbBuilderInterface'); $links2 = ['<a href="/example2">Test2</a>']; $this->breadcrumb->setLinks($links2); $this->breadcrumb->setCacheContexts(['baz'])->setCacheTags(['qux']); $builder2->expects($this->once())->method('applies')->will($this->returnValue(TRUE)); $builder2->expects($this->once())->method('build')->willReturn($this->breadcrumb); $route_match = $this->getMock('Drupal\\Core\\Routing\\RouteMatchInterface'); $this->moduleHandler->expects($this->once())->method('alter')->with('system_breadcrumb', $this->breadcrumb, $route_match, array('builder' => $builder2)); $this->breadcrumbManager->addBuilder($builder1, 10); $this->breadcrumbManager->addBuilder($builder2, 0); $breadcrumb = $this->breadcrumbManager->build($route_match); $this->assertEquals($links2, $breadcrumb->getLinks()); $this->assertEquals(['baz'], $breadcrumb->getCacheContexts()); $this->assertEquals(['qux'], $breadcrumb->getCacheTags()); $this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge()); }
/** * {@inheritdoc} */ public function build(RouteMatchInterface $route_match) { $breadcrumb = new Breadcrumb(); $links = array(); // General path-based breadcrumbs. Use the actual request path, prior to // resolving path aliases, so the breadcrumb can be defined by simply // creating a hierarchy of path aliases. $path = trim($this->context->getPathInfo(), '/'); $path_elements = explode('/', $path); $exclude = array(); // Don't show a link to the front-page path. $front = $this->config->get('page.front'); $exclude[$front] = TRUE; // /user is just a redirect, so skip it. // @todo Find a better way to deal with /user. $exclude['/user'] = TRUE; // Because this breadcrumb builder is entirely path-based, vary by the // 'url.path' cache context. $breadcrumb->setCacheContexts(['url.path']); while (count($path_elements) > 1) { array_pop($path_elements); // Copy the path elements for up-casting. $route_request = $this->getRequestForPath('/' . implode('/', $path_elements), $exclude); if ($route_request) { $route_match = RouteMatch::createFromRequest($route_request); $access = $this->accessManager->check($route_match, $this->currentUser, NULL, TRUE); // The set of breadcrumb links depends on the access result, so merge // the access result's cacheability metadata. $breadcrumb = $breadcrumb->addCacheableDependency($access); if ($access->isAllowed()) { $title = $this->titleResolver->getTitle($route_request, $route_match->getRouteObject()); if (!isset($title)) { // Fallback to using the raw path component as the title if the // route is missing a _title or _title_callback attribute. $title = str_replace(array('-', '_'), ' ', Unicode::ucfirst(end($path_elements))); } $url = Url::fromRouteMatch($route_match); $links[] = new Link($title, $url); } } } if ($path && '/' . $path != $front) { // Add the Home link, except for the front page. $links[] = Link::createFromRoute($this->t('Home'), '<front>'); } return $breadcrumb->setLinks(array_reverse($links)); }
/** * {@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->setCacheContexts(['route.book_navigation']); return $breadcrumb; }