Skip to content

Instantly share code, notes, and snippets.

@RishiKulshreshtha
Created June 22, 2017 13:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RishiKulshreshtha/1b4f80a97b230f3aadf2c967b86ad483 to your computer and use it in GitHub Desktop.
Save RishiKulshreshtha/1b4f80a97b230f3aadf2c967b86ad483 to your computer and use it in GitHub Desktop.
Adding Taxonomy Term to the Node's Breadcrumb
<?php
/**
* @file
* Place your custom PHP code in this file.
*/
/**
* Implements HOOK_preprocess_breadcrumb().
*/
function YOURTHEME_preprocess_breadcrumb(&$variables) {
if (($node = \Drupal::routeMatch()->getParameter('node')) && $variables['breadcrumb']) {
$breadcrumb = &$variables['breadcrumb'];
if (!empty($node->field_tags->entity)) {
$term_url = $node->field_tags->entity->toLink();
$node_url = array_pop($breadcrumb);
array_push($breadcrumb, $term_url);
array_push($breadcrumb, $node_url);
// Implementing Cache.
$variables['#cache']['contexts'][] = "url.path";
$variables['#cache']['tags'][] = "node:{$node->nid->value}";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment