I want to disable path based breadcrumbs if the current route is not a registered admin route. The front end of the site is driven entirely by Menu Breadcrumb, but in some cases the URL path is generated dynamically with Pathauto based on a combination of field values. This results in sometimes the parent paths generated cause path based breadcrumb in core to return a breadcrumb when it should not (if the current page is not in the menu, Menu Breadcrumb is skipped). At the same time, I do not want breadcrumbs in the admin interfered with.
Is the way to support both cases to decorate the core PathBasedBreadcrumb service? Here is what I came up with:
My module service definition:
services:
mymodule.breadcrumb:
class: Drupal\mymodule\PathBasedBreadcrumbBuilderOverride
decorates: system.breadcrumb.default
decoration_priority: 10
public: false
arguments: [ '@router.request_context', '@access_manager', '@router', '@path_processor_manager', '@config.factory', '@title_resolver', '@current_user', '@path.current', '@path.matcher', '@router.admin_context']
The class:
/**
* Defines a class to build path-based breadcrumbs.
*/
class PathBasedBreadcrumbBuilderOverride extends PathBasedBreadcrumbBuilder {
/**
* The admin context service.
*
* @var \Drupal\Core\Routing\AdminContext
*/
protected $adminContext;
/**
* {@inheritdoc}
*/
public function __construct(RequestContext $context, AccessManagerInterface $access_manager, RequestMatcherInterface $router, InboundPathProcessorInterface $path_processor, ConfigFactoryInterface $config_factory, TitleResolverInterface $title_resolver, AccountInterface $current_user, CurrentPathStack $current_path, PathMatcherInterface $path_matcher = NULL, AdminContext $admin_context = NULL) {
parent::__construct($context, $access_manager, $router, $path_processor, $config_factory, $title_resolver, $current_user, $current_path, $path_matcher);
$this->adminContext = $admin_context;
}
/**
* {@inheritdoc}
*/
public function applies(RouteMatchInterface $route_match) {
return $this->adminContext->isAdminRoute();
}
}
applies
in that case only works from the admin. I am getting the result I would expect to get (no breadcrumb in the frontend of the site unless the current page is in the menu, letting Menu Breadcrumb be the only manager).
Are there other ways of doing this, or is this the simplest way without interfering with core functionality?