Skip to content

Instantly share code, notes, and snippets.

@Greg-Boggs
Created March 6, 2016 01:45
Show Gist options
  • Select an option

  • Save Greg-Boggs/2338e65e6e60b8812ed7 to your computer and use it in GitHub Desktop.

Select an option

Save Greg-Boggs/2338e65e6e60b8812ed7 to your computer and use it in GitHub Desktop.
Current Page Crumb Breadcrumb Builder. Enable this module to add the current page to the Drupal 8 breadcrumb https://www.drupal.org/sandbox/gregboggs/2664958
<?php
/**
* @file
* Contains \Drupal\current_page_crumb\BreadcrumbBuilder.
*/
namespace Drupal\current_page_crumb;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Component\Utility\Unicode;
use Drupal\system\PathBasedBreadcrumbBuilder;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
/**
* 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.
*
* {@inheritdoc}
*/
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.
if ($route && !$route->getOption('_admin_route')) {
$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(array('-', '_'), ' ', Unicode::ucfirst(end($path_elements)));
}
$breadcrumbs->addLink(Link::createFromRoute($title, '<none>'));
}
return $breadcrumbs;
}
}
@geerlingguy

Copy link
Copy Markdown

Note that this change should also include the following before the final return statement, so caching is configured correctly:

    // Add the full URL path as a cache context, since we will display the
    // current page as part of the breadcrumb.
    $breadcrumbs->addCacheContexts(['url.path']);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment