Skip to content

Instantly share code, notes, and snippets.

@NickTomlin
Last active December 17, 2015 20:09
Show Gist options
  • Save NickTomlin/5665473 to your computer and use it in GitHub Desktop.
Save NickTomlin/5665473 to your computer and use it in GitHub Desktop.
Taxonomy breadcrumbs. All credit to Dave Ross (@csixty4).
// to be added in your theme's template.php file
/**
* Override theme_breadcrumb().
*
* Base breadcrumbs on paths e.g., about/our-organization/bob-jones
* turns into About Us > Our Organization > Bob Jones
*/
function sndev_breadcrumb($breadcrumb) {
$links = array();
$path = '';
$arguments = explode('/', request_uri());
foreach ($arguments as $key => $value) {
if (empty($value)) {
unset($arguments[$key]);
}
}
$arguments = array_values($arguments);
$links[] = l(t('Home'), '<front>');
if (!empty($arguments)) {
foreach ($arguments as $key => $value) {
if ($key == (count($arguments) - 1)) {
$links[] = drupal_get_title();
}
else {
if (!empty($path)) {
$path .= '/'. $value;
} else {
$path .= $value;
}
$menu_item = menu_get_item(drupal_lookup_path('source', $path));
if ($menu_item['title']) {
$links[] = l($menu_item['title'], $path);
}
else {
$links[] = l(ucwords(str_replace('-', ' ', $value)), $path);
}
}
}
}
drupal_set_breadcrumb($links);
$breadcrumb = drupal_get_breadcrumb();
if (count($breadcrumb) > 1) {
return '<div class="breadcrumb">'. implode(' &rsaquo; ', $breadcrumb) .'</div>';
}
}
name = Page Taxonomy Breadcrumbs
description = Adds custom breadcrumbs (based on taxonomy) to product Pages. Works in conjunction with theme template.php
package = SN Custom
core = 7.x
;files[] = sn_product_page.module
files[] = sn_product_page.tokens.inc
<?php
/**
* @file
* Token integration for the Pathauto module.
*/
/**
* Implements hook_token_info().
*/
function page_taxonomy_breadcrumbs_token_info() {
$info = array();
$info['tokens']['array']['join-term-path'] = array(
'name' => t('Joined taxonomy path'),
'description' => t('The array values each cleaned by Pathauto and then joined with the slash into a string that resembles an URL.'),
);
return $info;
}
/**
* Implements hook_tokens().
*/
function page_taxonomy_breadcrumb_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
if ($type == 'array' && !empty($data['array'])) {
$array = $data['array'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'join-term-path':
module_load_include('inc', 'pathauto');
$values = array();
foreach (element_children($array) as $key) {
$value = array_pop(explode('/', drupal_get_path_alias("taxonomy/term/$key")));
$values[] = pathauto_cleanstring($value);
}
$replacements[$original] = implode('/', $values);
break;
}
}
}
return $replacements;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment