0

I want to modify my bread crumb ( remove one link from bread crumb and add one link to bread drumb).

I tried,

/**
 * Implements hook_system_breadcrumb_alter().
 */
function modulename_system_breadcrumb_alter(\Drupal\Core\Breadcrumb\Breadcrumb &$breadcrumb, \Drupal\Core\Routing\RouteMatchInterface $route_match, array $context) {
// to get current path and alias 
  $current_path = \Drupal::service('path.current')->getPath();
  $result = \Drupal::service('path.alias_manager')->getAliasByPath($current_path);

// get current route name 
  $route_name = \Drupal::service('current_route_match')->getRouteName();
// check route is an quickinfo content create in group 
  if ($breadcrumb && $route_name == 'entity.group_content.group_node__quickinfo.create_form') {
// get bread crumb links
    $breadcrumb_links = $breadcrumb->getLinks();
// fetch titile of the current path    
    $title = \Drupal::service('title_resolver')->getTitle(\Drupal::request(), $route_match->getRouteObject());
    if (!empty($title)) {
// change third link of a breadcrumb 
     $breadcrumb_links['2'] = Link::createFromRoute(t('RZ-schnellinfo'), '<none>');
    }
//    set links again
   $breadcrumb->setLinks($breadcrumb_links);
  }
}

SetLinks() ==> work when only bread crumb links doesn't exist, i.e.. we can create new links by using addLinks(). Since we need to remove a link we can use this function .

Then I followed https://www.drupal.org/node/2026025 but I am getting below error.

exception 'Symfony\Component\DependencyInjection\Exception\LogicException' with message 'Service [error] 'xxxxx.breadcrumb_xxxx_create' for consumer 'breadcrumb' does not implement Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface.' in

2 Answers 2

1

As comment said above, you have to implement Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface. Here is a working snippet that I use to implement those Breadcrumbs :

It appends a custom routename to all 'Nodes' pages :


<?php

namespace Drupal\Your_Module_Namespace\Breadcrumb;

use Drupal;
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;

class Your_Module_NamespaceBreadCrumbBuilder implements BreadcrumbBuilderInterface {

  /**
   * {@inheritdoc}
   */
  public function applies(RouteMatchInterface $attributes) {
    $parameters = $attributes->getParameters()->all();
    // I need my breadcrumbs for a few node types ONLY,
    // so it should be applied on node page ONLY.
    if (isset($parameters['node']) && !empty($parameters['node'])) {
      return TRUE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function build(RouteMatchInterface $route_match) {
    $breadcrumb = new Breadcrumb();
    $breadcrumb->addLink(Link::createFromRoute('Home', '<front>'));

    $node = Drupal::routeMatch()->getParameter('node');
    $node_type = $node->bundle();

    // Put the needed logic here

    // addLink example
    $breadcrumb->addLink(Link::createFromRoute('My Page Name', 'drupal_internal.routename'));
    $breadcrumb->addCacheContexts(['route']);

    return $breadcrumb;
  }
}

0

Above in the start you should use this class .

use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;

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.