1

The current breadcrumb only shows 'home'. I would like to update it so it shows 'home>shoes>Tennis Shoe' where 'shoes' is referenced content type(product display) and 'Tennis shoes' the instance(node) title of that content type

Here is how i tried to accomplish this.

I would push additional items(shoes, tennis shoe) into vars[breadcrumb] at page level(theme_preprocess_page) as following:

function THEME_preprocess_page(&$vars, $hook) {
...
$vars['breadcrumb'][] = '<a href="/shoes">shoes<a>';
$vars['breadcrumb'][] = '<a href="/shoes/tennis-shoe">Tennis Shoe<a>';
...
}

And then let my theme_breadcrumb() handle it as it walks through the array:

function THEME_breadcrumb($variables) {
    $breadcrumb = $variables['breadcrumb'];
    if (!empty($breadcrumb)) {
        $breadcrumbs = '<ul class="breadcrumb">';
        $count = count($breadcrumb) - 1;
        foreach ($breadcrumb as $key => $value) {
            if ($count != $key) {
                $breadcrumbs .= '<li>' . $value . '<span class="divider">/</span></li>';
            }
            else{
                $breadcrumbs .= '<li>' . $value . '</li>';
            }
        }
        $breadcrumbs .= '</ul>';
        return $breadcrumbs;
    }
}

This doesn't work. Whenever I add the items to the vars['breadcrumb'] in the preprocessor function as showed above, it never goes to 'THEME_breadcrumb', instead displays 'array' text. (apparently, it expects non array at vars['breadcrumb']) How do you manipulate breadcrumbs? Whats wrong with my reasoning? Thank You

Drupal 7, base of boostrap theme

2

I hope that you have set $breadcrumb in page.tpl.php. Secondly, you do not need to do it by using theme_preprocess_page.

Just implement using THEME_breadcrumb like this:

function theme_breadcrumb($variables) {
  $breadcrumb = $variables['breadcrumb'];
  $output = '';
  if ((arg(0) == 'node') && is_numeric(arg(1)) && ($node = node_load(arg(1)))) {
    if ($node->type == 'TYPE NAME') {
      $breadcrumb[] = $node->title;
    }
  }
  if (!empty($breadcrumb)) {
    $output.= '<div class="breadcrumb">' . implode(' › ', $breadcrumb) . '</div>';
  }
  return $output;
}
1

You can also use drupal_set_breadcrumb() in your template_preprocess_page() hook. Something like:

function THEMENAME_preprocess_page(&$variables) {
  // Wrap the code below with some logic here to target just content type you want, like
  // an if() block that looks for $variables['type'] == 'shoe' or whatever

  // Building breadcrumbs
  $breadcrumb = array();
  $breadcrumb[] = l('Home', '<front>');
  $breadcrumb[] = l('Shoes', 'shoes');
  // Setting breadcrumbs
  drupal_set_breadcrumb($breadcrumb);
}

This only sets the breadcrumb trail, so the current page title will automatically be placed at the end of the breadcrumbs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.