Wednesday, January 5, 2011

Drupal breadcrumb categories

There are two hierarchies in Drupal that you can bind your breadcrumbs to . You can bind your breadcrumbs to either the sites menu, or the sites taxonomy but you can't do either right out of the box. Unfortunately when you create a page you aren't given the option of using a custom breadcrumb trail. You can't just type it in a field and hit "go".

There are modules that take advantage of both the menu and taxonomy structures. Here is a breadcrumb module comparison that shows some of the features of the main players.

As a last resort, you can always create a series of templates based on the particular node (by creating node-x.tpl.php files) or create a custom block just for that page that you can position using CSS but why not let a module do the work for you?

Sunday, January 2, 2011

Theming breadcrumbs

Most of the themes out there for Drupal run on PHPTemplate these days and PHPTemplate uses template.php in order to store it's theme functions.

This is where you will find (or place) the base function for the output for the breadcrumb.

In this example I'm looking at the Marinelli theme. Inside the template.php file the code that returns the themed breadcrumb trail follows:
/**
* Return a themed breadcrumb trail.
*
* @param $breadcrumb
* An array containing the breadcrumb links.
* @return a string containing the breadcrumb output.
*/
function phptemplate_breadcrumb($breadcrumb) {
if (!empty($breadcrumb)) {
$breadcrumb[] = drupal_get_title();
array_shift($breadcrumb);
return '<div class="path"><p><span>'.t('You are here').'</span>'. implode(' / ', $breadcrumb) .'</p></div>';
}
}
What this function does is to insert some HTML around the existing breadcrumb while spreading the stored variable array out into text, fresh and ready to display in the page.tpl.php or other theme template file.

This is an example of an already over-ridden (themed) breadcrumb. You could use your own implementation if nothing exists by placing a similar function into your own template.php file (when using the PHPTemplate system -- which you likely are by default).

The finalized $breadcrumb output is going to look something like this:
<div class="path"><p><span>You are here</span>Autos </p></div>

You can style it using CSS how you see fit.

What this doesn't address however is how to get more than one category listed in the breadcrumb. For that you're going to need some help addressed in the next post, Drupal breadcrumb categories.