The basic set up: I have a custom content type called 'Guide'. Guides have an imagefield and some other custom fields; it is designed to be the top level/root of a book. All child pages of Guides are created with the regular book pages content type, but not all book pages on this site are children of Guides.

I have a little bit of custom code in a module for setting the breadcrumb of the Guide. There is an index page (View) that lists all Guides on the site, and when you're on a Guide node, the second breadcrumb is set to that page. Pretty basic stuff.

/**
 * Implements hook_node_view
 */
function gta_web_guides_node_view($node, $view_mode, $langcode) {
  $type_crumbs = array(
    'web_guide' => array('title' => 'Web Guides', 'path' => 'web-guides'),
  );
  
  // Make sure we're rendering in full display mode AND the content type has a custom breadcrumb defined
  if ('full' != $view_mode || !array_key_exists($node->type, $type_crumbs)) {
    return;
  }
  
  $crumbs[] = l('Home', '<front>');
  $crumbs[] = (!empty($type_crumbs[$node->type]['path'])) ? l($type_crumbs[$node->type]['title'], $type_crumbs[$node->type]['path']) : $type_crumbs[$node->type]['title'];
  
  drupal_set_breadcrumb($crumbs); 
}

Returns

Home -> Guides -> An Awesome Guide

Of course, when you click on a child page of the Guide, the breadcrumb does not include the Guides index page.

Home -> An Awesome Guide -> An Informative Child Page

So the question is, how do I get this:

Home -> Guides -> An Awesome Guide -> An Informative Child Page

How do I [1] Add the index page to the trail while [2] maintaining the full hierarchical breadcrumb for each book page and [3] only adding to breadcrumbs on book pages where the root is a Guide?

Comments

jenna.tollerson’s picture

/**
 * Determine if a node is part of a Web Guide book
 * _book_root is not a drupal hook
 */
function gta_web_guides_book_root($node) {
  if (!empty($node->book['bid'])) {
    $bid = $node->book['bid']; // $bid is same as $nid of book root
    $bookroot = node_load($bid);
    if ($bookroot->type == 'web_guide') {
      return TRUE;
    } else {
      return FALSE;
    }
  }
}

/**
 * Implements hook_node_view
 */
function gta_web_guides_node_view($node, $view_mode, $langcode) {
  // Make sure we're rendering in full display mode, and we're in a web guide
  if ('full' != $view_mode ||  (gta_web_guides_book_root($node) == FALSE)) {
    return;
  }

  $crumbs = menu_get_active_breadcrumb(); // get current breadcrumb so we can tear it up
  $web_guide_crumb = l('Web Guides', 'web-guides');
  // insert Web Guides breadcrumb after home
  array_splice($crumbs, 1, 0, $web_guide_crumb);
  
  drupal_set_breadcrumb($crumbs);
}

It works, but if anyone has ideas on how to improve this code, I am ready to learn.