How To Manage Breadcrumbs In Your Drupal 8 Module

Overview

In this tutorial, we will go through how to set breadcrumbs in your Drupal 8 module. In Drupal 7 we were using drupal_set_breadcrumb(). In Drupal 8 drupal_set_breadcrumb() is removed and breadcrumbs are managed using the Breadcrumb service. There are only a few steps to do this:

  • Declare your service
  • Define a class implementing BreadcrumbBuilderInterface

This tutorial assumes that you have a working Drupal 8 module.

Path Breadcrumbs FAQ

How to add hierarchical taxonomy terms in breadcrumb?

Answer: There are many ways to solve the same problem in Path Breadcrumbs.

  1. Use "parent" tokens (for example %node:field-category:parent:parent:parent:i18n-name) as many times as many deep levels you want.
    Read more here.
  2. Use magic token "pb-join" to automatically add all taxonomy hierarchy to breadcrumbs (for example %node:field-category:pb-join:name and %node:field-category:pb-join:url).
    Read more here.
  3. If your taxonomy listing is a view, try to use Views path view/%taxonomy and “Taxonomy term:ID” for the argument. Under “Selection rules” choose the taxonomy you want.

Why Path Breadcrumbs for path content/testalias doesn't work?

Answer: Path Breadcrumbs works only with system Drupal paths. Not aliases.
Most common paths: node/7 for node 7, taxonomy/term/%term for any taxonomy term.

How can I know if it is system path or alias?

Answer: You can find out system path with Devel module's “Menu item” functionality. Just go on page:

devel/menu/item?path=any-path

where any-path is path or alias for which you want to know system path.

Answer:

  1. Use the same system path node/% (for example node/%product for product content type (PB1) and node/%node for all other nodes (PB2)).

    Note: Context name (%product, %node, %whatever) doesn't matter and can be anything you want.

  2. Add selection rule to Path Breadcrumb for path node/%product (PB1):
    “Node: Bundle” is bundle “Product”.
  3. Reorder Path Breadcrumbs on page admin/structure/path-breadcrumbs to set up PB1 before PB2.

Answer: Use i18n_variable module from i18n package. More info with screenshots.

How can I know which Path Breadcrumbs item is shown on page?

Answer: Disable Path Breadcrumbs cache and use Contextual links to go to current Path Breadcrumbs item.
If you don't see “Edit path breadcrumbs” link in Contextual menu near breadcrumbs area, it means that Path Breadcrumbs doesn't work on this page.
Read more here.

Answer: Use magic token "pb-join" to automatically add all menu item parents in your breadcrumbs.

  • For nodes: %node:menu-link:pb-join:name /
    %node:menu-link:pb-join:url</li>
    <li>For current page menu item: <code>%site:current-page:menu-link:pb-join:name

    / %site:current-page:menu-link:pb-join:url

  • For Book trail (Token module required): %node:book:pb-join:name / %node:book:pb-join:url
  • Path Breadcrumbs

    Path Breadcrumbs module helps you to create breadcrumbs for any page with any selection rules and load any entity from the URL!

    Installation

    Standard installation process.

    Settings page

    Path Breadcrumbs module can be used out of the box without additional configuration.
    However, it has a list of useful settings. Read more in “Settings page” section.

    UI overview

    Create your first Path Breadcrumbs item now!

    Documentation in progress.

    You should also check Frequently Asked Questions for new Path Breadcrumbs (PB) users:

    Use Case: Curated Crumbs breadcrumb based on an Entity Reference parent and a custom breadcrumb title

    In some cases, a content editor might want tight control over the breadcrumb that shows up on each page of the site while still maintaining consistency across the site. There are lots of different breadcrumb modules, but this how-to will go over creating a curated breadcrumb using Crumbs, Entity Reference, and a custom text field.

    Modules

    1. Install Crumbs 7.x-2.0-dev (this branch has caching)
    2. Install Entity Reference

    Content

    1. Add an Entity Reference field to all content types called Breadcrumb Parent (field_breadcrumb_parent)
      This field should be single-value, otherwise it won't work!
    2. Add a text field to all content types called Breadcrumb Title (field_breadcrumb_title)
    3. Add some content and set the Breadcrumb Parent and Breadcrumb Title so that some nodes are in a hierarchy

    Configure Crumbs plugins

    1. Go to the main crumbs admin page: admin/structure/crumbs
    2. Drag two items from the Inherit / automatic section into the Enabled section:
      • text.field_breadcrumb_title.*
      • entityreference.field_breadcrumb_parent.*
    3. If you don't see your plugin, try clearing your cache
    4. Save the configuration

    Crumbs, the breadcrumbs suite

    Crumbs is a powerful breadcrumb-generating engine.

    Building the trail, step by step

    Starting with the path of the current page, Crumbs finds the parent of the path, then the parent path of the parent, etc, until it has the complete breadcrumb trail.

    To do this, Crumbs uses a system of plugins with user-defined priorities. On each step, it will iterate over the plugins until one of them knows the parent path for the current step. And then it moves to the next step.

    On each step, Crumbs does only focus on the current step, it does not care what other steps it has calculated before. One path always has the same parent, throughout the site.

    Finding a title for each path in the trail

    When this procedure is done, Crumbs will do a similar procedure to find the title for each breadcrumb link in the trail. For each link, it will iterate over the plugins, until one of them knows a title. If none of them knows, the title from the router item will be used.

    Important:
    A breadcrumb item with an empty title will be skipped. A plugin can say "I don't know, let the others decide", but it can also explicitly return an empty title, to remove the link from the breadcrumb.
    This behavior can cause some confusion, but is actually quite useful.

    Plugin priorities

    Comparison of Breadcrumb customizing modules

    Modules that influence the generation of a breadcrumb trail

    Comparison of modules that customize the breadcrumb and make nodes and other content appear to be under a chosen menu item:

    Pages

    Subscribe with RSS Subscribe to RSS - breadcrumbs