Friday, June 8, 2018

How to add breadcrumbs to the site

This is a quick how to:

Inside template.php or any custom module, add this function:
function passlabs_breadcrumb($variables) {
    $breadcrumb = $variables['breadcrumb'];
    $normal_path = trim($_GET['q'], '/');
    $path_alias = drupal_get_path_alias($normal_path);
    $alias_parts = explode('/', $path_alias);

    //fix bug where the "Blog" breadcrumb is not visible in the main blog page
    if (end($alias_parts) == "blog") {
        $blog_term = "Blog";
    } else {
        $blog_term = "";
    }

    //Remove unusuable breadcrumbs from blog or product detail pages
    if (strpos($path_alias, "blog") !== false ) {
        $healthy = "My Pass"; //gets rid of "My Pass" link for blog page
    } else {
        $healthy = array('Amplifiers','Amplifier', 'Preamplifiers','Preamplifier', 'Products', 'My Pass', 'Company', 'Support');
    }

    $b_items = 'In the press'; //these breadcrumb terms will also be replaced/eliminated

    // Set the page title by capitalizing the first letter of the alias
    $page_title = ucfirst($alias_parts[0]);

    if ($alias_parts[0] != $path_alias) {
        $breadcrumb[] = l($page_title, $alias_parts[0]);
        $breadcrumb[] = l(drupal_get_title(), $normal_path); // Link to current URL
    }
    // In order to avoid duplicating the top level path, check for a top level page
    else if ($alias_parts[0] == $path_alias) {
        $breadcrumb[] = l(drupal_get_title(), $path_alias);
    }

    $output = '';

  if (count($breadcrumb) > 1) {
    if ($breadcrumb) {
        $filter1 = str_replace($healthy, "nothing", $breadcrumb); //removes items from breadcrumbs
        $filter2 = str_replace($b_items, "nothing", $filter1); //removes items from breadcrumbs
    foreach ($filter2 as $value84) {
    if (strpos($value84, 'nothing') !== false) {
        $output .= '';
    } else {
        $output .= str_replace(" Series", "", $value84) . ' <span class="fa fa-angle-right">&nbsp;</span> </li><li>';
    }
    }
    return '<ul class="breadcrumb"><li>' . $output . $blog_term . '</li></ul>';
    }
  }
}
See comments inside code for more details
Inside your template's theme file add the following, in this case we used live-layout.tpl.php:
<?php print $breadcrumb; ?>
This will work using the main menu structure (from backend)

No comments:

Post a Comment