class ContextBreadcrumbBuilder in Context Active Trail 8
Same name and namespace in other branches
- 8.2 src/ContextBreadcrumbBuilder.php \Drupal\context_active_trail\ContextBreadcrumbBuilder
Build breadcrumbs based on active trail from context.
Hierarchy
- class \Drupal\context_active_trail\ContextBreadcrumbBuilder implements BreadcrumbBuilderInterface
Expanded class hierarchy of ContextBreadcrumbBuilder
1 string reference to 'ContextBreadcrumbBuilder'
1 service uses ContextBreadcrumbBuilder
File
- src/
ContextBreadcrumbBuilder.php, line 18
Namespace
Drupal\context_active_trailView source
class ContextBreadcrumbBuilder implements BreadcrumbBuilderInterface {
/**
* The context manager.
*
* @var \Drupal\context\ContextManager
*/
protected $contextManager;
/**
* The active trail.
*
* @var \Drupal\Core\Menu\MenuActiveTrailInterface
*/
protected $activeTrail;
/**
* The menu link manager.
*
* @var \Drupal\Core\Menu\MenuLinkManagerInterface
*/
protected $linkManager;
/**
* The title resolver.
*
* @var \Drupal\Core\Controller\TitleResolverInterface
*/
protected $titleResolver;
/**
* The current request.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* Configuration of the context reaction affecting breadcrumbs.
*
* @var array
*/
protected $configuration;
/**
* Constructor.
*
* @param \Drupal\context\ContextManager $context_manager
* The context manager.
* @param \Drupal\Core\Menu\MenuActiveTrailInterface $active_trail
* The active trail.
* @param \Drupal\Core\Menu\MenuLinkManagerInterface $link_manager
* The menu link manager.
* @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver
* The title resolver.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
*/
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();
}
/**
* {@inheritdoc}
*/
public function applies(RouteMatchInterface $route_match) {
foreach ($this->contextManager
->getActiveReactions('active_trail') as $reaction) {
if ($reaction
->setsBreadcrumbs()) {
return $this->configuration = $reaction
->getConfiguration();
}
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function build(RouteMatchInterface $route_match) {
$breadcrumb = new Breadcrumb();
$breadcrumb
->addCacheContexts([
'url.path',
]);
// Start with home page.
$breadcrumb
->addLink(Link::createFromRoute(t('Home'), '<front>'));
// Add links from menu.
$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()));
}
// Include current page title.
if ($this->configuration['breadcrumb_title']) {
$title = $this->titleResolver
->getTitle($this->request, $route_match
->getRouteObject());
$breadcrumb
->addLink(Link::createFromRoute($title, '<none>'));
}
return $breadcrumb;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContextBreadcrumbBuilder:: |
protected | property | The active trail. | |
ContextBreadcrumbBuilder:: |
protected | property | Configuration of the context reaction affecting breadcrumbs. | |
ContextBreadcrumbBuilder:: |
protected | property | The context manager. | |
ContextBreadcrumbBuilder:: |
protected | property | The menu link manager. | |
ContextBreadcrumbBuilder:: |
protected | property | The current request. | |
ContextBreadcrumbBuilder:: |
protected | property | The title resolver. | |
ContextBreadcrumbBuilder:: |
public | function |
Whether this breadcrumb builder should be used to build the breadcrumb. Overrides BreadcrumbBuilderInterface:: |
|
ContextBreadcrumbBuilder:: |
public | function |
Builds the breadcrumb. Overrides BreadcrumbBuilderInterface:: |
|
ContextBreadcrumbBuilder:: |
public | function | Constructor. |