ContextBreadcrumbBuilder.php in Context Active Trail 8
File
src/ContextBreadcrumbBuilder.php
View source
<?php
namespace Drupal\context_active_trail;
use Drupal\context\ContextManager;
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\RouteMatchInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class ContextBreadcrumbBuilder implements BreadcrumbBuilderInterface {
protected $contextManager;
protected $activeTrail;
protected $linkManager;
protected $titleResolver;
protected $request;
protected $configuration;
public function __construct(ContextManager $context_manager, MenuActiveTrailInterface $active_trail, MenuLinkManagerInterface $link_manager, TitleResolverInterface $title_resolver, RequestStack $request_stack) {
$this->contextManager = $context_manager;
$this->activeTrail = $active_trail;
$this->linkManager = $link_manager;
$this->titleResolver = $title_resolver;
$this->request = $request_stack
->getCurrentRequest();
}
public function applies(RouteMatchInterface $route_match) {
foreach ($this->contextManager
->getActiveReactions('active_trail') as $reaction) {
if ($reaction
->setsBreadcrumbs()) {
return $this->configuration = $reaction
->getConfiguration();
}
}
return FALSE;
}
public function build(RouteMatchInterface $route_match) {
$breadcrumb = new Breadcrumb();
$breadcrumb
->addCacheContexts([
'url.path',
]);
$breadcrumb
->addLink(Link::createFromRoute(t('Home'), '<front>'));
$link_ids = array_filter($this->activeTrail
->getActiveTrailIds(NULL));
foreach (array_reverse($link_ids) as $link_id) {
$link = $this->linkManager
->getInstance([
'id' => $link_id,
]);
$breadcrumb
->addLink(Link::fromTextAndUrl($link
->getTitle(), $link
->getUrlObject()));
}
if ($this->configuration['breadcrumb_title']) {
$title = $this->titleResolver
->getTitle($this->request, $route_match
->getRouteObject());
$breadcrumb
->addLink(Link::createFromRoute($title, '<none>'));
}
return $breadcrumb;
}
}