Setting breadcrumbs to exactly what you want
In Drupal it is quite easy to set breadcrumbs with the function drupal_set_breadcrumb(), which takes an array of links (as you would make with the l() function). It's a bit of a blunt instrument but it works (and wins out over hook_menu_breadcrumb_alter()).
The situation being addressed here is individual node pages which have no breadcrumb to start and need one that matches a giant filtering view. The code below, and the view, are all in the Cultura Archive module if you want all the context.
This was found to be easier than configuring Custom Breadcrumbs, if it is indeed possible to achieve the same result.
<?php
/**
* Implements hook_node_view_alter().
*/
function cultura_archive_node_view_alter(&$build) {
if ($build['#bundle'] == CULTURA_DISCUSSION_NODE_TYPE && $build['#view_mode'] == 'full') {
$node = &$build['#node'];
$year = cultura_archive_term($node, CULTURA_YEAR);
$semester = cultura_archive_term($node, CULTURA_SEMESTER);
$host = cultura_archive_term($node, CULTURA_HOST_SCHOOL);
$guest = cultura_archive_term($node, CULTURA_GUEST_SCHOOL);
$breadcrumb = drupal_get_breadcrumb();
$path = 'cultura-exchanges';
if ($year) {
$path .= '/year/';
$breadcrumb[] = cultura_archive_breadcrumb_link($year, $path);
}
if ($semester) {
$path .= '/semester/';
$breadcrumb[] = cultura_archive_breadcrumb_link($semester, $path);
}
if ($host) {
$path .= '/host/';
$breadcrumb[] = cultura_archive_breadcrumb_link($host, $path);
}
if ($guest) {
$path .= '/guest/';
$breadcrumb[] = cultura_archive_breadcrumb_link($guest, $path);
}
drupal_set_breadcrumb($breadcrumb);
}
}
/**
* Helper function to load the taxonomy term for a given field.
*/
function cultura_archive_term($node, $field) {
$items = field_get_items('node', $node, $field);
if ($items) {
return taxonomy_term_load($items[0]['tid']);
}
else {
return FALSE;
}
}
/**
* Helper function to make a link in the breadcrumb chain out of a term.
*/
function cultura_archive_breadcrumb_link($term, &$path) {
module_load_include('inc', 'pathauto');
$path .= pathauto_cleanstring($term->name);
return l($term->name, $path);
}
?>
http://browse-tutorials.com/snippet/alter-node-breadcrumbs-hook-form-your-module-drupal-7
See cultura_archive.module for my own version of this, but choosing to call drupal_set_breadcrumb() from hook_node_view_alter() was the main thing i validated/stole from this post.
Comments
Post new comment