TransWikia.com

Get the breadcrumbs for a specific node other than the current

Drupal Answers Asked by Nils Riedemann on October 26, 2021

I have a list of nodes created by a view. Those nodes tend to have titles that are mostly the same, but since they are in a rather logical hierarchy defined by the menu I’d like to output the breadcrumb to the nodes as well.

I couldn’t find a core-function that does that. Anyone an idea how to work around/on that?

One Answer

Core breadcrumbs in Drupal 7 are implemented in a way that they only work for the current page. So to achieve this with core, you would have to "simulate" a page request and somehow harvest the breadcrumb. This is probably painful and not worth the effort.

In Drupal 8, you have breadcrumb builders that take a route match as an argument. This route match can be any page. So yeah, what you want would be possible in D8. But you are in Drupal 7.

In Drupal 7 with Crumbs module, breadcrumbs are calculated independently of the current page. You can see this in action on admin/structure/crumbs/demo.

E.g. crumbs_get_trail() accepts a $path argument, which can be any path you want. But this gives you a "trail", not a breadcrumb itself.

Unfortunately there is no one-liner to give you the breadcrumb for any page.

But here is what you can do:

  // Create a local version of the Crumbs service container.
  $services = new crumbs_DIC_ServiceContainer();
  $page_info = $services->page;
  $page_info->path = 'node/123';

  // Optional: Set display settings.
  $page_info->minTrailItems = 1;
  $page_info->showFrontPage = TRUE;
  $page_info->showCurrentPage = TRUE;

  // Now choose which piece of information you want.
  $trail = $page_info->trail;
  $breadcrumb_html = $page_info->breadcrumbHtml;
  $breadcrumb_data = $page_info->breadcrumbData;
  $breadcrumb_items = $page_info->breadcrumbItems;
  $raw_breadcrumb_items = $page_info->rawBreadcrumbItems;

This can be starting point for you to play around with.

If you use Crumbs for this purpose, you get the full package: All your breadcrumbs will now be controlled by Crumbs. Maybe this is what you want, or maybe not.

I am not aware of other breadcrumb-customizing modules which provide this functionality.

Answered by donquixote on October 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP