2

In Drupal 8, how can one determine the Menu Link Title of a node, given its URL and other information like its route & parameters?

This is to modify the core path-based breadcrumbs (PathBasedBreadcrumbBuilder.php) to use the Menu Link Title of a node, as breadcrumb link text, rather than the node's Title field. This would have to be a common requirement, since the default D8 breadcrumbs would be very long in deep menus with wordy titles. For example, we would need:

Home > Design > Drupal

instead of the D8 default:

Home > Introduction to Our Design Services > Drupal 8: The Best CMS for Everything

for all nodes in the "Drupal" sub-menu.

The default breadcrumb service method PathBasedBreadcrumbBuilder::build uses a bunch of injected services to look up & check routes to each ancestor pathname of the node URL, then ultimately TitleResolver::getTitle to get the titles for those routes. We would just need a way of looking up the corresponding MenuLinkContent instead so we could then get the menu title via MenuLinkContent::getTitle.

I have tried to outline the problem for the Drupal Module Development forum (change Breadcrumbs from Node Title to Menu Link Title) but got no response when asking how to simply bridge the gap between the space of URLs/routes and the space of menu link content. Maybe this means it's not possible without a traversal of the menu: if so I would deeply appreciate some idea of where to start with that approach.

0

The sandbox module "wb_breadcrumbs' did the trick for me: https://www.drupal.org/sandbox/oles89/2367919

0

If you've got a RouteMatch (e.g., your breadcrumb builder passing you the $route_match argument), you can use the MenuLinkManager service (@plugin.manager.menu.link), which when injected into your module could be used like the following (e.g., for the main menu):

$route_name = $route_match->getRouteName();
$route_parameters = $route_match->getRawParameters()->all();
$route_links = $this->menuLinkManager->
  loadLinksByRoute($route_name, $route_parameters, 'main');
$route_link = reset($route_links);

$menu_title = $route_link->getTitle();
$menu_url = $route_link->getUrlObject();

Regarding the breadcrumb implementation: that link and all its ancestors would be also be available via MenuLinkManager::getParentIds, as plugin IDs in the reverse order as they would appear in the breadcrumb trail:

$menu_plugin_id = $route_link->getPluginId();
$menu_parents = $this->menuLinkManager->getParentIds($menu_plugin_id);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.