We can use hook_system_breadcrumb_alter
in .module
file to add the current title to breadcrumb.
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Link;
/**
* Implements hook_system_breadcrumb_alter().
*/
function mymodule_base_system_breadcrumb_alter(Breadcrumb &$breadcrumb, RouteMatchInterface $route_match, array $context) {
if ($breadcrumb && !\Drupal::service('router.admin_context')
->isAdminRoute()) {
$title = \Drupal::service('title_resolver')
->getTitle(\Drupal::request(), $route_match->getRouteObject());
if (!empty($title)) {
$breadcrumb->addLink(Link::createFromRoute($title, '<none>'));
}
}
}
If we have more complicated logic for our breadcrumb items and if we have to build almost every item in breadcrumb through some logic then we must develop a custom BreadcrumbBuilder. BreadcrumbBuilder also helps us to build breadcrumbs for views, custom controllers, etc.
Custom BreadcrumbBuilder
File - my_module.services.yml
# File - my_module.services.yml
services:
my_module.breadcrumb:
class: Drupal\my_module\BreadcrumbBuilder
arguments:
- '@router.admin_context'
tags:
- { name: breadcrumb_builder, priority: 100 }
File - /src/BreadcrumbBuilder.php
<?php
namespace Drupal\my_module;
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Link;
use Drupal\Core\Routing\AdminContext;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\node\NodeInterface;
/**
* Provides custom BreadcrumbBuilder for askhub theme.
*/
class BreadcrumbBuilder implements BreadcrumbBuilderInterface {
use StringTranslationTrait;
/**
* AdminContext service.
*
* @var \Drupal\Core\Routing\AdminContext
*/
protected $adminContext;
/**
* Creates a new ThemeNegotiator instance.
*
* @param \Drupal\Core\Routing\AdminContext $admin_context
* AdminContext service.
*/
public function __construct(AdminContext $admin_context) {
$this->adminContext = $admin_context;
}
/**
* {@inheritdoc}
*/
public function applies(RouteMatchInterface $route_match) {
$node = $route_match->getParameter('node');
$view = $route_match->getParameter('view_id');
$view_display_id = $route_match->getParameter('display_id');
if (!$this->adminContext->isAdminRoute() &&
$node instanceof NodeInterface &&
$node->bundle() == "askhub") {
return TRUE;
}
elseif (!$this->adminContext->isAdminRoute() &&
$view == 'askhub' && $view_display_id == 'askhub') {
return TRUE;
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function build(RouteMatchInterface $route_match) {
$breadcrumb = new Breadcrumb();
$breadcrumb->addCacheContexts(['route']);
// Breadcrumb links.
$links[] = Link::createFromRoute($this->t('Home'), '<front>');
// Add current node title.
$node = $route_match->getParameter('node');
if ($node instanceof NodeInterface) {
$links[] = Link::createFromRoute($this->t('Ask Hub'), 'view.askhub.askhub');
$links[] = Link::createFromRoute($node->getTitle(),
'<none>');
}
else {
$links[] = Link::createFromRoute($this->t('Ask Hub'), '<none>');
}
$breadcrumb->setLinks($links);
return $breadcrumb;
}
}