class BreadcrumbBuilder in Current Page Crumb 8
Adds the current page title to the breadcrumb.
Extend PathBased Breadcrumbs to include the current page title as an unlinked crumb. The module uses the path if the title is unavailable and it excludes all admin paths.
Hierarchy
- class \Drupal\system\PathBasedBreadcrumbBuilder implements BreadcrumbBuilderInterface uses StringTranslationTrait
- class \Drupal\current_page_crumb\BreadcrumbBuilder
Expanded class hierarchy of BreadcrumbBuilder
1 string reference to 'BreadcrumbBuilder'
1 service uses BreadcrumbBuilder
File
- src/
BreadcrumbBuilder.php, line 21
Namespace
Drupal\current_page_crumbView source
class BreadcrumbBuilder extends PathBasedBreadcrumbBuilder {
/**
* {@inheritdoc}
*/
public function build(RouteMatchInterface $route_match) {
$breadcrumbs = parent::build($route_match);
$request = \Drupal::request();
$path = trim($this->context
->getPathInfo(), '/');
$path_elements = explode('/', $path);
$route = $request->attributes
->get(RouteObjectInterface::ROUTE_OBJECT);
// Do not adjust the breadcrumbs on admin paths and front page.
if ($route && !$route
->getOption('_admin_route') && !$this->pathMatcher
->isFrontPage()) {
$title = $this->titleResolver
->getTitle($request, $route);
if (!isset($title)) {
// Fallback to using the raw path component as the title if the
// route is missing a _title or _title_callback attribute.
$title = str_replace([
'-',
'_',
], ' ', Unicode::ucfirst(end($path_elements)));
}
$breadcrumbs
->addLink(Link::createFromRoute($title, '<none>'));
}
// Handle expiring views paths and any entity default page cache.
$parameters = $route_match
->getParameters();
foreach ($parameters as $key => $parameter) {
if ($key === 'view_id') {
$breadcrumbs
->addCacheTags([
'config:views.view.' . $parameter,
]);
}
if ($parameter instanceof CacheableDependencyInterface) {
$breadcrumbs
->addCacheableDependency($parameter);
}
}
// Expire the cache when things need to update based on route, path and language.
$breadcrumbs
->addCacheContexts([
'route',
'url.path',
'languages',
]);
return $breadcrumbs;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BreadcrumbBuilder:: |
public | function |
Builds the breadcrumb. Overrides PathBasedBreadcrumbBuilder:: |
|
PathBasedBreadcrumbBuilder:: |
protected | property | The menu link access service. | |
PathBasedBreadcrumbBuilder:: |
protected | property | Site config object. | |
PathBasedBreadcrumbBuilder:: |
protected | property | The router request context. | |
PathBasedBreadcrumbBuilder:: |
protected | property | The current path service. | |
PathBasedBreadcrumbBuilder:: |
protected | property | The current user object. | |
PathBasedBreadcrumbBuilder:: |
protected | property | The patch matcher service. | |
PathBasedBreadcrumbBuilder:: |
protected | property | The inbound path processor. | |
PathBasedBreadcrumbBuilder:: |
protected | property | The dynamic router service. | |
PathBasedBreadcrumbBuilder:: |
protected | property | The title resolver. | |
PathBasedBreadcrumbBuilder:: |
public | function |
Whether this breadcrumb builder should be used to build the breadcrumb. Overrides BreadcrumbBuilderInterface:: |
|
PathBasedBreadcrumbBuilder:: |
protected | function | Matches a path in the router. | |
PathBasedBreadcrumbBuilder:: |
public | function | Constructs the PathBasedBreadcrumbBuilder. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |