You are here

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

Expanded class hierarchy of BreadcrumbBuilder

1 string reference to 'BreadcrumbBuilder'
current_page_crumb.services.yml in ./current_page_crumb.services.yml
current_page_crumb.services.yml
1 service uses BreadcrumbBuilder
current_page_crumb.breadcrumb in ./current_page_crumb.services.yml
Drupal\current_page_crumb\BreadcrumbBuilder

File

src/BreadcrumbBuilder.php, line 21

Namespace

Drupal\current_page_crumb
View 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

Namesort descending Modifiers Type Description Overrides
BreadcrumbBuilder::build public function Builds the breadcrumb. Overrides PathBasedBreadcrumbBuilder::build
PathBasedBreadcrumbBuilder::$accessManager protected property The menu link access service.
PathBasedBreadcrumbBuilder::$config protected property Site config object.
PathBasedBreadcrumbBuilder::$context protected property The router request context.
PathBasedBreadcrumbBuilder::$currentPath protected property The current path service.
PathBasedBreadcrumbBuilder::$currentUser protected property The current user object.
PathBasedBreadcrumbBuilder::$pathMatcher protected property The patch matcher service.
PathBasedBreadcrumbBuilder::$pathProcessor protected property The inbound path processor.
PathBasedBreadcrumbBuilder::$router protected property The dynamic router service.
PathBasedBreadcrumbBuilder::$titleResolver protected property The title resolver.
PathBasedBreadcrumbBuilder::applies public function Whether this breadcrumb builder should be used to build the breadcrumb. Overrides BreadcrumbBuilderInterface::applies
PathBasedBreadcrumbBuilder::getRequestForPath protected function Matches a path in the router.
PathBasedBreadcrumbBuilder::__construct public function Constructs the PathBasedBreadcrumbBuilder.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.