| Commit | Line | Data |
|---|---|---|
| dfd5d58a SB |
1 | <?php |
| 2 | // $Id$ | |
| 3 | ||
| 4 | /** | |
| 5 | * @file | |
| 6 | * Provides a simple "jump menu". | |
| 7 | * | |
| 8 | * A jump menu is a select box and an optional 'go' button which can be removed | |
| 9 | * if javascript is in use. Each item is keyed to the href that the button | |
| 10 | * should go to. With javascript, the page is immediately redirected. Without | |
| 11 | * javascript, the form is submitted and a drupal_goto() is given. | |
| 12 | * | |
| 13 | */ | |
| 14 | ||
| 15 | /** | |
| 16 | * Generate a jump menu form. | |
| 17 | * | |
| 18 | * This can either be used with drupal_get_form() or directly added to a | |
| 19 | * form. The button provides its own submit handler so by default, other | |
| 20 | * submit handlers will not be called. | |
| 21 | * | |
| 22 | * One note: Do not use #tree = TRUE or it will be unable to find the | |
| 23 | * proper value. | |
| 24 | * | |
| 25 | * @code | |
| 26 | * ctools_include('jump-menu'); | |
| 27 | * $output = drupal_get_form('ctools_jump_menu', $targets, $options); | |
| 28 | * @endcode | |
| 29 | * | |
| 30 | * @param $select | |
| 31 | * An array suitable for use as the #options. The keys will be the direct | |
| 32 | * URLs that will be jumped to, so you absolutely must encode these using | |
| 33 | * url() in order for them to work reliably. | |
| 34 | * | |
| 35 | * @param $options | |
| 36 | * $options may be an array with the following options: | |
| 37 | * - 'title': The text to display for the #title attribute. | |
| 38 | * - 'description': The text to display for the #description attribute. | |
| 39 | * - 'default_value': The text to display for the #default_value attribute. | |
| 40 | * - 'hide': If TRUE the go button will be set to hide via javascript and | |
| 41 | * will submit on change. | |
| 42 | * - 'button': The text to display on the button. | |
| 43 | * - 'image': If set, an image button will be used instead, and the image | |
| 44 | * set to this. | |
| 45 | * - 'inline': If set to TRUE (default) the display will be forced inline. | |
| 46 | */ | |
| 0a5510f6 | 47 | function ctools_jump_menu($form, &$form_state, $select, $options = array()) { |
| dfd5d58a SB |
48 | $options += array( |
| 49 | 'button' => t('Go'), | |
| 50 | 'choose' => t('- Choose -'), | |
| 51 | 'inline' => TRUE, | |
| 52 | 'hide' => TRUE, | |
| 53 | ); | |
| 54 | ||
| 55 | ctools_add_js('jump-menu'); | |
| 56 | ||
| 57 | if (!empty($options['choose'])) { | |
| 58 | $select = array('' => $options['choose']) + $select; | |
| 59 | } | |
| 60 | ||
| 61 | $form['jump'] = array( | |
| 62 | '#type' => 'select', | |
| 63 | '#options' => $select, | |
| 64 | '#attributes' => array( | |
| a40482ac | 65 | 'class' => array('ctools-jump-menu-select'), |
| dfd5d58a SB |
66 | ), |
| 67 | ); | |
| 68 | ||
| 69 | if (!empty($options['title'])) { | |
| 70 | $form['jump']['#title'] = $options['title']; | |
| 71 | } | |
| 72 | ||
| 73 | if (!empty($options['description'])) { | |
| 74 | $form['jump']['#description'] = $options['description']; | |
| 75 | } | |
| 76 | ||
| 77 | if (!empty($options['default_value'])) { | |
| 78 | $form['jump']['#default_value'] = $options['default_value']; | |
| 79 | } | |
| 80 | ||
| 81 | if (isset($options['image'])) { | |
| 82 | $form['go'] = array( | |
| 83 | '#type' => 'image_button', | |
| 84 | '#src' => $options['image'], | |
| 85 | '#submit' => array('ctools_jump_menu_submit'), | |
| 86 | '#attributes' => array( | |
| a40482ac | 87 | 'class' => array('ctools-jump-menu-button'), |
| dfd5d58a SB |
88 | ), |
| 89 | ); | |
| 90 | } | |
| 91 | else { | |
| 92 | $form['go'] = array( | |
| 93 | '#type' => 'submit', | |
| 94 | '#value' => $options['button'], | |
| 95 | '#attributes' => array( | |
| a40482ac | 96 | 'class' => array('ctools-jump-menu-button'), |
| dfd5d58a SB |
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']) { | |
| a40482ac SB |
107 | $form['jump']['#attributes']['class'][] = 'ctools-jump-menu-change'; |
| 108 | $form['go']['#attributes']['class'][] = 'ctools-jump-menu-hide'; | |
| dfd5d58a SB |
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 | $form_state['redirect'] = $form_state['values']['jump']; | |
| 121 | } |