View source
<?php
module_load_include('inc', 'custom_breadcrumbs', 'custom_breadcrumbs.admin');
function custom_breadcrumbsapi_cb_breadcrumb_info() {
$breadcrumb_type_info = array();
$breadcrumb_type_info['module'] = array(
'table' => 'custom_breadcrumbsapi',
'field' => 'module_page',
'type' => 'module',
'name_constructor' => '_custom_breadcrumbsapi_breadcrumb_name',
);
return $breadcrumb_type_info;
}
function _custom_breadcrumbsapi_breadcrumb_name($breadcrumb) {
if (isset($breadcrumb->module_page)) {
return $breadcrumb->module_page;
}
}
function custom_breadcrumbsapi_help($path, $arg) {
switch ($path) {
case 'admin/help#custom_breadcrumbsapi':
$output = '<p>' . t("Custom Breadcrumbsapi provides a simple api that allows custom breadcrumbs to be defined for module pages that use a theme template or a custom_breadcrumbsapi hook. For the latter, module developers need to provide a <em>moduleName</em>_custom_breadcrumbsapi() function that returns an array containing the names of the module pages for which custom breadcrumbs may be defined. Then, in the callback functions for each of those pages, the following lines needs to be inserted:") . '</p>';
$output .= '<p><strong>' . t("drupal_alter('breadcrumb', \$breadcrumb, '<em>module_page_name</em>');") . '</strong></p>';
$output .= t('within the function, preferably after defining $breadcrumb but before setting the breadcrumb.');
return $output;
}
}
function custom_breadcrumbsapi_menu() {
$items = array();
$items['admin/structure/custom_breadcrumbs/module/add'] = array(
'title' => 'Module Pages',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'custom_breadcrumbsapi_form',
'module',
),
'access arguments' => array(
'administer custom breadcrumbs',
),
'type' => MENU_LOCAL_TASK,
);
$items['admin/structure/custom_breadcrumbs/module/edit'] = array(
'title' => 'Edit custom breadcrumb for module page',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'custom_breadcrumbsapi_form',
'module',
),
'access arguments' => array(
'administer custom breadcrumbs',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function custom_breadcrumbsapi_breadcrumb_alter($breadcrumb, $modulepage, $objs = array()) {
global $language;
$languages = array(
'language' => $language->language,
'all' => '',
);
$breadcrumbs = custom_breadcrumbs_load_breadcrumbs('custom_breadcrumbsapi', NULL, array(
'module_page' => $modulepage,
), $languages);
if (!empty($breadcrumbs)) {
if ($breadcrumb = custom_breadcrumbs_select_breadcrumb($breadcrumbs, $objs)) {
custom_breadcrumbs_set_breadcrumb($breadcrumb, $objs);
$breadcrumb = drupal_get_breadcrumb();
}
}
}
function custom_breadcrumbsapi_form($form, &$form_state, $type) {
$form = array();
$breadcrumb = NULL;
$bid = arg(5);
if (isset($bid)) {
drupal_set_title(t('Edit Custom Breadcrumb for Module Page'));
$breadcrumbs = custom_breadcrumbs_load_breadcrumbs('custom_breadcrumbsapi', NULL, array(
'bid' => $bid,
));
$breadcrumb = array_pop($breadcrumbs);
}
else {
drupal_set_title(t('Add Custom Breadcrumb for Module Page'));
}
$options = array();
drupal_theme_initialize();
$hooks = theme_get_registry();
if (is_array($hooks)) {
foreach ($hooks as $name => $hook) {
if (isset($hook['template'])) {
$options[$name] = $hook['template'];
}
}
}
$modules = module_implements('custom_breadcrumbsapi');
foreach ($modules as $module) {
$func = $module . '_custom_breadcrumbsapi';
$names = $func();
foreach ($names as $name) {
$options[$name] = $name;
}
}
if (empty($options)) {
$form['notice'] = array(
'#markup' => t('No module pages using theme templates or currently implementing the custom breadcrumbs api. Read the <a href="@help">documentation</a> to learn how to do this.', array(
'@help' => url('admin/help/custom_breadcrumbsapi'),
)),
);
}
else {
$form['module_page'] = array(
'#type' => 'select',
'#title' => t('Module Page'),
'#required' => TRUE,
'#options' => $options,
'#description' => t('The module page that this custom breadcrumb trail will apply to.'),
'#default_value' => isset($breadcrumb->module_page) ? $breadcrumb->module_page : NULL,
'#weight' => -10,
);
$form['#module'] = 'custom_breadcrumbsapi';
$form['#infokey'] = 'module';
$form += custom_breadcrumbs_common_form_elements($bid, $breadcrumb);
$form['visibility_php']['#description'] = t('Determine whether this breadcrumb should be displayed by using a PHP snippet to return TRUE or FALSE. For module pages in the theme registry, you will have access to the %var array. Consult the individual template files for variable names that are used as the arguments of this array. Do not use opening and closing php tags.', array(
'%var' => '$variables',
));
$form['#submit'][] = 'custom_breadcrumbs_form_submit';
$form['#validate'][] = 'custom_breadcrumbs_form_validate';
}
return $form;
}
function custom_breadcrumbsapi_preprocess(&$variables, $hook) {
static $tried = array();
if (!isset($tried[$hook])) {
$tried[$hook] = TRUE;
global $language;
$languages = array(
'language' => $language->language,
'all' => '',
);
$breadcrumbs = custom_breadcrumbs_load_breadcrumbs('custom_breadcrumbsapi', NULL, array(
'module_page' => $hook,
), $languages);
if (!empty($breadcrumbs)) {
$objs = isset($variables) && is_array($variables) ? $variables : array();
if ($breadcrumb = custom_breadcrumbs_select_breadcrumb($breadcrumbs, $objs)) {
custom_breadcrumbs_set_breadcrumb($breadcrumb, $objs);
$variables['breadcrumb'] = theme('breadcrumb', array(
'breadcrumb' => drupal_get_breadcrumb(),
));
}
}
}
}