jump-menu.inc 4.6 KB

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