Modifying the breadcrumb variable at will

PLEASE NOTE! The following snippet is user submitted. Use at your own risk! For users who have setup drupal using an alternate database to the default (MYSQL), please note that the snippets may contain some database queries specific to MYSQL.

Setting breadcrumbs manually

Here's the manual way to set breadcrumbs. drupal_set_breadcrumb() expects an array of links:

<?php
$links
= array(l(t('about'), 'about'), l(t('bios'), 'about/bios'));
drupal_set_breadcrumb($links);
?>

will create a breadcrumb like

about >> bios

Of course if you put this code in page.tpl.php then every page will have the same breadcrumb (doh!). So, use the manual method in the body of an individual page or node. Or, figure out a more dynamic way to change all breadcrumbs in page.tpl.php.

Remove the "Home" breadcrumb link dynamically

Here is some code to modify the breadcrumb for every page. Insert this code anywhere in your page.tpl.php file to get rid of the "Home" link in the breadcrumb.

<?php
$breadcrumb
= drupal_get_breadcrumb();
if (
$breadcrumb[0] == 'home'){
 
// shift out first element
 
array_shift($breadcrumb);
 
drupal_set_breadcrumb($breadcrumb);
}
?>

If before the breadcrumb was Home >> About >> Bios
After the above code is inserted into page.tpl.php the breadcrumb would be About >> Bios

<?php print $breadcrumb ?>

zirafa - August 13, 2006 - 02:34

<?php
print $breadcrumb
?>
should work just fine. If it says there is an array, then drupal_set_breadcrumb() is probably not firing for some reason in your logic.

I had to point this out.

solipsist - August 28, 2006 - 09:51

I had to point this out. Common error in the code above #1: = means assignment, == means comparison. The if clause above assigns a value, it doesn't compare.

Secondly, using the var $breadcrumb overwrites the phpTemplate string var for the breadcrumb.

This code should work:

<?php
$breadcrumb_array
= drupal_get_breadcrumb();
if (
$breadcrumb_array[0] == 'home'){
 
// shift out first element
 
array_shift($breadcrumb_array);
 
drupal_set_breadcrumb($breadcrumb_array);
}
?>

--
Jakob Persson
web design and usability consulting
http://www.jakob-persson.com

Does this actually work?

jeff h - September 14, 2006 - 04:00

Does this actually work for anyone? It seems to me that $breadcrumb_array[0] never == "home". Because of all the link code on either side of the word Home, element 0 in the array contains "Home" (note the capital), but doesn't equal "home".

Even with the above adjustments, the following simply prints the original breadcrumb trail, including home.

$breadcrumb_array = drupal_get_breadcrumb();
if (strpos($breadcrumb_array[0], 'Home')) {
  // shift out first element
  array_shift($breadcrumb_array);
  drupal_set_breadcrumb($breadcrumb_array);
print $breadcrumb_array[0];
};
print $breadcrumb;

Theme function

solipsist - September 14, 2006 - 12:33

jeff: You need to make a theme function override. By the time the var is available in the template it is a string.

<?php
function themename_breadcrumb($breadcrumb) {
  if (!empty(
$breadcrumb)) {
   
array_shift($breadcrumb); //gets rid of "Home"
   
return '<div class="breadcrumb">' . implode('&nbsp;&raquo;&nbsp;', $breadcrumb) . '</div>';
  }
}
?>

--
Jakob Persson
web design and usability consulting
http://www.jakob-persson.com

Breadcrumb content

rares - December 5, 2007 - 17:31

This:
$breadcrumb[0] == 'home'
is just a wrong way to picture breadcrumb data. The array elements are links: resulting from l function calls or coding.

 
 

Drupal is a registered trademark of Dries Buytaert.