1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Provides a simple "jump menu".
|
6
|
*
|
7
|
* A jump menu is a select box and an optional 'go' button which can be removed
|
8
|
* if javascript is in use. Each item is keyed to the href that the button
|
9
|
* should go to. With javascript, the page is immediately redirected. Without
|
10
|
* javascript, the form is submitted and a drupal_goto() is given.
|
11
|
*
|
12
|
*/
|
13
|
|
14
|
/**
|
15
|
* Generate a jump menu form.
|
16
|
*
|
17
|
* This can either be used with drupal_get_form() or directly added to a
|
18
|
* form. The button provides its own submit handler so by default, other
|
19
|
* submit handlers will not be called.
|
20
|
*
|
21
|
* One note: Do not use #tree = TRUE or it will be unable to find the
|
22
|
* proper value.
|
23
|
*
|
24
|
* @code
|
25
|
* ctools_include('jump-menu');
|
26
|
* $output = drupal_get_form('ctools_jump_menu', $targets, $options);
|
27
|
* @endcode
|
28
|
*
|
29
|
* @param $select
|
30
|
* An array suitable for use as the #options. The keys will be the direct
|
31
|
* URLs that will be jumped to, so you absolutely must encode these using
|
32
|
* url() in order for them to work reliably.
|
33
|
*
|
34
|
* @param $options
|
35
|
* $options may be an array with the following options:
|
36
|
* - 'title': The text to display for the #title attribute.
|
37
|
* - 'description': The text to display for the #description attribute.
|
38
|
* - 'default_value': The text to display for the #default_value attribute.
|
39
|
* - 'hide': If TRUE the go button will be set to hide via javascript and
|
40
|
* will submit on change.
|
41
|
* - 'button': The text to display on the button.
|
42
|
* - 'image': If set, an image button will be used instead, and the image
|
43
|
* set to this.
|
44
|
* - 'inline': If set to TRUE (default) the display will be forced inline.
|
45
|
*/
|
46
|
function ctools_jump_menu($form, &$form_state, $select, $options = array()) {
|
47
|
$options += array(
|
48
|
'button' => t('Go'),
|
49
|
'choose' => t('- Choose -'),
|
50
|
'inline' => TRUE,
|
51
|
'hide' => TRUE,
|
52
|
);
|
53
|
|
54
|
ctools_add_js('jump-menu');
|
55
|
|
56
|
if (!empty($options['choose'])) {
|
57
|
$select = array('' => $options['choose']) + $select;
|
58
|
}
|
59
|
|
60
|
$form['jump'] = array(
|
61
|
'#type' => 'select',
|
62
|
'#options' => $select,
|
63
|
'#attributes' => array(
|
64
|
'class' => array('ctools-jump-menu-select'),
|
65
|
),
|
66
|
);
|
67
|
|
68
|
if (!empty($options['title'])) {
|
69
|
$form['jump']['#title'] = $options['title'];
|
70
|
}
|
71
|
|
72
|
if (!empty($options['description'])) {
|
73
|
$form['jump']['#description'] = $options['description'];
|
74
|
}
|
75
|
|
76
|
if (!empty($options['default_value'])) {
|
77
|
$form['jump']['#default_value'] = $options['default_value'];
|
78
|
}
|
79
|
|
80
|
if (isset($options['image'])) {
|
81
|
$form['go'] = array(
|
82
|
'#type' => 'image_button',
|
83
|
'#src' => $options['image'],
|
84
|
'#submit' => array('ctools_jump_menu_submit'),
|
85
|
'#attributes' => array(
|
86
|
'class' => array('ctools-jump-menu-button'),
|
87
|
),
|
88
|
);
|
89
|
}
|
90
|
else {
|
91
|
$form['go'] = array(
|
92
|
'#type' => 'submit',
|
93
|
'#value' => $options['button'],
|
94
|
'#submit' => array('ctools_jump_menu_submit'),
|
95
|
'#attributes' => array(
|
96
|
'class' => array('ctools-jump-menu-button'),
|
97
|
),
|
98
|
);
|
99
|
}
|
100
|
|
101
|
if ($options['inline']) {
|
102
|
$form['jump']['#prefix'] = '<div class="container-inline">';
|
103
|
$form['go']['#suffix'] = '</div>';
|
104
|
}
|
105
|
|
106
|
if ($options['hide']) {
|
107
|
$form['jump']['#attributes']['class'][] = 'ctools-jump-menu-change';
|
108
|
$form['go']['#attributes']['class'][] = 'ctools-jump-menu-hide';
|
109
|
}
|
110
|
|
111
|
return $form;
|
112
|
}
|
113
|
|
114
|
/**
|
115
|
* Submit handler for the jump menu.
|
116
|
*
|
117
|
* This is normally only invoked upon submit without javascript enabled.
|
118
|
*/
|
119
|
function ctools_jump_menu_submit($form, &$form_state) {
|
120
|
if ($form_state['values']['jump'] === '') {
|
121
|
// We have nothing to do when the user has not selected any value.
|
122
|
return;
|
123
|
}
|
124
|
|
125
|
// If the path we are redirecting to contains the string :: then treat the
|
126
|
// the string after the double colon as the path to redirect to.
|
127
|
// This allows duplicate paths to be used in jump menus for multiple options.
|
128
|
$redirect_array = explode("::", $form_state['values']['jump']);
|
129
|
|
130
|
if(isset($redirect_array[1]) && !empty($redirect_array[1])){
|
131
|
$redirect = $redirect_array[1];
|
132
|
}
|
133
|
else {
|
134
|
$redirect = $form_state['values']['jump'];
|
135
|
}
|
136
|
|
137
|
// If the path we are redirecting to starts with the base path (for example,
|
138
|
// "/somepath/node/1"), we need to strip the base path off before passing it
|
139
|
// to $form_state['redirect'].
|
140
|
$base_path = base_path();
|
141
|
if (strpos($redirect, $base_path) === 0) {
|
142
|
$redirect = substr($redirect, strlen($base_path));
|
143
|
}
|
144
|
|
145
|
// Parse the URL so that query strings and fragments are preserved in the
|
146
|
// redirect.
|
147
|
$redirect = drupal_parse_url($redirect);
|
148
|
$redirect['path'] = urldecode($redirect['path']);
|
149
|
$form_state['redirect'] = array($redirect['path'], $redirect);
|
150
|
}
|