1

I have to create a breadcrumb so that, if a page has a menu, the breadcrumb should be the menu with its hierarchy. If the node page doesn't have a menu, the page title should be in the breadcrumb.

I tries the Menu breadcrumb module. Menu pages are coming correctly, but other pages are showing the page title in the breadcrumb.

How can I achieve this?

1 Answer 1

2

You can do this by creating custom module and creating yourmodule.services.yml

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

and creating custom class which should extend BreadcrumbBuilderInterface like this

class FwsBreadcrumbBuilder implements BreadcrumbBuilderInterface {
  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;
  }

  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 One'), '<front>');

    $breadcrumb->setLinks($links);

    return $breadcrumb;
  }
}

I have also got the above code from http://drupal.stackexchange.com

You can also add new and alter previous breadcrumbs in hook_preprocess_HOOK function like this

function yourmodule_or_yourtheme_preprocess_breadcrumb(&$variables) {
    // Your Logic Here
}

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.