Drupal - How to alter breadcrumbs from my controller in my module?
To customize your Breadcrumbs you need to take full control of the Breadcrumb for that particular route with your own Breadcrumb Builder service.
To do this, you need to create an entry in your module/profile services file:
e.g., mymodule/mymodule.services.yml
services:
mymodule.breadcrumb:
class: Drupal\mymodule\MyModuleBreadcrumbBuilder
tags:
- { name: breadcrumb_builder, priority: 100 }
You then need to create your Breadcrumb Builder class
e.g. mymodule/src/MyModuleBreadcrumbBuilder.php
<?php
/**
* @file
* Contains Drupal\mymodule\MyModuleBreadcrumbBuilder.
*/
namespace Drupal\mymodule;
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Class MyModuleBreadcrumbBuilder.
*
* @package Drupal\mymodule
*/
class MyModuleBreadcrumbBuilder implements BreadcrumbBuilderInterface {
/**
* {@inheritdoc}
*/
public function applies(RouteMatchInterface $route_match) {
$route = $route_match->getCurrentRouteMatch();
// Add conditionals that return TRUE when the current route should have it's
// breadcrumb handled here.
return FALSE;
}
/**
* {@inheritdoc}
*/
public function build(RouteMatchInterface $route_match) {
$breadcrumb = new Breadcrumb();
$breadcrumb->addCacheContexts(['route']);
// Add logic here that builds up the breadcrumbs based on desired behaviour.
$links[] = Link::createFromRoute(t('Home'), '<front>');
$breadcrumb->setLinks($links);
return $breadcrumb;
}
}
The applies
method is where you register this Breadcrumb Builder to be used for the particular route, so build a conditional there that matches your route(s) and returns TRUE
.
The build
method is where you build your Breadcrumb for any routes that applies
returns TRUE
, so if you are dealing with multiple routes you will need to build the appropriate logic here to build said Breadcrumbs.
I recommend reading through Palantir's blog post for more background and details: https://www.palantir.net/blog/d8ftw-breadcrumbs-work