use Drupal\Core\Link;
use Drupal\Core\ParamConverter\ParamNotConvertedException;
use Drupal\Core\Path\CurrentPathStack;
+use Drupal\Core\Path\PathMatcherInterface;
use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
use Drupal\Core\Routing\RequestContext;
use Drupal\Core\Routing\RouteMatch;
*/
protected $currentUser;
+ /**
+ * The current path service.
+ *
+ * @var \Drupal\Core\Path\CurrentPathStack
+ */
+ protected $currentPath;
+
+ /**
+ * The patch matcher service.
+ *
+ * @var \Drupal\Core\Path\PathMatcherInterface
+ */
+ protected $pathMatcher;
+
/**
* Constructs the PathBasedBreadcrumbBuilder.
*
* The current user object.
* @param \Drupal\Core\Path\CurrentPathStack $current_path
* The current path.
+ * @param \Drupal\Core\Path\PathMatcherInterface $path_matcher
+ * The path matcher service.
*/
- 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) {
+ 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) {
$this->context = $context;
$this->accessManager = $access_manager;
$this->router = $router;
$this->titleResolver = $title_resolver;
$this->currentUser = $current_user;
$this->currentPath = $current_path;
+ $this->pathMatcher = $path_matcher ?: \Drupal::service('path.matcher');
}
/**
$breadcrumb = new Breadcrumb();
$links = [];
+ // Add the url.path.parent cache context. This code ignores the last path
+ // part so the result only depends on the path parents.
+ $breadcrumb->addCacheContexts(['url.path.parent']);
+
+ // Do not display a breadcrumb on the frontpage.
+ if ($this->pathMatcher->isFrontPage()) {
+ return $breadcrumb;
+ }
+
// General path-based breadcrumbs. Use the actual request path, prior to
// resolving path aliases, so the breadcrumb can be defined by simply
// creating a hierarchy of path aliases.
// /user is just a redirect, so skip it.
// @todo Find a better way to deal with /user.
$exclude['/user'] = TRUE;
- // Add the url.path.parent cache context. This code ignores the last path
- // part so the result only depends on the path parents.
- $breadcrumb->addCacheContexts(['url.path.parent']);
while (count($path_elements) > 1) {
array_pop($path_elements);
// Copy the path elements for up-casting.
$links[] = new Link($title, $url);
}
}
-
- }
- if ($path && '/' . $path != $front) {
- // Add the Home link, except for the front page.
- $links[] = Link::createFromRoute($this->t('Home'), '<front>');
}
+ // Add the Home link.
+ $links[] = Link::createFromRoute($this->t('Home'), '<front>');
+
return $breadcrumb->setLinks(array_reverse($links));
}