You are here

class ContextBreadcrumbBuilder in Context Active Trail 8

Same name and namespace in other branches
  1. 8.2 src/ContextBreadcrumbBuilder.php \Drupal\context_active_trail\ContextBreadcrumbBuilder

Build breadcrumbs based on active trail from context.

Hierarchy

Expanded class hierarchy of ContextBreadcrumbBuilder

1 string reference to 'ContextBreadcrumbBuilder'
context_active_trail.services.yml in ./context_active_trail.services.yml
context_active_trail.services.yml
1 service uses ContextBreadcrumbBuilder
context_active_trail.breadcrumb.default in ./context_active_trail.services.yml
Drupal\context_active_trail\ContextBreadcrumbBuilder

File

src/ContextBreadcrumbBuilder.php, line 18

Namespace

Drupal\context_active_trail
View 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

Namesort descending Modifiers Type Description Overrides
ContextBreadcrumbBuilder::$activeTrail protected property The active trail.
ContextBreadcrumbBuilder::$configuration protected property Configuration of the context reaction affecting breadcrumbs.
ContextBreadcrumbBuilder::$contextManager protected property The context manager.
ContextBreadcrumbBuilder::$linkManager protected property The menu link manager.
ContextBreadcrumbBuilder::$request protected property The current request.
ContextBreadcrumbBuilder::$titleResolver protected property The title resolver.
ContextBreadcrumbBuilder::applies public function Whether this breadcrumb builder should be used to build the breadcrumb. Overrides BreadcrumbBuilderInterface::applies
ContextBreadcrumbBuilder::build public function Builds the breadcrumb. Overrides BreadcrumbBuilderInterface::build
ContextBreadcrumbBuilder::__construct public function Constructor.