0

I am trying to make a custom taxonomy breadcrumbs based the code from this question: Drupal 8 How to do a custom taxonomy breadcrumb?

This line adds a new breadcrumb for the home page:

$breadcrumb->addLink(Link::createFromRoute(t('Home'), 'front'));

PS: I removed parenthesis from front to display line here.

I want to add another breadcrumb with the text of the name of a taxonomy term and the URL of the same term:

$breadcrumb->addLink(Link::createFromRoute(t($term->getName()), $url));

I cannot figure out how to replace "front" with the route name of the $term object.

  • try using like $breadcrumb->addLink(\Drupal\Core\Link::fromTextAndUrl(t($term->getName()), $url))); – Ajay Reddy Oct 17 '17 at 14:12
  • fromTextAndUrl Function takes url object as an input, how can i get the url object of a certain term ? – Bassem Mohamed Oct 17 '17 at 14:30
  • Use this \Drupal\Core\Url::fromUserInput("YOUR LINK GOES HERE") – Ajay Reddy Oct 17 '17 at 14:32
  • see also drupal.stackexchange.com/questions/233530/… – HongPong Apr 24 '18 at 18:25
2

Since taxonomy terms are entities, you can simply do this:

$link = $term->toLink();
$breadcrumb->addLink($link);

As a side-note, the route name for taxonomy terms is "entity.taxonomy_term.canonical".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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