Last active
August 29, 2015 14:05
-
-
Save netsensei/d333c87b8e44e2591be8 to your computer and use it in GitHub Desktop.
Drupal: Automagically set breadcrumbs for Taxonomy Menu without Menu Position rules.
This file contains 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 | |
/** | |
* Implements hook_node_view(). | |
* | |
* Using Taxonomy Menu and you want your breadcrumbs / active trail to follow suit? | |
* This snippet will handle that so that your breadcrumbs can / might look like this: | |
* | |
* home >> term A >> subterm B >> node title | |
* | |
* How to use: | |
* 1) Set the $node->type to the bundle you want to target | |
* 2) Set 'field_article_taxonomyreference' to the relevant taxonomy reference field. | |
* 3) Profit!! | |
* | |
* Why not use Menu Position module? | |
* You could, but you might end up creating a rule per menu item. With taxonomy menu, | |
* and a somewhat complex menu tree, such a solution wouldn't scale well. | |
*/ | |
function hook_node_view($node, $view_mode, $langcode) { | |
if ($node->type == 'article' && $view_mode == 'full') { | |
$items = field_get_items('node', $node, 'field_article_taxonomyreference'); | |
if (!empty($items)) { | |
$item = array_pop($items); | |
$term = $item['taxonomy_term']; | |
$path = taxonomy_menu_create_path($term->vid, $term->tid); | |
// Set the active menu_tree path | |
menu_tree_set_path('main-menu', $path); | |
// Manually set the preferred link for this path so that | |
// menu_get_active_trail() returns the proper trail. | |
$preferred_links = &drupal_static('menu_link_get_preferred'); | |
$preferred_links[$_GET['q']][MENU_PREFERRED_LINK] = menu_link_get_preferred($path); | |
// Add the node path at the end of the trail. | |
$active_trail = menu_set_active_trail(); | |
$menu_item = array( | |
'title' => $node->title, | |
'href' => 'node/' . $node->nid, | |
'link_path' => 'node/' . $node->nid, | |
'type' => MENU_CALLBACK, | |
'localized_options' => array(), | |
); | |
array_push($active_trail, $menu_item); | |
menu_set_active_trail($active_trail); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment