How to add each node's breadcrumb in a view?

Created on 6 September 2023, 10 months ago

I have a view which displays the title and teaser of each node. I also need each node's breadcrumb to display in the view results. I have created a hook_preprocess_node function in my theme with the intent of using the nodes' urls to get the breadcrumb. I have tried to get the $route_match to pass to the builder, but have not been successful. Is this the right approach? If so, how do I get the correct $route_match Interface to pass? The following generates an array but not the Interface that is required.

$path = $variables['url'];
$router = \Drupal::service('router.no_access_checks');
$routerMatch = $router->match($path);
$breadcrumbBuilder = \Drupal::service('easy_breadcrumb.breadcrumb');
$breadcrumb = $breadcrumbBuilder->build($routerMatch);
$variables['breadcrumb'] = $breadcrumb;
πŸ’¬ Support request
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States sassafrass

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @sassafrass
  • πŸ‡ΊπŸ‡ΈUnited States Greg Boggs Portland Oregon

    I hacked this together from Stack Exchange. I'm sure there's a much cleaner way to code that :)

    https://drupal.stackexchange.com/questions/191548/how-do-i-build-breadcr...

    /**
     * Override or insert variables into the node template.
     *
     * @param $vars
     *   An array of variables to pass to the theme template.
     */
    function multcolibtheme_preprocess_node(&$variables) {
      if (\Drupal::routeMatch()->getRouteName() == 'view.library_locations.page_1') {
        if (in_array($variables['node']->id(), array_column($variables['view']->result, 'nid'))) {
    
          // Initialize an array to store breadcrumbs for nodes in the view.
          $breadcrumbs = [];
    
          // Loop through each result in the view.
          foreach ($variables['view']->result as $result) {
            if ($result->nid == $variables['node']->id()) {
    
              // Set the context to the current node.
              $node = \Drupal\node\Entity\Node::load($variables['node']->id());
    
              if ($node) {
                $routeName = $node->toUrl()->getRouteName();
                $routeParameters = $node->toUrl()->getRouteParameters();
                $route = Drupal::service('router.route_provider')->getRouteByName($routeName);
                $routeMatch = new Drupal\Core\Routing\RouteMatch($routeName, $route, $routeParameters, $routeParameters);
                $variables['search_breadcrumb'] = Drupal::service('easy_breadcrumb.breadcrumb')->build($routeMatch)->toRenderable();
              }
            }
          }
        }
      }
    }
    
  • πŸ‡ΊπŸ‡ΈUnited States Greg Boggs Portland Oregon

    I didn't add a template, and I'm not sure about the toRenderable() bit on the end there. But, that code should be close.

  • πŸ‡ΊπŸ‡ΈUnited States sassafrass

    Awesome! That worked! Thank-you so much!

  • Status changed to Fixed 10 months ago
  • πŸ‡ΊπŸ‡ΈUnited States Greg Boggs Portland Oregon

    Woot!

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024