| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Provide custom breadcrumbs for node-type pages.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implement hook_menu().
|
| 11 |
*/
|
| 12 |
function custom_breadcrumbs_menu() {
|
| 13 |
$items = array();
|
| 14 |
|
| 15 |
$items['admin/build/custom_breadcrumbs'] = array(
|
| 16 |
'title' => 'Custom breadcrumbs',
|
| 17 |
'description' => 'Add custom breadcrumb trails for content types.',
|
| 18 |
'page callback' => 'custom_breadcrumbs_page',
|
| 19 |
'access callback' => 'user_access',
|
| 20 |
'access arguments' => array('administer custom breadcrumbs'),
|
| 21 |
'file' => 'custom_breadcrumbs.admin.inc',
|
| 22 |
);
|
| 23 |
|
| 24 |
$items['admin/build/custom_breadcrumbs/add'] = array(
|
| 25 |
'title' => 'Add custom breadcrumb',
|
| 26 |
'type' => MENU_CALLBACK,
|
| 27 |
'page callback' => 'drupal_get_form',
|
| 28 |
'page arguments' => array('custom_breadcrumbs_form'),
|
| 29 |
'access callback' => 'user_access',
|
| 30 |
'access arguments' => array('administer custom breadcrumbs'),
|
| 31 |
'file' => 'custom_breadcrumbs.admin.inc',
|
| 32 |
);
|
| 33 |
|
| 34 |
$items['admin/build/custom_breadcrumbs/edit'] = array(
|
| 35 |
'title' => 'Edit custom breadcrumb',
|
| 36 |
'type' => MENU_CALLBACK,
|
| 37 |
'page callback' => 'drupal_get_form',
|
| 38 |
'page arguments' => array('custom_breadcrumbs_form'),
|
| 39 |
'access callback' => 'user_access',
|
| 40 |
'access arguments' => array('administer custom breadcrumbs'),
|
| 41 |
'file' => 'custom_breadcrumbs.admin.inc',
|
| 42 |
);
|
| 43 |
|
| 44 |
return $items;
|
| 45 |
}
|
| 46 |
|
| 47 |
/**
|
| 48 |
* Implement hook_permission().
|
| 49 |
*/
|
| 50 |
function custom_breadcrumbs_permission() {
|
| 51 |
return array('administer custom breadcrumbs' => array(
|
| 52 |
'title' => 'Administer Custom Breadcrumbs',
|
| 53 |
'description' => 'Configure the Custom Breadcrumbs Modules'),
|
| 54 |
'use php in custom breadcrumbs'=> array(
|
| 55 |
'title' => 'Use PHP in Custom Breadcrumbs',
|
| 56 |
'description' => 'Control Breadcrumb Visibility with PHP code'),
|
| 57 |
);
|
| 58 |
}
|
| 59 |
|
| 60 |
/**
|
| 61 |
* Implement hook_theme().
|
| 62 |
*/
|
| 63 |
function custom_breadcrumbs_theme() {
|
| 64 |
return array('custom_breadcrumbs_help_identifiers' => array('arguments' => array()));
|
| 65 |
}
|
| 66 |
|
| 67 |
/**
|
| 68 |
* Implement hook_node_view().
|
| 69 |
*/
|
| 70 |
function custom_breadcrumbs_node_view($node, $build_mode) {
|
| 71 |
if ($build_mode == 'full' && ($breadcrumb = _custom_breadcrumbs_load_for_type($node))) {
|
| 72 |
$titles = preg_split("/[\n]+/", $breadcrumb->titles);
|
| 73 |
$paths = preg_split("/[\n]+/", $breadcrumb->paths);
|
| 74 |
|
| 75 |
$trail = array(l(t('Home'), '<front>'));
|
| 76 |
for ($i = 0; $i < count($titles); $i++) {
|
| 77 |
$data = array('node' =>$node);
|
| 78 |
$title = token_replace(trim($titles[$i]), $data);
|
| 79 |
$path = token_replace(trim($paths[$i]), $data);
|
| 80 |
if (($title != '') && ($title != '<none>')) {
|
| 81 |
// Create breadcrumb only if there is a title.
|
| 82 |
$trail[] = _custom_breadcrumbs_create_crumb($title, $path);
|
| 83 |
}
|
| 84 |
}
|
| 85 |
drupal_set_breadcrumb($trail);
|
| 86 |
}
|
| 87 |
}
|
| 88 |
|
| 89 |
function _custom_breadcrumbs_load_breadcrumb($bid) {
|
| 90 |
$result = db_query('SELECT * FROM {custom_breadcrumb} WHERE bid = :bid', array(':bid' => $bid));
|
| 91 |
$breadcrumb = $result->fetchObject();
|
| 92 |
return $breadcrumb;
|
| 93 |
}
|
| 94 |
|
| 95 |
function _custom_breadcrumbs_load_for_type($node) {
|
| 96 |
$result = db_query('SELECT * FROM {custom_breadcrumb} WHERE node_type = :node_type', array(':node_type' => $node->type));
|
| 97 |
while ($breadcrumb = $result->fetchObject()) {
|
| 98 |
if (!empty($breadcrumb->visibility_php)) {
|
| 99 |
// Use PHP code to check the visibility.
|
| 100 |
ob_start();
|
| 101 |
$visibility = eval(trim($breadcrumb->visibility_php));
|
| 102 |
ob_end_clean();
|
| 103 |
if ($visibility == TRUE) {
|
| 104 |
return $breadcrumb;
|
| 105 |
}
|
| 106 |
}
|
| 107 |
else {
|
| 108 |
return $breadcrumb;
|
| 109 |
}
|
| 110 |
}
|
| 111 |
}
|
| 112 |
|
| 113 |
function _custom_breadcrumbs_load_all_breadcrumbs($refresh = FALSE) {
|
| 114 |
static $breadcrumbs;
|
| 115 |
if ($refresh || !isset($breadcrumbs)) {
|
| 116 |
$result = db_query('SELECT * FROM {custom_breadcrumb}');
|
| 117 |
$breadcrumbs = $result->fetchAll();
|
| 118 |
}
|
| 119 |
return $breadcrumbs;
|
| 120 |
}
|
| 121 |
|
| 122 |
function custom_breadcrumbs_save_breadcrumb($breadcrumb = NULL) {
|
| 123 |
if (is_array($breadcrumb->paths)) {
|
| 124 |
$breadcrumb->paths = implode("\n", $breadcrumb->paths);
|
| 125 |
}
|
| 126 |
if (is_array($breadcrumb->titles)) {
|
| 127 |
$breadcrumb->titles = implode("\n", $breadcrumb->titles);
|
| 128 |
}
|
| 129 |
|
| 130 |
if (isset($breadcrumb->bid)) {
|
| 131 |
drupal_write_record('custom_breadcrumb', $breadcrumb, 'bid');
|
| 132 |
}
|
| 133 |
else {
|
| 134 |
drupal_write_record('custom_breadcrumb', $breadcrumb);
|
| 135 |
}
|
| 136 |
}
|
| 137 |
|
| 138 |
function _custom_breadcrumbs_delete_breadcrumb($bid) {
|
| 139 |
db_delete('custom_breadcrumb')->condition('bid', $bid)->execute();
|
| 140 |
}
|
| 141 |
/**
|
| 142 |
* Private function for custom breadcrumb to create a crumb item
|
| 143 |
*
|
| 144 |
* @param $title
|
| 145 |
* The human readable title to be rendered by the browser
|
| 146 |
* @param $original_path
|
| 147 |
* The desired URI and/or special identifier
|
| 148 |
*/
|
| 149 |
function _custom_breadcrumbs_create_crumb($title, $original_path) {
|
| 150 |
// Collapse double slashes to one.
|
| 151 |
$original_path = preg_replace('/\/+/', '/', $original_path);
|
| 152 |
// Removing leading and trailing slashes.
|
| 153 |
$original_path = preg_replace('/^\/|\/+$/', '', $original_path);
|
| 154 |
list($identifier, $path) = explode("|", $original_path, 2);
|
| 155 |
|
| 156 |
if ($path) {
|
| 157 |
switch ($identifier) {
|
| 158 |
case '<pathauto>':
|
| 159 |
if (module_exists('pathauto'))
|
| 160 |
$crumb = l($title, pathauto_cleanstring($path, FALSE));
|
| 161 |
break;
|
| 162 |
default:
|
| 163 |
$crumb = l($title, $original_path);
|
| 164 |
}
|
| 165 |
}
|
| 166 |
else {
|
| 167 |
// This may be just be a single identifier.
|
| 168 |
switch ($identifier) {
|
| 169 |
case '<none>':
|
| 170 |
$crumb = $title;
|
| 171 |
break;
|
| 172 |
default:
|
| 173 |
$crumb = l($title, $original_path);
|
| 174 |
}
|
| 175 |
}
|
| 176 |
|
| 177 |
return $crumb;
|
| 178 |
}
|
| 179 |
|
| 180 |
/**
|
| 181 |
* Builds a table of identifiers and their behaviors
|
| 182 |
*/
|
| 183 |
function theme_custom_breadcrumbs_help_identifiers() {
|
| 184 |
$headers = array(t('Identifier'), t('Behaviour'));
|
| 185 |
$rows = array();
|
| 186 |
|
| 187 |
// <none> identifier
|
| 188 |
$row = array();
|
| 189 |
$row[] = check_plain('<none>');
|
| 190 |
$row[] = 'This will result in a plain text crumb. This identifier should not be used with the pipe (|) symbol.';
|
| 191 |
$rows[] = $row;
|
| 192 |
|
| 193 |
// <pathauto> identifier
|
| 194 |
if (module_exists('pathauto')) {
|
| 195 |
$row = array();
|
| 196 |
$row[] = check_plain('<pathauto>');
|
| 197 |
$row[] = 'Cleans the given path using your pathauto replacement rules.';
|
| 198 |
$rows[] = $row;
|
| 199 |
}
|
| 200 |
|
| 201 |
return theme('table', $headers, $rows);
|
| 202 |
}
|