8.5.x menu.api.php hook_system_breadcrumb_alter(\Drupal\Core\Breadcrumb\Breadcrumb &$breadcrumb, \Drupal\Core\Routing\RouteMatchInterface $route_match, array $context)
8.0.x menu.api.php hook_system_breadcrumb_alter(\Drupal\Core\Breadcrumb\Breadcrumb &$breadcrumb, \Drupal\Core\Routing\RouteMatchInterface $route_match, array $context)
8.1.x menu.api.php hook_system_breadcrumb_alter(\Drupal\Core\Breadcrumb\Breadcrumb &$breadcrumb, \Drupal\Core\Routing\RouteMatchInterface $route_match, array $context)
8.2.x menu.api.php hook_system_breadcrumb_alter(\Drupal\Core\Breadcrumb\Breadcrumb &$breadcrumb, \Drupal\Core\Routing\RouteMatchInterface $route_match, array $context)
8.3.x menu.api.php hook_system_breadcrumb_alter(\Drupal\Core\Breadcrumb\Breadcrumb &$breadcrumb, \Drupal\Core\Routing\RouteMatchInterface $route_match, array $context)
8.4.x menu.api.php hook_system_breadcrumb_alter(\Drupal\Core\Breadcrumb\Breadcrumb &$breadcrumb, \Drupal\Core\Routing\RouteMatchInterface $route_match, array $context)
8.6.x menu.api.php hook_system_breadcrumb_alter(\Drupal\Core\Breadcrumb\Breadcrumb &$breadcrumb, \Drupal\Core\Routing\RouteMatchInterface $route_match, array $context)

Perform alterations to the breadcrumb built by the BreadcrumbManager.

Parameters

\Drupal\Core\Breadcrumb\Breadcrumb $breadcrumb: A breadcrumb object returned by BreadcrumbBuilderInterface::build().

\Drupal\Core\Routing\RouteMatchInterface $route_match: The current route match.

array $context: May include the following key:

Related topics

1 function implements hook_system_breadcrumb_alter()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

menu_ui_system_breadcrumb_alter in core/modules/menu_ui/menu_ui.module
Implements hook_system_breadcrumb_alter().

File

core/lib/Drupal/Core/Menu/menu.api.php, line 431
Hooks and documentation related to the menu system and links.

Code

function hook_system_breadcrumb_alter(\Drupal\Core\Breadcrumb\Breadcrumb &$breadcrumb, \Drupal\Core\Routing\RouteMatchInterface $route_match, array $context) {

  // Add an item to the end of the breadcrumb.
  $breadcrumb
    ->addLink(\Drupal\Core\Link::createFromRoute(t('Text'), 'example_route_name'));
}

Comments

ventzie’s picture

In class \Drupal\Core\Breadcrumb\Breadcrumb Breadcrumb::$links is protected and Breadcrumb::getLinks doesn't return reference to the Breadcrumb::$links. So the only thing that can be done is not alter the links but add a new one - Breadcrumb::addLink. Breadcrumb::setLinks cannot be used also because Breadcrumb::$links most of the time is not empty :
public function setLinks(array $links) {
if (!empty($this->links)) {
throw new \LogicException('Once breadcrumb links are set, only additional breadcrumb links can be added.');
}

$this->links = $links;

return $this;
}
Besides the ampersand in the first paramneter is useless because objects are passed by reference anyway.

k-l’s picture

It is possible to change existing breadcrumbs by overwriting $breadcrumb variable like this:
$links = $breadcrumb->getLinks();
// Remove link from breadcrumb link.
$links[1]->setUrl(\Drupal\Core\Url::fromRoute("<none>"));
$breadcrumb = new Breadcrumb();
$breadcrumb->setLinks($links);

anou’s picture

In yourmodule.module file

<?php
/**
 * Implements hook_system_breadcrumb_alter().
 */
function yourmodule_system_breadcrumb_alter(\Drupal\Core\Breadcrumb\Breadcrumb &$breadcrumb, \Drupal\Core\Routing\RouteMatchInterface $route_match, array $context) {
  // Append the current page title to the breadcrumb for non-admin routes.
  if ($breadcrumb && !\Drupal::service('router.admin_context')->isAdminRoute()) {

    $title = \Drupal::service('title_resolver')->getTitle(\Drupal::request(), $route_match->getRouteObject());
    if (!empty($title)) {
      $breadcrumb->addLink(\Drupal\Core\Link::createFromRoute($title, '<none>'));
    }
  }
}
taggartj’s picture

thanks

vivekweb03’s picture

It is possible to change titles of breadcrumb links.

I tried it using hook_system_breadcrumb_alter() like:

$breadcrumb_links = $breadcrumb->getLinks();
$breadcrumb_links[0] = \Drupal\Core\Link::createFromRoute(t('schnellinfo'), '');

With this code, able to get all links and set set title of first link. But don't know how to assign it.

taggartj’s picture

/**
 * Implements hook_system_breadcrumb_alter().
 */
function MYMODULE_system_breadcrumb_alter(Breadcrumb &$breadcrumb, RouteMatchInterface $route_match, array $context) {
  if (!empty($breadcrumb)) {
    $links = $breadcrumb->getLinks();
    if (!empty($links[1])) {
      if ($links[1]->getText() == 'Node') {
        $links[1]->setUrl(Url::fromUri('internal:/admin/workbench'));
        $links[1]->setText('My Workbench');
        $breadcrumb = new Breadcrumb();
        $breadcrumb->setLinks($links);
      }
    }
  }
}

taggartj’s picture

// Some how Bredcrums more than 4 deep in menu structure dont show SO FIX THAT.
function module_system_breadcrumb_alter(Breadcrumb &$breadcrumb, RouteMatchInterface $route_match, array $context) {
  if (!empty($breadcrumb)) {
    $links = $breadcrumb->getLinks();
    if (!empty($links[1])) {
      // Set seccond link to inactive.
      $links[1]->setUrl(Url::fromRoute(''));
      if (count($links) >= 4) {
        try {
          $node = $route_match->getParameter('node');
          if (is_object($node)) {
            $node_id = $node->id();
            $alias = \Drupal::service('path.alias_manager')->getAliasByPath('/node/' . $node_id);
            $alias = 'internal:' . $alias;
            $query = \Drupal::service('entity.query')->get('menu_link_content');
            $query->condition('link.uri', $alias);
            $entity_ids = $query->execute();
            if (is_array($entity_ids) && count($entity_ids) == 1) {
              $entity_id = reset($entity_ids);
              $menu_link_storage = \Drupal::entityTypeManager()->getStorage('menu_link_content');
              $loaded = $menu_link_storage->load($entity_id);
              $parent_id = $loaded->getParentId();
              $uuid_explode = explode('menu_link_content:', $parent_id);
              if (!empty($uuid_explode[1])) {
                $uuid = $uuid_explode[1];
                $parrent = $menu_link_storage->loadByProperties(['uuid' => $uuid]);
                if (is_array($parrent)) {
                  $parrent = reset($parrent);
                  $title = $parrent->getTitle();
                  // Add A new link.
                  $url = $parrent->getUrlObject();
                  $new_link = Link::fromTextAndUrl(t($title), $url);
                  $links[] = $new_link;
                }
              }
            }
          }
        }
        catch (\Exception $e) {
          // Silent error.
        }
      }
      $breadcrumb = new Breadcrumb();
      $breadcrumb->setLinks($links);
    }
  }
}

or you can use bredcrum builder to take full control see at:
http://taggartjensen.com/code-snip/drupal-8-make-breadcrums-awesome