| 1 |
<?php |
<?php |
| 2 |
// $Id: custom_breadcrumbs.module,v 1.8 2008/01/27 09:02:31 eaton Exp $ |
// $Id$ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* Implementation of hook_menu(). |
* @file |
| 6 |
|
* Provide custom breadcrumbs for node-type pages. |
| 7 |
|
*/ |
| 8 |
|
|
| 9 |
|
/** |
| 10 |
|
* Implement hook_menu(). |
| 11 |
*/ |
*/ |
| 12 |
function custom_breadcrumbs_menu() { |
function custom_breadcrumbs_menu() { |
| 13 |
$items = array(); |
$items = array(); |
| 44 |
return $items; |
return $items; |
| 45 |
} |
} |
| 46 |
|
|
| 47 |
function custom_breadcrumbs_perm() { |
/** |
| 48 |
return array('administer custom breadcrumbs', 'use php in custom breadcrumbs'); |
* 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 |
function custom_breadcrumbs_nodeapi($node, $op, $teaser, $page) { |
/** |
| 61 |
if ($op == 'view' && !$teaser && $page) { |
* Implement hook_theme(). |
| 62 |
if ($breadcrumb = _custom_breadcrumbs_load_for_type($node)) { |
*/ |
| 63 |
$titles = explode("\n", $breadcrumb->titles); |
function custom_breadcrumbs_theme() { |
| 64 |
$paths = explode("\n", $breadcrumb->paths); |
return array('custom_breadcrumbs_help_identifiers' => array('arguments' => array())); |
| 65 |
|
} |
| 66 |
$titles = module_exists('token') ? token_replace($titles, 'node', $node) : $titles; |
|
| 67 |
$paths = module_exists('token') ? token_replace($paths, 'node', $node) : $paths; |
/** |
| 68 |
|
* Implement hook_node_view(). |
| 69 |
$trail = array(l(t('Home'), '')); |
*/ |
| 70 |
for ($i = 0; $i < count($titles); $i++) { |
function custom_breadcrumbs_node_view($node, $build_mode) { |
| 71 |
// Skip empty titles |
if ($build_mode == 'full' && ($breadcrumb = _custom_breadcrumbs_load_for_type($node))) { |
| 72 |
if ($title = trim($titles[$i])) { |
$titles = preg_split("/[\n]+/", $breadcrumb->titles); |
| 73 |
// Output plaintext instead of a link if there is a title without a path. |
$paths = preg_split("/[\n]+/", $breadcrumb->paths); |
| 74 |
$path = trim($paths[$i]); |
|
| 75 |
if (strlen($path) > 0 && $path != '<none>') { |
$trail = array(l(t('Home'), '<front>')); |
| 76 |
$trail[] = l($title, trim($paths[$i])); |
for ($i = 0; $i < count($titles); $i++) { |
| 77 |
} |
$data = array('node' =>$node); |
| 78 |
else { |
$title = token_replace(trim($titles[$i]), $data); |
| 79 |
$trail[] = check_plain($title); |
$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 |
} |
} |
|
drupal_set_breadcrumb($trail); |
|
| 84 |
} |
} |
| 85 |
|
drupal_set_breadcrumb($trail); |
| 86 |
} |
} |
|
|
|
| 87 |
} |
} |
| 88 |
|
|
| 89 |
function _custom_breadcrumbs_load_breadcrumb($bid) { |
function _custom_breadcrumbs_load_breadcrumb($bid) { |
| 90 |
$sql = 'SELECT * FROM {custom_breadcrumb} WHERE bid = %d'; |
$result = db_query('SELECT * FROM {custom_breadcrumb} WHERE bid = :bid', array(':bid' => $bid)); |
| 91 |
$result = db_query($sql, $bid); |
$breadcrumb = $result->fetchObject(); |
|
$breadcrumb = db_fetch_object($result); |
|
| 92 |
return $breadcrumb; |
return $breadcrumb; |
| 93 |
} |
} |
| 94 |
|
|
| 95 |
function _custom_breadcrumbs_load_for_type($node) { |
function _custom_breadcrumbs_load_for_type($node) { |
| 96 |
$sql = "SELECT * FROM {custom_breadcrumb} WHERE node_type = '%s'"; |
$result = db_query('SELECT * FROM {custom_breadcrumb} WHERE node_type = :node_type', array(':node_type' => $node->type)); |
| 97 |
$result = db_query($sql, $node->type); |
while ($breadcrumb = $result->fetchObject()) { |
|
while ($breadcrumb = db_fetch_object($result)) { |
|
| 98 |
if (!empty($breadcrumb->visibility_php)) { |
if (!empty($breadcrumb->visibility_php)) { |
| 99 |
// Use PHP code to check the visibility. |
// Use PHP code to check the visibility. |
| 100 |
ob_start(); |
ob_start(); |
| 101 |
$result = eval(trim($breadcrumb->visibility_php)); |
$visibility = eval(trim($breadcrumb->visibility_php)); |
| 102 |
ob_end_clean(); |
ob_end_clean(); |
| 103 |
if ($result == TRUE) { |
if ($visibility == TRUE) { |
| 104 |
return $breadcrumb; |
return $breadcrumb; |
| 105 |
} |
} |
| 106 |
} |
} |
| 113 |
function _custom_breadcrumbs_load_all_breadcrumbs($refresh = FALSE) { |
function _custom_breadcrumbs_load_all_breadcrumbs($refresh = FALSE) { |
| 114 |
static $breadcrumbs; |
static $breadcrumbs; |
| 115 |
if ($refresh || !isset($breadcrumbs)) { |
if ($refresh || !isset($breadcrumbs)) { |
| 116 |
$sql = 'SELECT * FROM {custom_breadcrumb}'; |
$result = db_query('SELECT * FROM {custom_breadcrumb}'); |
| 117 |
$result = db_query($sql); |
$breadcrumbs = $result->fetchAll(); |
|
|
|
|
$breadcrumbs = array(); |
|
|
while ($breadcrumb = db_fetch_object($result)) { |
|
|
$breadcrumbs[] = $breadcrumb; |
|
|
} |
|
| 118 |
} |
} |
| 119 |
return $breadcrumbs; |
return $breadcrumbs; |
| 120 |
} |
} |
| 136 |
} |
} |
| 137 |
|
|
| 138 |
function _custom_breadcrumbs_delete_breadcrumb($bid) { |
function _custom_breadcrumbs_delete_breadcrumb($bid) { |
| 139 |
$sql = 'DELETE FROM {custom_breadcrumb} WHERE bid = %d'; |
db_delete('custom_breadcrumb')->condition('bid', $bid)->execute(); |
| 140 |
db_query($sql, $bid); |
} |
| 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 |
} |
} |