'Custom breadcrumbs', 'description' => 'Add custom breadcrumb trails for content types.', 'page callback' => 'custom_breadcrumbs_page', 'access arguments' => array('administer custom breadcrumbs'), 'file' => 'custom_breadcrumbs.admin.inc', ); $items['admin/structure/custom_breadcrumbs/add'] = array( 'title' => 'Add custom breadcrumb', 'type' => MENU_CALLBACK, 'page callback' => 'drupal_get_form', 'page arguments' => array('custom_breadcrumbs_form'), 'access arguments' => array('administer custom breadcrumbs'), 'file' => 'custom_breadcrumbs.admin.inc', ); $items['admin/structure/custom_breadcrumbs/edit'] = array( 'title' => 'Edit custom breadcrumb', 'type' => MENU_CALLBACK, 'page callback' => 'drupal_get_form', 'page arguments' => array('custom_breadcrumbs_form'), 'access callback' => 'user_access', 'access arguments' => array('administer custom breadcrumbs'), 'file' => 'custom_breadcrumbs.admin.inc', ); return $items; } /** * Implement hook_permission(). */ function custom_breadcrumbs_permission() { return array('administer custom breadcrumbs' => array( 'title' => 'Administer Custom Breadcrumbs', 'description' => 'Configure the Custom Breadcrumbs Modules'), 'use php in custom breadcrumbs' => array( 'title' => 'Use PHP in Custom Breadcrumbs', 'description' => 'Control Breadcrumb Visibility with PHP code'), ); } /** * Implements hook_theme(). */ function custom_breadcrumbs_theme() { return array('custom_breadcrumbs_help_identifiers' => array('arguments' => array())); } /** * Implements hook_node_view(). */ function custom_breadcrumbs_node_view($node, $build_mode) { if ($build_mode == 'full' && ($breadcrumb = _custom_breadcrumbs_load_for_type($node))) { $titles = preg_split("/[\n]+/", $breadcrumb->titles); $paths = preg_split("/[\n]+/", $breadcrumb->paths); $trail = array(l(t('Home'), '')); for ($i = 0; $i < count($titles); $i++) { $data = array('node' => $node); $title = token_replace(trim($titles[$i]), $data); $path = token_replace(trim($paths[$i]), $data); if (($title != '') && ($title != '')) { // Create breadcrumb only if there is a title. $trail[] = _custom_breadcrumbs_create_crumb($title, $path); } } drupal_set_breadcrumb($trail); } } function _custom_breadcrumbs_load_breadcrumb($bid) { $result = db_query('SELECT * FROM {custom_breadcrumb} WHERE bid = :bid', array(':bid' => $bid)); $breadcrumb = $result->fetchObject(); return $breadcrumb; } function _custom_breadcrumbs_load_for_type($node) { $result = db_query('SELECT * FROM {custom_breadcrumb} WHERE node_type = :node_type', array(':node_type' => $node->type)); while ($breadcrumb = $result->fetchObject()) { if (!empty($breadcrumb->visibility_php)) { // Use PHP code to check the visibility. ob_start(); $visibility = eval(trim($breadcrumb->visibility_php)); ob_end_clean(); if ($visibility == TRUE) { return $breadcrumb; } } else { return $breadcrumb; } } } function _custom_breadcrumbs_load_all_breadcrumbs($refresh = FALSE) { static $breadcrumbs; if ($refresh || !isset($breadcrumbs)) { $result = db_query('SELECT * FROM {custom_breadcrumb}'); $breadcrumbs = $result->fetchAll(); } return $breadcrumbs; } function custom_breadcrumbs_save_breadcrumb($breadcrumb = NULL) { if (is_array($breadcrumb->paths)) { $breadcrumb->paths = implode("\n", $breadcrumb->paths); } if (is_array($breadcrumb->titles)) { $breadcrumb->titles = implode("\n", $breadcrumb->titles); } if (isset($breadcrumb->bid)) { drupal_write_record('custom_breadcrumb', $breadcrumb, 'bid'); } else { drupal_write_record('custom_breadcrumb', $breadcrumb); } } function _custom_breadcrumbs_delete_breadcrumb($bid) { db_delete('custom_breadcrumb')->condition('bid', $bid)->execute(); } /** * Private function for custom breadcrumb to create a crumb item * * @param $title * The human readable title to be rendered by the browser * @param $original_path * The desired URI and/or special identifier */ function _custom_breadcrumbs_create_crumb($title, $original_path) { // Collapse double slashes to one. $original_path = preg_replace('/\/+/', '/', $original_path); // Removing leading and trailing slashes. $original_path = preg_replace('/^\/|\/+$/', '', $original_path); list($identifier, $path) = explode("|", $original_path, 2); if ($path) { switch ($identifier) { case '': if (module_exists('pathauto')) $crumb = l($title, pathauto_cleanstring($path, FALSE)); break; default: $crumb = l($title, $original_path); } } else { // This may be just be a single identifier. switch ($identifier) { case '': $crumb = check_plain($title); break; default: $crumb = l($title, $original_path); } } return $crumb; } /** * Builds a table of identifiers and their behaviors */ function theme_custom_breadcrumbs_help_identifiers() { $headers = array(t('Identifier'), t('Behaviour')); $rows = array(); // identifier $row = array(); $row[] = check_plain(''); $row[] = 'This will result in a plain text crumb. This identifier should not be used with the pipe (|) symbol.'; $rows[] = $row; // identifier if (module_exists('pathauto')) { $row = array(); $row[] = check_plain(''); $row[] = 'Cleans the given path using your pathauto replacement rules.'; $rows[] = $row; } return theme('table', array('header' => $headers, 'rows' => $rows)); }