Skip to content

Instantly share code, notes, and snippets.

@DuaelFr
Last active March 22, 2016 11:02
Show Gist options
  • Save DuaelFr/b3a11bcdb74ad12ed581 to your computer and use it in GitHub Desktop.
Save DuaelFr/b3a11bcdb74ad12ed581 to your computer and use it in GitHub Desktop.
D8 breadcrumb following menus
<?php
/**
* @file
* Contains \Drupal\mymodule\MyBreadcrumbBuilder.
*/
namespace Drupal\mymodule;
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Controller\TitleResolverInterface;
use Drupal\Core\Link;
use Drupal\Core\Menu\MenuActiveTrailInterface;
use Drupal\Core\Menu\MenuLinkManagerInterface;
use Drupal\Core\Routing\AdminContext;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Class MyBreadcrumbBuilder.
*/
class MyBreadcrumbBuilder implements BreadcrumbBuilderInterface {
use StringTranslationTrait;
/**
* Constructs the MyBreadcrumbBuilder.
*
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* @param \Drupal\Core\Menu\MenuActiveTrailInterface $active_trail
* @param \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager
* @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver
* @param \Drupal\Core\Routing\AdminContext $admin_context
*/
public function __construct(RequestStack $request_stack, MenuActiveTrailInterface $active_trail, MenuLinkManagerInterface $menu_link_manager, TitleResolverInterface $title_resolver, AdminContext $admin_context) {
$this->current_request = $request_stack->getCurrentRequest();
$this->active_trail = $active_trail;
$this->menu_link_manager = $menu_link_manager;
$this->title_resolver = $title_resolver;
$this->admin_context = $admin_context;
}
/**
* {@inheritdoc}
*/
public function applies(RouteMatchInterface $route_match) {
return !$this->admin_context->isAdminRoute($route_match->getRouteObject());
}
/**
* {@inheritdoc}
*/
public function build(RouteMatchInterface $route_match) {
$breadcrumb = new Breadcrumb();
$breadcrumb->addCacheContexts(['url.path']);
$links = [];
// Start with the current page, without link.
$current_title = $this->title_resolver->getTitle($this->current_request, $route_match->getRouteObject());
if (!empty($current_title)) {
$links[] = new Link($current_title, Url::fromRoute('<none>'));
}
// If the current item is in a menu, follow the active trail.
$menus = ['main', 'footer'];
do {
$active_link = $this->active_trail->getActiveLink(current($menus));
} while(next($menus) && empty($active_link));
if (!empty($active_link)) {
$trail = $this->active_trail->getActiveTrailIds($active_link->getMenuName());
$trail = array_filter($trail);
array_shift($trail);
foreach ($trail as $menu_id) {
$menu_link = $this->menu_link_manager->createInstance($menu_id);
$title = $menu_link->getTitle();
$url = $menu_link->getUrlObject();
$links[] = new Link($title, $url);
}
}
// End with the home link.
$links[] = new Link($this->t('Home'), Url::fromRoute('<front>'));
return $breadcrumb->setLinks(array_reverse($links));
}
}
services:
mymodule.breadcrumb:
class: Drupal\mymodule\MyBreadcrumbBuilder
arguments: ['@request_stack', '@menu.active_trail', '@plugin.manager.menu.link', '@title_resolver', '@router.admin_context']
tags:
- { name: breadcrumb_builder, priority: 100 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment