Issue #1224340: Allow multiple jump menu to same destination.
[project/ctools.git] / includes / jump-menu.inc
CommitLineData
dfd5d58a 1<?php
dfd5d58a
SB
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 */
0a5510f6 46function ctools_jump_menu($form, &$form_state, $select, $options = array()) {
dfd5d58a
SB
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(
a40482ac 64 'class' => array('ctools-jump-menu-select'),
dfd5d58a
SB
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(
a40482ac 86 'class' => array('ctools-jump-menu-button'),
dfd5d58a
SB
87 ),
88 );
89 }
90 else {
91 $form['go'] = array(
92 '#type' => 'submit',
93 '#value' => $options['button'],
94 '#attributes' => array(
a40482ac 95 'class' => array('ctools-jump-menu-button'),
dfd5d58a
SB
96 ),
97 );
98 }
99
100 if ($options['inline']) {
101 $form['jump']['#prefix'] = '<div class="container-inline">';
102 $form['go']['#suffix'] = '</div>';
103 }
104
105 if ($options['hide']) {
a40482ac
SB
106 $form['jump']['#attributes']['class'][] = 'ctools-jump-menu-change';
107 $form['go']['#attributes']['class'][] = 'ctools-jump-menu-hide';
dfd5d58a
SB
108 }
109
110 return $form;
111}
112
113/**
114 * Submit handler for the jump menu.
115 *
116 * This is normally only invoked upon submit without javascript enabled.
117 */
118function ctools_jump_menu_submit($form, &$form_state) {
f2177195
EM
119 // If the path we are redirecting to contains the string :: then treat the
120 // the string after the double colon as the path to redirect to.
121 // This allows duplicate paths to be used in jump menus for multiple options.
122 $redirect_array = explode("::", $form_state['values']['jump']);
123
124 if(isset($redirect_array[1]) && !empty($redirect_array[1])){
125 $redirect = $redirect_array[1];
126 }
127 else {
128 $redirect = $form_state['values']['jump'];
129 }
3ec36b01
EM
130
131 // If the path we are redirecting to starts with the base path (for example,
132 // "/somepath/node/1"), we need to strip the base path off before passing it
133 // to $form_state['redirect'].
134 $base_path = base_path();
135 if (strpos($redirect, $base_path) === 0) {
136 $redirect = substr($redirect, strlen($base_path));
137 }
138
139 // Parse the URL so that query strings and fragments are preserved in the
140 // redirect.
141 $redirect = drupal_parse_url($redirect);
142 $form_state['redirect'] = array($redirect['path'], $redirect);
dfd5d58a 143}