Drupal 8 & 9: How create a simple custom module for breadcrumbs: Example #1
When in front of me there was a task to make a simple site with breadcrumbs, which can show me the necessary view I started looking my familiar and beloved path_breadcrumbs. However, the module has been for drupal 7 only. So I had to quickly write my own module. Below is an example of how you can do it yourself. I tried to leave the necessary comments in the code. I hope this will help anyone :-)
name: EasyDrupal Breadcrumbs type: module description: Setting breadcrumbs for custom content types. core: 8.x package: Custom
<?php /** * @file * Contains easydrupal_breadcrumb.module. */ use Drupal\Core\Routing\RouteMatchInterface; /** * Implements hook_help(). */ function easydrupal_breadcrumb_help($route_name, RouteMatchInterface $route_match) { switch ($route_name) { // Main module help for the easydrupal_breadcrumb module. case 'help.page.easydrupal_breadcrumb': $output = ''; $output .= '<h3>' . t('About') . '</h3>'; $output .= '<p>' . t('Setting breadcrumbs for custom content types') . '</p>'; return $output; default: } }
services: easydrupal_breadcrumb.breadcrumb: class: Drupal\easydrupal_breadcrumb\Breadcrumb\EasydrupalBreadcrumbBuilder tags: - { name: breadcrumb_builder, priority: 100 }
<?php namespace Drupal\easydrupal_breadcrumb\Breadcrumb; use Drupal\Core\Breadcrumb\Breadcrumb; use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Link; class EasydrupalBreadCrumbBuilder implements BreadcrumbBuilderInterface { /** * {@inheritdoc} */ public function applies(RouteMatchInterface $attributes) { $parameters = $attributes->getParameters()->all(); // I need my breadcrumbs for a few node types ONLY, // so it should be applied on node page ONLY. if (isset($parameters['node']) && !empty($parameters['node'])) { return TRUE; } } /** * {@inheritdoc} */ public function build(RouteMatchInterface $route_match) { $breadcrumb = new Breadcrumb(); $breadcrumb->addLink(Link::createFromRoute('Home', '<front>')); $node = \Drupal::routeMatch()->getParameter('node'); $node_type = $node->bundle(); switch ($node_type) { // If node type is "project" // I want to add as parent of breadcrumb my summary projects view. case 'project': $breadcrumb->addLink(Link::createFromRoute('Projects', 'view.portfolio.page_1')); break; // If node type is "article". // I want to add as parent of breadcrumb my summary articles view. case 'article': $breadcrumb->addLink(Link::createFromRoute('Articles', 'view.articles.page_1')); break; } // Don't forget to add cache control by a route, // otherwise you will surprice, // all breadcrumb will be the same for all pages. $breadcrumb->addCacheContexts(['route']); return $breadcrumb; } }
In this example, I showed how to quickly make a simple module for yourself. This example can be easily changed, for example, show "Active menu trail". Or, for example, another reason if you don't want any large >100kb module for your site and you know how to do that yourself.
I hope the information in this article helpful. And of course, I would happy if you can provide the own solution in a comment below.
I think will be interesting also "How to create a simple custom module for breadcrumbs: Example #2" which has more real examples.