Projet

Général

Profil

Paste
Télécharger (4,6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / includes / jump-menu.inc @ 7e72b748

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
 * Generate a jump menu form.
15
 *
16
 * This can either be used with drupal_get_form() or directly added to a
17
 * form. The button provides its own submit handler so by default, other
18
 * submit handlers will not be called.
19
 *
20
 * One note: Do not use #tree = TRUE or it will be unable to find the
21
 * proper value.
22
 *
23
 * @code
24
 * ctools_include('jump-menu');
25
 * $output = drupal_get_form('ctools_jump_menu', $targets, $options);
26
 * @endcode
27
 *
28
 * @param $select
29
 *   An array suitable for use as the #options. The keys will be the direct
30
 *   URLs that will be jumped to, so you absolutely must encode these using
31
 *   url() in order for them to work reliably.
32
 *
33
 * @param $options
34
 *   $options may be an array with the following options:
35
 *   - 'title': The text to display for the #title attribute.
36
 *   - 'description': The text to display for the #description attribute.
37
 *   - 'default_value': The text to display for the #default_value attribute.
38
 *   - 'hide': If TRUE the go button will be set to hide via javascript and
39
 *     will submit on change.
40
 *   - 'button': The text to display on the button.
41
 *   - 'image': If set, an image button will be used instead, and the image
42
 *     set to this.
43
 *   - 'inline': If set to TRUE (default) the display will be forced inline.
44
 */
45
function ctools_jump_menu($form, &$form_state, $select, $options = array()) {
46
  $options += array(
47
    'button' => t('Go'),
48
    'choose' => t('- Choose -'),
49
    'inline' => TRUE,
50
    'hide' => TRUE,
51
  );
52

    
53
  $form['#attached']['js'][] = ctools_attach_js('jump-menu');
54

    
55
  if (!empty($options['choose'])) {
56
    $select = array('' => $options['choose']) + $select;
57
  }
58

    
59
  $form['jump'] = array(
60
    '#type' => 'select',
61
    '#options' => $select,
62
    '#attributes' => array(
63
      'class' => array('ctools-jump-menu-select'),
64
    ),
65
  );
66

    
67
  if (!empty($options['title'])) {
68
    $form['jump']['#title'] = $options['title'];
69
  }
70

    
71
  if (!empty($options['description'])) {
72
    $form['jump']['#description'] = $options['description'];
73
  }
74

    
75
  if (!empty($options['default_value'])) {
76
    $form['jump']['#default_value'] = $options['default_value'];
77
  }
78

    
79
  if (isset($options['image'])) {
80
    $form['go'] = array(
81
      '#type' => 'image_button',
82
      '#src' => $options['image'],
83
      '#submit' => array('ctools_jump_menu_submit'),
84
      '#attributes' => array(
85
        'class' => array('ctools-jump-menu-button'),
86
      ),
87
    );
88
  }
89
  else {
90
    $form['go'] = array(
91
      '#type' => 'submit',
92
      '#value' => $options['button'],
93
      '#submit' => array('ctools_jump_menu_submit'),
94
      '#attributes' => array(
95
        'class' => array('ctools-jump-menu-button'),
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']) {
106
    $form['jump']['#attributes']['class'][] = 'ctools-jump-menu-change';
107
    $form['go']['#attributes']['class'][] = 'ctools-jump-menu-hide';
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
 */
118
function ctools_jump_menu_submit($form, &$form_state) {
119
  if ($form_state['values']['jump'] === '') {
120
    // We have nothing to do when the user has not selected any value.
121
    return;
122
  }
123

    
124
  // If the path we are redirecting to contains the string :: then treat the
125
  // the string after the double colon as the path to redirect to.
126
  // This allows duplicate paths to be used in jump menus for multiple options.
127
  $redirect_array = explode("::", $form_state['values']['jump']);
128

    
129
  if (isset($redirect_array[1]) && !empty($redirect_array[1])) {
130
    $redirect = $redirect_array[1];
131
  }
132
  else {
133
    $redirect = $form_state['values']['jump'];
134
  }
135

    
136
  // If the path we are redirecting to starts with the base path (for example,
137
  // "/somepath/node/1"), we need to strip the base path off before passing it
138
  // to $form_state['redirect'].
139
  $base_path = base_path();
140
  if (strpos($redirect, $base_path) === 0) {
141
    $redirect = substr($redirect, strlen($base_path));
142
  }
143

    
144
  // Parse the URL so that query strings and fragments are preserved in the
145
  // redirect.
146
  $redirect = drupal_parse_url($redirect);
147
  $redirect['path'] = urldecode($redirect['path']);
148
  $form_state['redirect'] = array($redirect['path'], $redirect);
149
}