public function BreadcrumbBuilder::build in Custom Breadcrumbs 1.x
Builds the breadcrumb.
Parameters
\Drupal\Core\Routing\RouteMatchInterface $route_match: The current route match.
Return value
\Drupal\Core\Breadcrumb\Breadcrumb A breadcrumb.
Overrides BreadcrumbBuilderInterface::build
File
- src/
BreadcrumbBuilder.php, line 164
Class
- BreadcrumbBuilder
- Class BreadcrumbBuilder.
Namespace
Drupal\custom_breadcrumbsCode
public function build(RouteMatchInterface $route_match) {
$breadcrumb = new Breadcrumb();
// Set up homepage link.
if ($this->customBreadcrumbsSettings['home'] && !$this->pathMatcher
->isFrontPage()) {
$breadcrumb
->addLink(Link::createFromRoute($this->customBreadcrumbsSettingsData
->get('home_link'), '<front>'));
}
// Prepare all route parameters.
$params = $route_match
->getParameters()
->all();
// Check breadcrumbs by patch.
if ($breadcrumbSetting = $this
->matchPaths($route_match)) {
$this
->applyBreadcrumb($breadcrumb, $breadcrumbSetting, NULL);
}
else {
// Set up breadcrumbs by content entity configs.
$this
->applyContentEntityBreadcrumb($breadcrumb, $route_match);
}
// Set up the last current page crumb.
if ($this->customBreadcrumbsSettings['current_page'] && !$this->pathMatcher
->isFrontPage()) {
$title = '';
$route = '<none>';
$route_parameters = $route_parameters_link = [];
foreach ($params as $key => $value) {
if ($value instanceof ContentEntityInterface) {
$route_parameters_link[$key] = $value
->id();
$title = $value
->label();
}
}
if ($this->customBreadcrumbsSettings['current_page_link']) {
$route = $route_match
->getRouteName();
$route_parameters = $route_parameters_link;
}
// Title resolver works good when you render breadcrumb on full page,
// when we attach breadcrumb on node teaser, it doesn't work.
if (empty($title)) {
try {
$title = $this->titleResolver
->getTitle($this->currentRequest, $route_match
->getRouteObject());
} catch (\InvalidArgumentException $exception) {
$title = NULL;
}
}
if ($title != NULL) {
$breadcrumb
->addLink(Link::createFromRoute($this
->prepareTitle($title), $route, $route_parameters));
}
}
$breadcrumb
->addCacheContexts([
'url.path',
]);
return $breadcrumb;
}