3

The breadcrumb code seems to gather everything from the URL, as it should. But in my module, the other entries used in the URL path do not represent somewhere where the user can go. They instead pass information from my form to a controller object.

Is there a way to remove these entries in the breadcrumb from my modules' controller object?

6

To customize your Breadcrumbs you need to take full control of the Breadcrumb for that particular route with your own Breadcrumb Builder service.

To do this, you need to create an entry in your module/profile services file:

e.g., mymodule/mymodule.services.yml

services:
  mymodule.breadcrumb:
    class: Drupal\mymodule\MyModuleBreadcrumbBuilder
    tags:
      - { name: breadcrumb_builder, priority: 100 }

You then need to create your Breadcrumb Builder class

e.g. mymodule/src/MyModuleBreadcrumbBuilder.php

<?php

/**
 * @file
 * Contains Drupal\mymodule\MyModuleBreadcrumbBuilder.
 */

namespace Drupal\mymodule;

use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Class MyModuleBreadcrumbBuilder.
 *
 * @package Drupal\mymodule
 */
class MyModuleBreadcrumbBuilder implements BreadcrumbBuilderInterface {

  /**
   * {@inheritdoc}
   */
  public function applies(RouteMatchInterface $route_match) {
    $route = $route_match->getCurrentRouteMatch();

    // Add conditionals that return TRUE when the current route should have it's
    // breadcrumb handled here.
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function build(RouteMatchInterface $route_match) {
    $breadcrumb = new Breadcrumb();
    $breadcrumb->addCacheContexts(['route']);

    // Add logic here that builds up the breadcrumbs based on desired behaviour.
    $links[] = Link::createFromRoute(t('Home'), '<front>');

    $breadcrumb->setLinks($links);

    return $breadcrumb;
  }

}

The applies method is where you register this Breadcrumb Builder to be used for the particular route, so build a conditional there that matches your route(s) and returns TRUE.

The build method is where you build your Breadcrumb for any routes that applies returns TRUE, so if you are dealing with multiple routes you will need to build the appropriate logic here to build said Breadcrumbs.

I recommend reading through Palantir's blog post for more background and details: https://www.palantir.net/blog/d8ftw-breadcrumbs-work

  • 1
    Whats the priority for please? – therobyouknow Nov 23 '18 at 22:49
  • 1
    Priority is explained here: valuebound.com/resources/blog/how-to-create-breadcrumb-drupal-8 – therobyouknow Nov 24 '18 at 0:49
  • 1
    +1 upvote on both question and answer. This question/answer solved my problem whereby, after using redirect in a form submit, I got superfluous breadcrumbs (e.g. Home / Admin / Same Link / Same Link / Same Link / Same Link). With this solution I was able to correct this. Worth knowing that if you want to rebuild breadcrumb trail that includes admin, the route name is system.admin as defined in web/core/modules/system/system.links.menu.yml and you can use this route name for building your breadcrumb, in the build method as the above answer describes above. – therobyouknow Nov 24 '18 at 0:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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