You are here

public function PanelsBreadcrumbBuilder::build in Panels Breadcrumbs 8

Builds the breadcrumb.

Parameters

\Drupal\Core\Routing\RouteMatchInterface $route_match: The current route match.

Return value

\Drupal\Core\Breadcrumb\Breadcrumb A breadcrumb.

Overrides BreadcrumbBuilderInterface::build

File

src/Breadcrumb/PanelsBreadcrumbBuilder.php, line 83

Class

PanelsBreadcrumbBuilder
Class PanelsBreadcrumbBuilder.

Namespace

Drupal\panels_breadcrumbs\Breadcrumb

Code

public function build(RouteMatchInterface $route_match) {
  $breadcrumb = new Breadcrumb();
  $page_variant = $route_match
    ->getParameter('page_manager_page_variant');
  $variant_settings = $page_variant
    ->get('variant_settings');
  $titles = array_filter(array_map('trim', explode("\r\n", $variant_settings['panels_breadcrumbs']['titles'])), 'strlen');
  $paths = array_filter(array_map('trim', explode("\r\n", $variant_settings['panels_breadcrumbs']['paths'])), 'strlen');
  $entities = [];
  foreach ($page_variant
    ->getContexts() as $id => $context) {
    $entities[$id] = $context
      ->getContextValue();
  }
  $links = [];
  if ($variant_settings['panels_breadcrumbs']['home'] == 1) {
    $home_title = $this
      ->getTitle($variant_settings['panels_breadcrumbs']['home_text'], $entities);
    $links[] = Link::createFromRoute($home_title, '<front>');
  }
  foreach ($titles as $key => $title) {
    $title = $this
      ->getTitle($title, $entities);
    $path = $this->token
      ->replace($paths[$key], $entities);
    if ($this->routeProvider
      ->getRoutesByNames([
      $path,
    ])) {
      $path = Url::fromRoute($path)
        ->toString();
    }
    if ($path && ($request = $this
      ->getRequestForPath($path))) {
      $route_match = RouteMatch::createFromRequest($request);
      $access = $this->accessManager
        ->check($route_match, $this->currentUser, NULL, TRUE);
      $breadcrumb = $breadcrumb
        ->addCacheableDependency($access);
      if ($access
        ->isAllowed()) {
        $links[] = Link::fromTextAndUrl($title, Url::fromRouteMatch($route_match));
      }
    }
    else {
      $links[] = Link::createFromRoute($title, '<nolink>');
    }
  }
  $this
    ->addCaching($route_match, $breadcrumb);
  $breadcrumb
    ->setLinks($links);
  return $breadcrumb;
}