You are here

protected function MenuBasedBreadcrumbBuilder::addMissingCurrentPage in Menu Breadcrumb 8

Same name and namespace in other branches
  1. 2.0.x src/MenuBasedBreadcrumbBuilder.php \Drupal\menu_breadcrumb\MenuBasedBreadcrumbBuilder::addMissingCurrentPage()

If the current page is missing from the breadcrumb links, add it.

Parameters

\Drupal\Core\Link[] $links: The breadcrumb links.

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

1 call to MenuBasedBreadcrumbBuilder::addMissingCurrentPage()
MenuBasedBreadcrumbBuilder::build in src/MenuBasedBreadcrumbBuilder.php
Builds the breadcrumb.

File

src/MenuBasedBreadcrumbBuilder.php, line 401

Class

MenuBasedBreadcrumbBuilder

Namespace

Drupal\menu_breadcrumb

Code

protected function addMissingCurrentPage(array &$links, RouteMatchInterface $route_match) {

  // Check if the current page is already present.
  if (!empty($links)) {
    $last_url = end($links)
      ->getUrl();
    if ($last_url
      ->isRouted() && $last_url
      ->getRouteName() === $route_match
      ->getRouteName() && $last_url
      ->getRouteParameters() === $route_match
      ->getRawParameters()
      ->all()) {

      // We already have a link, so no need to add one.
      return;
    }
  }

  // If we got here, the current page is missing from the breadcrumb links.
  // This can happen if the active trail is only partial, and doesn't reach
  // the current page, or if a taxonomy attachment is used.
  $route = $route_match
    ->getRouteObject();
  $request = Request::create($route
    ->getPath());

  // Performance optimization: set a short accept header to reduce overhead in
  // AcceptHeaderMatcher when matching the request.
  $request->headers
    ->set('Accept', 'text/html');
  $request->attributes
    ->add($route_match
    ->getParameters()
    ->all());
  $title = $this->titleResolver
    ->getTitle($request, $route);
  if (isset($title)) {
    $links[] = Link::fromTextAndUrl($title, Url::fromRouteMatch($route_match));
  }
}