1

I have a route that renders a node. Unfortunately in the breadcrumb the route title is rendered rather than the node title. When I preprocess in hook_preprocess_breadcrumb() inside my module only the preceding links are available inside $variable (not the current route title).

However, if I go to do hook_preprocess_breadcrumb() in my theme, the route title is inside $variables. So I could change it there but I want the functionality to be encapsulated inside my module, not split between module and theme.

I tried to create my own custom breadcrumb builder but I don't want to build it from scratch as there is some complex functionality built by other developers. I want to get the current breadcrumbs and then just replace the last one (the current page title) with my own.

I tried hook_system_breadcrumb_alter() as well but again the current page title is not showing up in variables inside my module.

<?php

class DefaultProductListBreadcrumbService implements BreadcrumbBuilderInterface {

  /**
   * Whether this breadcrumb builder should be used to build the breadcrumb.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   *
   * @return bool
   *   TRUE if this builder should be used or FALSE to let other builders
   *   decide.
   */
  public function applies(RouteMatchInterface $route_match) {
    // TODO: Implement applies() method.
    return true;
  }

  /**
   * Builds the breadcrumb.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   *
   * @return \Drupal\Core\Breadcrumb\Breadcrumb
   *   A breadcrumb.
   */
  public function build(RouteMatchInterface $route_match) {
    // TODO: Implement build() method.

    $breadcrumb = new Breadcrumb();
    $breadcrumb->addLink(Link::createFromRoute('Home', '<front>'));

    return $breadcrumb;
  }

}

For reference, I'm using bootstrap as the base theme with multiple sub themes (it's a multisite). I can't find anywhere that's adding the page title in the theme. I'm also using easy_breadcrumb module but I checked there and the page title is also not shown there.

1 Answer 1

1

You could use a title callback, like for example this one from the core node controller:

\Drupal\node\Controller\NodeViewController::title

  public function title(EntityInterface $node) {
    return $this->entityManager->getTranslationFromContext($node)->label();
  }

Your title callback would have other route parameters, so that you can find the correct node, see also https://drupal.stackexchange.com/a/248687/47547.

When using the default option in the Bootstrap theme to display the page title you might need to add cache metadata to the breadcrumb, see Breadcrumbs are cached, which depends on how you determine the node to display.

4
  • This does change the title of the breadcrumb but for example, what if I want the <title> tag and page title to be one thing e.g. 'New York Accomodation' but the breadcrumb to be just 'accomodation'. I still have the same problem in that the hooks do not show the title in the module, only on the theme layer.
    – chap
    Oct 24, 2017 at 23:22
  • This should work with the current title handling in Drupal, set 'accomodation' in _title_callback and 'New York Accomodation' in #titleof the main content (this is $build['#title'] in the controller).
    – 4uk4
    Oct 25, 2017 at 6:22
  • Unfortunately the <title> tag will be the same as the breadcrumb if I do that.
    – chap
    Oct 25, 2017 at 7:08
  • No, for the <title> tag, #title in the main content takes precedence over the route title. Check for other code trying to set the title, for example Metatag.
    – 4uk4
    Oct 25, 2017 at 7:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.