Drupal 8 print current breadcrumb trail with route title

Whao, that wasn't as easy as I thought. I wanted to print out the two top level breadcrumb elements, without "Home", with node title in a custom block. I tried it in many different ways but ran into surprises every time. So finally I'd like to share my code with you, if you should run into the same situation:

This is the part where the breadcrumbs and page title are retrieved:

  1. $breadcrumbs = $this->breadcrumbManager->build($this->routeMatch)->getLinks();
  2.     $items = [];
  3.     if(!empty($breadcrumbs[1])){
  4.       $items[] = $breadcrumbs[1]->getText();
  5.     }
  6.     if(!empty($breadcrumbs[2])){
  7.       $items[] = $breadcrumbs[2]->getText();
  8.     }
  9.     // Fallback: Page title
  10.     $request = \Drupal::request();
  11.     $page_title =  $this->titleResolver->getTitle($request, $this->routeMatch->getRouteObject());
  12.     if(!empty($page_title)){
  13.       $items[] = $page_title;
  14.     }

Kommentare

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.

Weitere Informationen über Formatierungsoptionen