View source
<?php
module_load_include('inc', 'custom_breadcrumbs', 'custom_breadcrumbs.admin');
module_load_include('inc', 'custom_breadcrumbs', 'custom_breadcrumbs_common');
function custom_breadcrumbs_paths_cb_breadcrumb_info() {
$breadcrumb_type_info = array();
$breadcrumb_type_info['paths'] = array(
'table' => 'custom_breadcrumbs_paths',
'field' => 'specific_path',
'type' => 'path',
'name_constructor' => '_custom_breadcrumbs_paths_breadcrumb_name',
);
return $breadcrumb_type_info;
}
function _custom_breadcrumbs_paths_breadcrumb_name($breadcrumb) {
if (isset($breadcrumb->specific_path)) {
return $breadcrumb->specific_path;
}
}
function custom_breadcrumbs_paths_menu() {
$items = array();
$items['admin/structure/custom_breadcrumbs/path/add'] = array(
'title' => 'Path',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'custom_breadcrumbs_paths_form',
'path',
),
'access arguments' => array(
'administer custom breadcrumbs',
),
'type' => MENU_LOCAL_TASK,
'weight' => 5,
);
$items['admin/structure/custom_breadcrumbs/path/edit'] = array(
'title' => 'Edit custom breadcrumb for path',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'custom_breadcrumbs_paths_form',
'path',
),
'access arguments' => array(
'administer custom breadcrumbs',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function custom_breadcrumbs_paths_preprocess_page(&$variables) {
if (!custom_breadcrumbs_exclude_path()) {
$objs = isset($variables) && is_array($variables) ? $variables : array();
if (_custom_breadcrumbs_paths_set_breadcrumb($objs)) {
$variables['breadcrumb'] = theme('breadcrumb', array(
'breadcrumb' => drupal_get_breadcrumb(),
));
}
}
}
function custom_breadcrumbs_paths_node_view($node, $build_mode) {
if ($build_mode == 'full') {
_custom_breadcrumbs_paths_set_breadcrumb(array(
'node' => $node,
), 3);
}
}
function custom_breadcrumbs_paths_views_pre_render(&$view) {
$curpath = drupal_get_normal_path($_GET['q']);
$viewpage = FALSE;
foreach ($view->display as $display) {
if (!_custom_breadcrumbs_allowed_display($display)) {
continue;
}
$viewpath = _custom_breadcrumbs_construct_view_path($display);
$viewpage = $viewpage || _custom_breadcrumbs_match_path($curpath, $viewpath);
}
if ($viewpage) {
_custom_breadcrumbs_paths_set_breadcrumb(array(
'view' => $view,
), 2);
}
}
function _custom_breadcrumbs_paths_set_breadcrumb(array $objs = array(), $priority = 1) {
static $success;
if (!isset($success) || $priority > $success) {
$matchpath = variable_get('custom_breadcrumbs_paths_allow_wildcards', FALSE);
$breadcrumbs = _custom_breadcrumbs_paths_get_breadcrumbs($matchpath);
if (!empty($breadcrumbs)) {
foreach ($breadcrumbs as $id => $breadcrumb) {
if (!$matchpath || _custom_breadcrumbs_paths_page_match($breadcrumb)) {
if (custom_breadcrumbs_is_visible($breadcrumb, $objs)) {
if ($matchpath) {
if (($pos = strrpos($breadcrumb->specific_path, '*')) !== FALSE) {
if (!isset($max) || isset($max) && $pos > $max) {
$max = $pos;
$max_id = $id;
}
}
else {
$max_id = $id;
break;
}
}
else {
$max_id = $id;
break;
}
}
}
}
if (isset($max_id)) {
custom_breadcrumbs_set_breadcrumb($breadcrumbs[$max_id], $objs);
$success = $priority;
return TRUE;
}
}
}
return FALSE;
}
function _custom_breadcrumbs_paths_get_breadcrumbs($matchpath = FALSE, $path = NULL) {
$request = request_path();
if (isset($request) || !is_null($path)) {
global $language;
$languages = array(
'language' => $language->language,
'all' => '',
);
$param = array();
if (!$matchpath) {
$prefix = $language->prefix . '\\/';
$path = is_null($path) ? preg_replace('/^' . $prefix . '/', '', $request) : $path;
$param = array(
'specific_path' => $path,
);
}
$breadcrumbs = custom_breadcrumbs_load_breadcrumbs('custom_breadcrumbs_paths', NULL, $param, $languages);
return $breadcrumbs;
}
}
function _custom_breadcrumbs_paths_page_match($breadcrumb) {
$page_match = FALSE;
$request = request_path();
if (isset($request)) {
if (isset($breadcrumb->language) && $breadcrumb->language != '') {
$path = $breadcrumb->language . '/' . $breadcrumb->specific_path;
$page_match = _custom_breadcrumbs_match_path($request, $path);
}
else {
global $language;
$path = $language->language . '/' . $breadcrumb->specific_path;
$page_match = _custom_breadcrumbs_match_path($request, $path);
}
if (!$page_match) {
$page_match = _custom_breadcrumbs_match_path($request, $breadcrumb->specific_path);
}
}
return $page_match;
}
function custom_breadcrumbs_paths_cb_node_form_table($node) {
if (isset($node->nid)) {
$alias = !empty($node->path['alias']) ? $node->path['alias'] : drupal_get_path_alias("node/" . $node->nid);
$matchpath = variable_get('custom_breadcrumbs_paths_allow_wildcards', FALSE);
$param = $matchpath ? array() : array(
'specific_path' => $alias,
);
$breadcrumbs = custom_breadcrumbs_load_breadcrumbs('custom_breadcrumbs_paths', NULL, $param);
foreach ($breadcrumbs as $key => $breadcrumb) {
if (!_custom_breadcrumbs_match_path($alias, $breadcrumb->specific_path)) {
unset($breadcrumbs[$key]);
}
}
return $breadcrumbs;
}
}
function custom_breadcrumbs_paths_form($form, &$form_state, $type) {
$bid = arg(5);
$breadcrumb = NULL;
if (isset($bid)) {
drupal_set_title(t('Edit Custom Breadcrumb for Path'));
$path_breadcrumbs = custom_breadcrumbs_load_breadcrumbs('custom_breadcrumbs_paths', NULL, array(
'bid' => $bid,
));
$breadcrumb = array_pop($path_breadcrumbs);
}
else {
drupal_set_title(t('Add Custom Breadcrumb for Path'));
}
$description = t('The Drupal path that this custom breadcrumb trail will apply to.');
if (variable_get('custom_breadcrumbs_paths_allow_wildcards', FALSE)) {
$description .= ' ' . t("The '*' character can be used as a wildcard to set a custom breadcrumb for all matching paths. For example, foo/bar* could be used to match every page with a path beginning with foo/bar. Do not include language prefixes in the path. This will be handled automatically according to the selected language.");
}
$form['specific_path'] = array(
'#type' => 'textfield',
'#title' => t('Specific Path'),
'#required' => TRUE,
'#description' => $description,
'#default_value' => isset($breadcrumb->specific_path) ? $breadcrumb->specific_path : NULL,
'#weight' => -10,
);
$form += custom_breadcrumbs_common_form_elements($bid, $breadcrumb);
$form['#module'] = 'custom_breadcrumbs_paths';
$form['#infokey'] = 'paths';
$form['#submit'][] = 'custom_breadcrumbs_form_submit';
$form['#validate'][] = 'custom_breadcrumbs_form_validate';
return $form;
}
function custom_breadcrumbs_paths_form_custom_breadcrumbs_admin_settings_alter(&$form, $form_state, $form_id) {
$form['adv_settings']['custom_breadcrumbs_paths_allow_wildcards'] = array(
'#type' => 'checkbox',
'#title' => t('Use wildcard pattern matching in paths'),
'#default_value' => variable_get('custom_breadcrumbs_paths_allow_wildcards', FALSE),
'#description' => t("If checked, the '*' character can be used as a wildcard to set a custom breadcrumb for all matching paths. For example, foo/bar/* could be used to match every page with a path beginning with foo/bar."),
'#weight' => -20,
);
}