I'm not aware of any contrib module that is capable of adding custom fields as suffix to breadcrumb links.
Fortunately, Drupal 8's modular architecture enables you to add your own breadcrumb builder service with a little bit of custom programming.
You will have to create an own module (within the following example, I'll call it my_module
or MyModule
respectively; you'll have to replace it accordingly) and add a my_module.services.yml file to its root directory.
Within this file you can define your own breadcrumb builder service. Make sure to let Drupal know, that it is a breadcrumb builder and in which order it should be processed by adding the relevant tags (read more about service tags here):
services:
my_module.breadcrumb:
class: Drupal\my_module\Service\MyModuleBreadcrumbBuilder
arguments: ['@entity.manager']
tags:
- { name: breadcrumb_builder, priority: 10001 }
Now that Drupal knows about your breadcrumb builder and will process it fairly early because of the high priority given, you will have to create the according class.
Create a service class MyModuleBreadcrumbBuilder.php
in your my_module/src/Service/
folder. Apart from the convention to letting its name end on BreadcrumbBuilder
, you can actually name it to your liking. Just make sure to change the class name within the .services.yml
file and the follwing example accordingly.
As you are up to creating a taxonomy breadcrumb builder, a good starting point is the TermBreadcrumbBuilder of the taxonomy core module, which we will extend in our example:
namespace Drupal\my_module\Service;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\taxonomy\TermBreadcrumbBuilder;
/**
* My custom breadcrumb builder for Animalia.
*/
class MyModuleBreadcrumbBuilder extends TermBreadcrumbBuilder implements BreadcrumbBuilderInterface {
}
As your requirements seem to apply to a certain taxonomy vocabulary only, you should implement the BreadcrumbBuilderInterface::applies() method to check whether you are on a term page of that vocabulary.
This assures your breadcrumb builder won't run on other entities/vocabularies that don't feature your custom field (I used the ID my_vocabulary
for your vocabulary; you will have to replace it accordingly):
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\taxonomy\TermInterface;
[...]
/**
* {@inheritdoc}
*/
public function applies(RouteMatchInterface $route_match) {
return $route_match->getRouteName() == 'entity.taxonomy_term.canonical'
&& ($term = $route_match->getParameter('taxonomy_term')) instanceof TermInterface
&& $term->getVocabularyId() == 'my_vocabulary';
}
The next step is to implementing the BreadcrumbBuilderInterface::build() method, that returns the actual breadcrumb. Here, we'll add your custom field value to the shown parent term link labels.
As we extended the taxonomy breadcrumb builder before, we already have the entity manager and term storage available, which enable us to loading the localized versions of the terms.
Most of the proceedings match the TermBreadcrumbBuilder::build() method, so that I added a comment only where the term field value is added:
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Link;
[...]
/**
* {@inheritdoc}
*/
public function build(RouteMatchInterface $route_match) {
$breadcrumb = new Breadcrumb();
$breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '<front>'));
$term = $route_match->getParameter('taxonomy_term');
$breadcrumb->addCacheableDependency($term);
$parents = $this->termStorage->loadAllParents($term->id());
array_shift($parents);
foreach (array_reverse($parents) as $term) {
$term = $this->entityManager->getTranslationFromContext($term);
// The 'magic' happens here by loading the field value and
// adding it to the link title; alter 'field_common_name' to
// match your custom field ID.
$my_field = $term->get('field_common_name');
$term_name = $term->getName() . (!empty($my_field->getValue()[0]['value']) ? ' (' . $my_field->getValue()[0]['value'] . ')' : '');
$breadcrumb->addCacheableDependency($term);
$breadcrumb->addLink(Link::createFromRoute($term_name, 'entity.taxonomy_term.canonical', ['taxonomy_term' => $term->id()]));
}
$breadcrumb->addCacheContexts(['route']);
return $breadcrumb;
}
After enabling the module (or flushing the caches, if you already enabled it before), you should now see your custom breadcrumbs when you open a term of your vocabulary.