I'm currently using the preprocess_breadcrumb hook in my .theme file to customize the output of my breadcrumbs like so:
function mytheme_preprocess_breadcrumb(&$variables){
$node = \Drupal::routeMatch()->getParameter('node');
$current_path = \Drupal::service('path.current')->getPath();
$result = \Drupal::service('path.alias_manager')->getAliasByPath($current_path);
$crumbs = explode("/", $result);
if($node){
$variables['current_title'] = $node->getTitle();
$variables['crumbs'] = $crumbs;
}
}
I then use breadcrumb.html.twig to use these new variables like so
<nav role="navigation" aria-labelledby="system-breadcrumb">
<h2 id="system-breadcrumb" class="visually-hidden">{{ 'You are here'|t }}</h2>
<ul class="breadcrumbs">
{% for crumb in crumbs %}
{% if loop.first %}
<li><a href="./">Home</a></li>
{% elseif loop.last %}
<li class="current"><a href="#">{{ current_title }}</a></li>
{% elseif crumb == 'node' %}
{% else %}
<li> <a href="../{{ crumb }}">{{ crumb }}</a></li>
{% endif %}
{% endfor %}
</ul>
</nav>
{% endif %}
However I'd like it more if all this was encapsulated in a module. In mymodule.module file I have
function mymodule_preprocess_breadcrumb(&$variables){
$node = \Drupal::routeMatch()->getParameter('node');
$current_path = \Drupal::service('path.current')->getPath();
$result = \Drupal::service('path.alias_manager')->getAliasByPath($current_path);
$crumbs = explode("/", $result);
if($node){
$variables['current_title'] = $node->getTitle();
$variables['crumbs'] = $crumbs;
}
}
But this function never fires. Is there a way to use the preprocess_breadcrumb hook from a module?
BookBreadcrumbBuilder
.