jump-menu.inc 4.6 KB

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