View source
<?php
define('JUMP_MENU_DEFAULT_CHOOSE', '-- Choose --');
define('JUMP_MENU_DEFAULT_BLOCK_SETTINGS_SHOW_CURRENT', 0);
function jump_menu($menu, $parent, $btn = FALSE, $max_depth = 0, $choose = 'Select a destination', $current = FALSE) {
ctools_include('jump-menu');
$menu = menu_tree_all_data($menu);
foreach ($menu as $m) {
if ($m['link']['mlid'] == $parent) {
$menu = $m['below'];
break;
}
}
$depths = array(
'current' => 1,
'max' => $max_depth,
);
$targets = array();
_jump_menu_create_options($targets, $menu, $depths);
if (count($targets) == 0) {
return 'Jump menu contains no items!';
}
else {
$options = array();
if ($btn) {
$options['hide'] = FALSE;
$options['button'] = $btn;
}
else {
$options['hide'] = TRUE;
}
$options['choose'] = t($choose);
if ($current) {
$current_path = base_path() . request_path();
if (!empty($current_path)) {
$options['default_value'] = $current_path;
}
}
return drupal_render(drupal_get_form('ctools_jump_menu', $targets, $options));
}
}
function _jump_menu_create_options(&$t, &$m, &$d) {
foreach ($m as $item) {
if ($item['link']['hidden'] == 0) {
if ($d['current'] > 1) {
$title = ' ' . str_repeat('-', $d['current'] - 1) . ' ' . $item['link']['title'];
}
else {
$title = $item['link']['title'];
}
$classes = 'd-' . $d['current'];
$current_path = drupal_get_normal_path(request_path());
if ($item['link']['href'] == $current_path) {
$classes .= ' active';
}
if (module_exists('special_menu_items') && $item['link']['page_callback'] == 'drupal_not_found') {
$t[] = array(
'title' => t($title),
'#attributes' => array(
'class' => $classes,
),
);
}
else {
$url_options = array(
'query' => $item['link']['localized_options']['query'],
'fragment' => $item['link']['localized_options']['fragment'],
);
$t[] = array(
'value' => url($item['link']['href'], $url_options),
'title' => t($title),
'#attributes' => array(
'class' => $classes,
),
);
}
}
if ($item['below'] && ($d['max'] == 0 || $d['current'] < $d['max'])) {
$d['current']++;
_jump_menu_create_options($t, $item['below'], $d);
}
}
$d['current']--;
}
function _jump_menu_render_block($delta, $options = array()) {
$block_name = str_replace('jump_menu-', '', $delta);
static $menus;
if (!isset($menus)) {
$menus = menu_get_menus(TRUE);
}
$options['hide'] = isset($options['hide']) ? $options['hide'] : TRUE;
$settings = variable_get('jump_menu_block_settings_show_current', array());
$current = isset($settings[$delta]) ? $settings[$delta] : JUMP_MENU_DEFAULT_BLOCK_SETTINGS_SHOW_CURRENT;
if (substr($block_name, 0, 2) == 'm_') {
foreach ($menus as $k => $v) {
if (substr($k, 0, 20) == substr($block_name, 2)) {
$data['subject'] = check_plain($menus[$k]);
$options['choose'] = isset($options['choose']) ? $options['choose'] : check_plain($menus[$k]);
$data['content'] = jump_menu($k, 0, FALSE, 0, $options['choose'], $current);
}
}
}
elseif (substr($block_name, 0, 11) == 'local-tasks') {
$links = menu_local_tasks(0);
$links_secondary = menu_local_tasks(1);
$secondary = count($links_secondary['tabs']['output']) != 0 ? TRUE : FALSE;
if ($links['tabs']['count'] > 0) {
$targets = array();
foreach ($links['tabs']['output'] as $l) {
if ($l['#link']['access'] == TRUE) {
$classes = $l['#active'] ? 'active' : '';
$targets[] = array(
'value' => url($l['#link']['href']),
'title' => t($l['#link']['title']),
'#attributes' => array(
'class' => $classes,
),
);
if ($secondary && $links_secondary['tabs']['output'][0]['#link']['tab_parent_href'] == $l['#link']['href']) {
foreach ($links_secondary['tabs']['output'] as $sl) {
$classes = $l['#active'] ? 'active' : '';
$targets[] = array(
'value' => url($sl['#link']['href']),
'title' => '- ' . t($sl['#link']['title']),
'#attributes' => array(
'class' => $classes,
),
);
}
}
}
}
$options['choose'] = isset($options['choose']) ? $options['choose'] : t(JUMP_MENU_DEFAULT_CHOOSE);
if ($current) {
$current_path = base_path() . request_path();
if (!empty($current_path)) {
$options['default_value'] = $current_path;
}
}
$data['subject'] = t('Local Tasks');
$data['content'] = drupal_render(drupal_get_form('ctools_jump_menu', $targets, $options));
}
else {
$data = FALSE;
}
}
else {
$notice = t('Something is wrong with the Jump Menu module, please report.');
if (variable_get('error_level') > 0) {
drupal_set_message($notice);
}
else {
$message = 'Unable to render Jump Menu block. Likely due to a bad menu delta in the database.';
watchdog('jump_menu', $message, array(), WATCHDOG_ERROR);
drupal_set_message($notice);
}
$data = FALSE;
}
return $data;
}
function jump_menu_theme_registry_alter(&$theme_registry) {
$theme_registry['select']['function'] = 'jump_menu_select';
}
function jump_menu_select($variables) {
$element = $variables['element'];
element_set_attributes($element, array(
'id',
'name',
'size',
));
_form_set_class($element, array(
'form-select',
));
$output = '<select' . drupal_attributes($element['#attributes']) . '>';
if ($element['#attributes']['class'][0] == 'ctools-jump-menu-select') {
$output .= jump_menu_form_select_options($element);
}
else {
$output .= form_select_options($element);
}
$output .= '</select>';
return $output;
}
function jump_menu_form_select_options($element, $choices = NULL) {
if (!isset($choices)) {
$choices = $element['#options'];
}
$value_valid = isset($element['#value']) || array_key_exists('#value', $element);
$value_is_array = $value_valid && is_array($element['#value']);
$options = '';
foreach ($choices as $key => $choice) {
if (is_array($choice)) {
if (isset($choice['value'])) {
$opt_value = (string) $choice['value'];
}
else {
if (isset($choice['title'])) {
$options .= '<optgroup label="' . $choice['title'] . '"></optgroup>';
}
else {
$options .= '<optgroup label="' . $key . '">';
$options .= form_select_options($element, $choice);
$options .= '</optgroup>';
}
}
}
else {
$opt_value = $key;
$choice = array(
'title' => $choice,
);
}
if (isset($opt_value)) {
if (!isset($choice['#attributes'])) {
$choice['#attributes'] = array();
}
if ($value_valid && (!$value_is_array && (string) $element['#value'] === $opt_value || $value_is_array && in_array($opt_value, $element['#value']))) {
$selected = ' selected="selected"';
}
else {
$selected = '';
}
$options .= '<option value="' . check_plain($opt_value) . '"' . $selected . drupal_attributes($choice['#attributes']) . '>' . check_plain($choice['title']) . '</option>';
}
unset($opt_value);
}
return $options;
}
function jump_menu_form_block_admin_configure_alter(&$form, &$form_state, $form_id) {
if ($form['module']['#value'] == 'jump_menu') {
$delta = $form['delta']['#value'];
$settings = variable_get('jump_menu_block_settings_show_current', array());
$default = isset($settings[$delta]) ? $settings[$delta] : JUMP_MENU_DEFAULT_BLOCK_SETTINGS_SHOW_CURRENT;
$form['jump_menu_options'] = array(
'#type' => 'details',
'#title' => t('Jump Menu'),
'#weight' => 1,
'jump_menu_show_current' => array(
'#type' => 'checkbox',
'#title' => t('Display Curent Location'),
'#description' => t('Set the jump menu to the currently active page location.'),
'#default_value' => $default,
),
);
$form['#submit'][] = 'jump_menu_block_settings_submit';
}
}
function jump_menu_block_settings_submit($form, &$form_state) {
$settings = variable_get('jump_menu_block_settings_show_current', array());
$delta = $form_state['values']['delta'];
if ($form_state['values']['jump_menu_show_current'] == JUMP_MENU_DEFAULT_BLOCK_SETTINGS_SHOW_CURRENT) {
if (isset($settings[$delta])) {
unset($settings[$delta]);
}
}
else {
$settings[$delta] = $form_state['values']['jump_menu_show_current'];
}
variable_set('jump_menu_block_settings_show_current', $settings);
}