You are here

public function BreadcrumbBuilder::build in Current Page Crumb 8

Builds the breadcrumb.

Parameters

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

Return value

\Drupal\Core\Breadcrumb\Breadcrumb A breadcrumb.

Overrides PathBasedBreadcrumbBuilder::build

File

src/BreadcrumbBuilder.php, line 26

Class

BreadcrumbBuilder
Adds the current page title to the breadcrumb.

Namespace

Drupal\current_page_crumb

Code

public function build(RouteMatchInterface $route_match) {
  $breadcrumbs = parent::build($route_match);
  $request = \Drupal::request();
  $path = trim($this->context
    ->getPathInfo(), '/');
  $path_elements = explode('/', $path);
  $route = $request->attributes
    ->get(RouteObjectInterface::ROUTE_OBJECT);

  // Do not adjust the breadcrumbs on admin paths and front page.
  if ($route && !$route
    ->getOption('_admin_route') && !$this->pathMatcher
    ->isFrontPage()) {
    $title = $this->titleResolver
      ->getTitle($request, $route);
    if (!isset($title)) {

      // Fallback to using the raw path component as the title if the
      // route is missing a _title or _title_callback attribute.
      $title = str_replace([
        '-',
        '_',
      ], ' ', Unicode::ucfirst(end($path_elements)));
    }
    $breadcrumbs
      ->addLink(Link::createFromRoute($title, '<none>'));
  }

  // Handle expiring views paths and any entity default page cache.
  $parameters = $route_match
    ->getParameters();
  foreach ($parameters as $key => $parameter) {
    if ($key === 'view_id') {
      $breadcrumbs
        ->addCacheTags([
        'config:views.view.' . $parameter,
      ]);
    }
    if ($parameter instanceof CacheableDependencyInterface) {
      $breadcrumbs
        ->addCacheableDependency($parameter);
    }
  }

  // Expire the cache when things need to update based on route, path and language.
  $breadcrumbs
    ->addCacheContexts([
    'route',
    'url.path',
    'languages',
  ]);
  return $breadcrumbs;
}