Solución:
Los usuarios que no quieran utilizar un módulo personalizado para implementar un BreadcrumbBuilder pueden cambiar las migas de pan en un enlace de preproceso.
Ejemplo de cómo puede agregar un enlace antes del último enlace:
function mytheme_preprocess_breadcrumb(&$variables) {
$breadcrumb = &$variables['breadcrumb'];
$lastlink = array_pop($breadcrumb);
array_push($breadcrumb, [
'text' => 'New Link',
'url' => '/newlink',
]);
array_push($breadcrumb, $lastlink);
}
Si agrega un término de taxonomoy de un nodo, tendrá que agregar tanto como etiquetas de caché como un contexto de caché en la URL para que esto funcione.
Podemos implementar migas de pan personalizadas a través de module
o vía HOOK_preprocess_breadcrumb
.
Vía HOOK_preprocess_breadcrumb
:
Agregue esto en su YOURTHEME.theme
/**
* Implements HOOK_preprocess_breadcrumb().
*/
function YOURTHEME_preprocess_breadcrumb(&$variables) {
if (($node = Drupal::routeMatch()->getParameter('node')) && $variables['breadcrumb']) {
$breadcrumb = &$variables['breadcrumb'];
if (!empty($node->field_tags->entity)) {
$term_url = $node->field_tags->entity->toLink();
$node_url = array_pop($breadcrumb);
array_push($breadcrumb, $term_url);
array_push($breadcrumb, $node_url);
// Implementing Cache.
$variables['#cache']['contexts'][] = "url.path";
$variables['#cache']['tags'][] = "node:{$node->nid->value}";
}
}
}
Vía module
:
En custom_breadcrumb.module
expediente
<?php
namespace Drupalcustom_breadcrumb;
use DrupalCoreBreadcrumbBreadcrumb;
use DrupalCoreBreadcrumbBreadcrumbBuilderInterface;
use DrupalCoreLink;
use DrupalCoreRoutingRouteMatchInterface;
use DrupalCoreStringTranslationStringTranslationTrait;
use DrupalnodeNodeInterface;
/**
* Class Breadcrumbs.
*
* @package Drupalcustom_breadcrumb
*/
class Breadcrumbs implements BreadcrumbBuilderInterface {
use StringTranslationTrait;
/**
* {@inheritDoc}
*/
public function applies(RouteMatchInterface $route_match) {
return $route_match->getRouteName() == 'entity.node.canonical' && $route_match->getParameter('node') instanceof NodeInterface;
}
/**
* {@inheritDoc}
*/
public function build(RouteMatchInterface $route_match) {
$node = $route_match->getParameter('node');
$breadcrumb = new Breadcrumb();
// By setting a "cache context" to the "url", each requested URL gets it's
// own cache. This way a single breadcrumb isn't cached for all pages on the
// site.
$breadcrumb->addCacheContexts(["url"]);
// By adding "cache tags" for this specific node, the cache is invalidated
// when the node is edited.
$breadcrumb->addCacheTags(["node:{$node->nid->value}"]);
// Add "Home" breadcrumb link.
$breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '<front>'));
// Given we have a taxonomy term reference field named "field_tags" and that
// field has data, add that term as a breadcrumb link.
if (!empty($node->field_tags->entity)) {
$breadcrumb->addLink($node->field_tags->entity->toLink());
}
return $breadcrumb;
}
}
y luego en custom_breadcrumb.services.yml
services:
custom_breadcrumb.breadcrumbs:
class: Drupalcustom_breadcrumbBreadcrumbs
tags:
- { name: breadcrumb_builder, priority: 100 }
Ambas formas son alcanzables, si su pregunta es cuál debe seguirse y cuál debe denominarse mejores prácticas, consulte https://drupal.stackexchange.com/a/238962/3808