User login

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.

Searched words: 
which hook to alter breadcrumbs custom breadcrumbs Adding breadcrumbs to a node

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.