How to alter an existing breadcrumb link programmatically? I am trying to alter the 'Home' Breadcrumb link using
hook_system_breadcrumb_alter()
The default Breadcrumb class allows you to add New links using the
addLinks
method.
However it does not allow one to edit the links. Is there a way to edit an existing link directly?
Turns outs you can't edit the existing breadcrumbs. However you can fetch them using the getter function, modify them and then call the Breadcrumb builder class.
&lt;?php
/**
 * Implements hook_system_breadcrumb_alter().
 */
function MODULE_system_breadcrumb_alter(Breadcrumb &amp;$breadcrumb, RouteMatchInterface $route_match, array $context) {
  // Add new link
  $breadcrumb-&gt;addLink($link);
  // Saving the original links and caching
  $original_links = $breadcrumb-&gt;getLinks();
  $cacheability = CacheableMetadata::createFromObject($breadcrumb);
  // Remove the old value of the array.
  array_shift($original_links);
  $home_link = array_pop($original_links);
  array_unshift($original_links, $home_link);
  // Finally generating the new link.
  $breadcrumb = new Breadcrumb();
  $breadcrumb-&gt;setLinks($original_links);
  $breadcrumb-&gt;addCacheableDependency($cacheability);
}