1

I want to add another element to breadcrumb.
I have:
Home > Node Title
I want to add another link between the two elements like:
Home > Link to view > Node Title

I have tried with hook_system_breadcrumb_alter like the following:

/**
 * Implements hook_system_breadcrumb_alter().
 */
function MyModules_system_breadcrumb_alter(Breadcrumb &$breadcrumb, RouteMatchInterface $route_match, array $context) {
  if ( $route_match->getRouteName() === 'entity.node.canonical'){
   $node = $route_match->getParameter('node');
   if ($node instanceof  Drupal\node\Entity\Node && $node->getType() === 'my_node_type'){
     // Get the listing view url.
     $listing_url = Url::fromRoute('view.my_view_id.my_display_id');
     // Add the listing url to breadcrumbs.
     $breadcrumb->addLink(Link::fromTextAndUrl(t('Link to view'), $listing_url));
   }
  }
}

But i get Home > Node Title > Link to view instead of Home > Link to view > Node Title
My question is how can i change the order of the last two breadcrumbs elements?

1 Answer 1

2

Create a new breadcrumb instance:

$links = $breadcrumb->getLinks();
// change order of $links
$cacheability = \Drupal\Core\Cache\CacheableMetadata::createFromObject($breadcrumb);
$breadcrumb = new \Drupal\Core\Breadcrumb\Breadcrumb();
$breadcrumb->setLinks($links);
$breadcrumb->addCacheableDependency($cacheability);
1
  • 1
    Thanks for your answer.
    – berramou
    Commented Jun 26, 2019 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.