Created
June 22, 2017 13:07
-
-
Save RishiKulshreshtha/1b4f80a97b230f3aadf2c967b86ad483 to your computer and use it in GitHub Desktop.
Adding Taxonomy Term to the Node's Breadcrumb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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