background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount
Eric.London's picture

This code snippet will show you how to search through the breadcrumbs and remove a particular item. Since I added this code to my theme in a preprocess_page function, I had to recreate $variables['breadcrumb']. NOTE: If I had chose to add this code to a module instead, I could have simply used the drupal_get_breadcrumb() and drupal_set_breadcrumb functions.

<?php
function MYTHEME_preprocess_page(&$variables) {
 
// in this example, I'm checking to see if icon_users.png exists in the breadcrumbs
 
if (stripos($variables['breadcrumb'],'icon_users.png')) {
   
// get the current breadcrumbs
   
$bc = drupal_get_breadcrumb();

   
// loop through the breadcrumbs
   
foreach ($bc as $k => $v) {
     
// check for a condition and remove as necessary
     
if (stripos($v,'icon_users.png')) unset($bc[$k]);
    }

   
// recreate the breadcrumbs using the theme_breadcrumb function
   
$variables['breadcrumb'] = theme('breadcrumb', $bc);
  }
}
?>

Eric.London's picture

Here's a quick code snippet that shows you how you can create custom breadcrumbs for a certain node type. This code would reside in your template.php theme file.

<?php
function MYTHEME_preprocess_page(&$variables) {
  if (
$variables['node']->type == 'MYNODETYPE') {
       
   
$links = array();

   
// creating a link to the home page
   
$links[] = l('Home', '<front>');

   
// here's how you could add a link to a taxonomy page
   
$vid = 2;
    foreach (
$variables['node']->taxonomy as $k => $v) {
      if (
$v->vid = $vid) {  
       
$links[] = l($v->name, 'taxonomy/term/' . $v->tid);
        break;
      }
    }
       
   
// yet another link
   
$links[] = l('Some Other Link', 'SOMEOTHERLINK');

   
// lastly, overwrite the contents of the breadcrumbs variable in the page scope
   
$variables['breadcrumb'] = theme('breadcrumb', $links);
  }
}
?>

NOTE: if you were adding this code to a module, you could use the drupal_set_breadcrumb() function to do the same functionality.

Eric.London's picture

At some point, you may need to change a link in the breadcrumbs. I put this function in my theme's template.php file to overrides the theming of the breadcrumbs and manually replace one of the links.

<?php
function MYTHEME_breadcrumb($breadcrumb) {
  if (!empty(
$breadcrumb)) {
   
// loop through breadcrumbs
   
foreach ($breadcrumb as $k => $v) {
     
// convert a tag into xml
     
$o = simplexml_load_string($v);

     
// replace link
     
if ($o['href']=='/OLDLINK') {
       
// create new link
       
$breadcrumb[$k] = l($o, 'NEWLINK');
      }
    }

    return
'<div class="breadcrumb">'. implode(' » ', $breadcrumb) .'</div>';
  }
}
?>

Eric.London's picture

Here's a quick snippet to add the current page to the breadcrumbs. I added this code to my template.php file.

<?php
function MYTHEME_breadcrumb($breadcrumb) {
  if (!empty(
$breadcrumb)) {
   
// add current page
   
$breadcrumb[] = l(drupal_get_title(), $_REQUEST['q']);
    return
'<div class="breadcrumb">'. implode(' » ', $breadcrumb) .'</div>';
  }
}
?>

Syndicate content