2

I have a D8 custom module providing its own breadcrumbs. The breadcrumbs are shown perfectly, but the current page (last item) is shown as a link. I want to change Change this but as I see it, the ::build method requires the $links to be returned as an array of Link.

use ...

class BreadcrumbsBuilder implements BreadcrumbBuilderInterface {

  public function __construct() {
    ...
  }

  public function applies(RouteMatchInterface $route_match) {

    $result = // calculate logic
    return $result;
  }

  public function build(RouteMatchInterface $route_match) {
    $crumbs = new Breadcrumb();
    // prepare the data
    $links = $this->buildLinks($prepared_data);

    $crumbs->buildLinks($links);
      return $crumbs;
  }

  private function buildLinks($prepared_data) {
    $links = [
      Link::createFromRoute(t('Home'), '<front>'),
      Link::createFromRoute($title, 'entity.node.canonical', ['node' => $this->node->id()]),
    ];

    foreach($prepared_data as $data) {
      $links[] = Link::fromTextAndUrl($page_title, Url::fromUserInput($dummy_data));
    }
    return $links;
  }

}

The crumbs are shown properly as:

home >> My Title >> page_1 >> page_2

But page_2 is the current page, so it should not show as a link

1 Answer 1

1

To answer my own question: The last link should point to route name '<none>' and that removes the href attribute from the item. So after all super easy.

// untested
foreach($prepared_data as $data) {
  $links[] = $is_last_link($data) 
     ? Link::createFromRoute($page_title, '<none>')
     : Link::fromTextAndUrl($page_title, Url::fromUserInput());
  }

Related: It appears Drupal 8 core does not provide the current page in the breadcrumbs. The contributed module Easy Breadcrumbs does this for D7. The D8 version is in still in sandbox. Reference: Blog post.

Your Answer

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

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