form.inc

form.inc Views' replacements for Drupal's form functions.

File

includes/form.inc
View source
  1. <?php
  2. /**
  3. * @file form.inc
  4. * Views' replacements for Drupal's form functions.
  5. *
  6. */
  7. function _drupal_build_form($form_id, &$form_state) {
  8. // Ensure that we have some defaults.
  9. // These are defaults only; if already set they will not be overridden.
  10. $form_state += array('storage' => NULL, 'submitted' => FALSE, 'input' => $_POST, 'method' => 'post');
  11. $args = isset($form_state['args']) ? $form_state['args'] : array();
  12. $cacheable = FALSE;
  13. if (isset($_SESSION['batch_form_state'])) {
  14. // We've been redirected here after a batch processing : the form has
  15. // already been processed, so we grab the post-process $form_state value
  16. // and move on to form display. See _batch_finished() function.
  17. $form_state = $_SESSION['batch_form_state'];
  18. unset($_SESSION['batch_form_state']);
  19. }
  20. else {
  21. // If the incoming $_POST contains a form_build_id, we'll check the
  22. // cache for a copy of the form in question. If it's there, we don't
  23. // have to rebuild the form to proceed. In addition, if there is stored
  24. // form_state data from a previous step, we'll retrieve it so it can
  25. // be passed on to the form processing code.
  26. if (isset($_POST['form_id']) && $_POST['form_id'] == $form_id && !empty($_POST['form_build_id'])) {
  27. $form = form_get_cache($_POST['form_build_id'], $form_state);
  28. if (!empty($form['#no_cache']) || empty($form)) {
  29. unset($form);
  30. }
  31. }
  32. // If the previous bit of code didn't result in a populated $form
  33. // object, we're hitting the form for the first time and we need
  34. // to build it from scratch.
  35. if (!isset($form)) {
  36. $form_state['post'] = $form_state['input'];
  37. // Use a copy of the function's arguments for manipulation
  38. $args_temp = $args;
  39. $args_temp[0] = &$form_state;
  40. array_unshift($args_temp, $form_id);
  41. $form = call_user_func_array('drupal_retrieve_form', $args_temp);
  42. $form_build_id = 'form-' . md5(mt_rand());
  43. $form['#build_id'] = $form_build_id;
  44. if ($form_state['method'] == 'get' && !isset($form['#method'])) {
  45. $form['#method'] = 'get';
  46. }
  47. drupal_prepare_form($form_id, $form, $form_state);
  48. // Store a copy of the unprocessed form for caching and indicate that it
  49. // is cacheable if #cache will be set.
  50. $original_form = $form;
  51. $cacheable = TRUE;
  52. unset($form_state['post']);
  53. }
  54. $form['#post'] = $form_state['input'];
  55. // Now that we know we have a form, we'll process it (validating,
  56. // submitting, and handling the results returned by its submission
  57. // handlers. Submit handlers accumulate data in the form_state by
  58. // altering the $form_state variable, which is passed into them by
  59. // reference.
  60. drupal_process_form_new($form_id, $form, $form_state);
  61. // If we were told not to redirect, but not told to re-render, return
  62. // here.
  63. if (!empty($form_state['executed']) && empty($form_state['rerender'])) {
  64. return;
  65. }
  66. if ($cacheable && !empty($form['#cache']) && empty($form['#no_cache'])) {
  67. // Caching is done past drupal_process_form so #process callbacks can
  68. // set #cache. By not sending the form state, we avoid storing
  69. // $form_state['storage'].
  70. form_set_cache($form_build_id, $original_form, NULL);
  71. }
  72. }
  73. // Most simple, single-step forms will be finished by this point --
  74. // drupal_process_form() usually redirects to another page (or to
  75. // a 'fresh' copy of the form) once processing is complete. If one
  76. // of the form's handlers has set $form_state['redirect'] to FALSE,
  77. // the form will simply be re-rendered with the values still in its
  78. // fields.
  79. //
  80. // If $form_state['storage'] or $form_state['rebuild'] have been
  81. // set by any submit or validate handlers, however, we know that
  82. // we're in a complex multi-part process of some sort and the form's
  83. // workflow is NOT complete. We need to construct a fresh copy of
  84. // the form, passing in the latest $form_state in addition to any
  85. // other variables passed into drupal_get_form().
  86. if (!empty($form_state['rebuild']) || !empty($form_state['storage'])) {
  87. $form = drupal_rebuild_form_new($form_id, $form_state, $args);
  88. }
  89. // If we haven't redirected to a new location by now, we want to
  90. // render whatever form array is currently in hand.
  91. return drupal_render_form($form_id, $form);
  92. }
  93. /**
  94. * Views' replacement of drupal_rebuild_form.
  95. *
  96. * This change merely respects a form's wishes not to be cached.
  97. */
  98. function drupal_rebuild_form_new($form_id, &$form_state, $args, $form_build_id = NULL) {
  99. // Remove the first argument. This is $form_id.when called from
  100. // drupal_get_form and the original $form_state when called from some AHAH
  101. // callback. Neither is needed. After that, put in the current state.
  102. $args[0] = &$form_state;
  103. // And the form_id.
  104. array_unshift($args, $form_id);
  105. $form = call_user_func_array('drupal_retrieve_form', $args);
  106. if (!isset($form_build_id)) {
  107. // We need a new build_id for the new version of the form.
  108. $form_build_id = 'form-' . md5(mt_rand());
  109. }
  110. $form['#build_id'] = $form_build_id;
  111. drupal_prepare_form($form_id, $form, $form_state);
  112. if (empty($form['#no_cache'])) {
  113. // Now, we cache the form structure so it can be retrieved later for
  114. // validation. If $form_state['storage'] is populated, we'll also cache
  115. // it so that it can be used to resume complex multi-step processes.
  116. form_set_cache($form_build_id, $form, $form_state);
  117. }
  118. // Originally this called drupal_process_form, but all that happens there
  119. // is form_builder and then submission; and the rebuilt form is not
  120. // allowed to submit. Therefore, just do this:
  121. $form['#post'] = $form_state['input'];
  122. $form = form_builder($form_id, $form, $form_state);
  123. return $form;
  124. }
  125. /**
  126. * Views' replacement for drupal_process_form that accepts commands
  127. * not to redirect, as well as forcing processing of 'get' method forms.
  128. */
  129. function drupal_process_form_new($form_id, &$form, &$form_state) {
  130. // submitting, and handling the results returned by its submission
  131. // handlers. Submit handlers accumulate data in the form_state by
  132. // altering the $form_state variable, which is passed into them by
  133. // reference.
  134. $form_state['values'] = array();
  135. // With $_GET, these forms are always submitted.
  136. if ($form_state['method'] == 'get') {
  137. if (!isset($form['#post']['form_build_id'])) {
  138. $form['#post']['form_build_id'] = $form['#build_id'];
  139. }
  140. if (!isset($form['#post']['form_id'])) {
  141. $form['#post']['form_id'] = $form_id;
  142. }
  143. if (!isset($form['#post']['form_token']) && isset($form['#token'])) {
  144. $form['#post']['form_token'] = drupal_get_token($form['#token']);
  145. }
  146. }
  147. $form = form_builder($form_id, $form, $form_state);
  148. // Only process the form if it is programmed or the form_id coming
  149. // from the POST data is set and matches the current form_id.
  150. if ((!empty($form['#programmed'])) || (!empty($form['#post']) && (isset($form['#post']['form_id']) && ($form['#post']['form_id'] == $form_id)))) {
  151. drupal_validate_form_new($form_id, $form, $form_state);
  152. // form_clean_id() maintains a cache of element IDs it has seen,
  153. // so it can prevent duplicates. We want to be sure we reset that
  154. // cache when a form is processed, so scenerios that result in
  155. // the form being built behind the scenes and again for the
  156. // browser don't increment all the element IDs needlessly.
  157. form_clean_id(NULL, TRUE);
  158. if ((!empty($form_state['submitted'])) && !form_get_errors() && empty($form_state['rebuild'])) {
  159. $form_state['redirect'] = NULL;
  160. form_execute_handlers('submit', $form, $form_state);
  161. // We'll clear out the cached copies of the form and its stored data
  162. // here, as we've finished with them. The in-memory copies are still
  163. // here, though.
  164. if (variable_get('cache', CACHE_DISABLED) == CACHE_DISABLED && !empty($form_state['values']['form_build_id'])) {
  165. cache_clear_all('form_' . $form_state['values']['form_build_id'], 'cache_form');
  166. cache_clear_all('storage_' . $form_state['values']['form_build_id'], 'cache_form');
  167. }
  168. // If batches were set in the submit handlers, we process them now,
  169. // possibly ending execution. We make sure we do not react to the batch
  170. // that is already being processed (if a batch operation performs a
  171. // drupal_execute).
  172. if ($batch =& batch_get() && !isset($batch['current_set'])) {
  173. // The batch uses its own copies of $form and $form_state for
  174. // late execution of submit handers and post-batch redirection.
  175. $batch['form'] = $form;
  176. $batch['form_state'] = $form_state;
  177. $batch['progressive'] = !$form['#programmed'];
  178. batch_process();
  179. // Execution continues only for programmatic forms.
  180. // For 'regular' forms, we get redirected to the batch processing
  181. // page. Form redirection will be handled in _batch_finished(),
  182. // after the batch is processed.
  183. }
  184. // If no submit handlers have populated the $form_state['storage']
  185. // bundle, and the $form_state['rebuild'] flag has not been set,
  186. // we're finished and should redirect to a new destination page
  187. // if one has been set (and a fresh, unpopulated copy of the form
  188. // if one hasn't). If the form was called by drupal_execute(),
  189. // however, we'll skip this and let the calling function examine
  190. // the resulting $form_state bundle itself.
  191. if (!$form['#programmed'] && empty($form_state['rebuild']) && empty($form_state['storage'])) {
  192. if (!empty($form_state['no_redirect'])) {
  193. $form_state['executed'] = TRUE;
  194. }
  195. else {
  196. drupal_redirect_form($form, $form_state['redirect']);
  197. }
  198. }
  199. }
  200. }
  201. }
  202. /**
  203. * The original version of drupal_validate_form does not have an override for
  204. * the static check to only validate a form id once. Unfortunately, we need
  205. * to be able to overridet his.
  206. */
  207. function drupal_validate_form_new($form_id, $form, &$form_state) {
  208. static $validated_forms = array();
  209. if (isset($validated_forms[$form_id]) && empty($form_state['must_validate'])) {
  210. return;
  211. }
  212. // If the session token was set by drupal_prepare_form(), ensure that it
  213. // matches the current user's session.
  214. if (isset($form['#token'])) {
  215. if (!drupal_valid_token($form_state['values']['form_token'], $form['#token'])) {
  216. // Setting this error will cause the form to fail validation.
  217. form_set_error('form_token', t('Validation error, please try again. If this error persists, please contact the site administrator.'));
  218. }
  219. }
  220. _form_validate($form, $form_state, $form_id);
  221. $validated_forms[$form_id] = TRUE;
  222. }
  223. /**
  224. * Process callback to add dependency to form items.
  225. *
  226. * Usage:
  227. *
  228. * On any form item, add
  229. * - @code '#process' => 'views_process_dependency' @endcode
  230. * - @code '#dependency' => array('id-of-form-without-the-#' => array(list, of, values, that, make, this, gadget, visible)); @endcode
  231. */
  232. function views_process_dependency($element, $edit, &$form_state, &$form) {
  233. static $dependencies;
  234. if (isset($element['#dependency']) && !isset($dependencies[$element['#id']])) {
  235. if (!isset($element['#dependency_count'])) {
  236. $element['#dependency_count'] = 1;
  237. }
  238. if (!empty($form_state['ajax'])) {
  239. $form_state['js settings']['viewsAjax']['formRelationships'][$element['#id']] = array('num' => $element['#dependency_count'], 'values' => $element['#dependency']);
  240. }
  241. else {
  242. views_add_js('dependent');
  243. $options['viewsAjax']['formRelationships'][$element['#id']] = array('num' => $element['#dependency_count'], 'values' => $element['#dependency']);
  244. drupal_add_js($options, 'setting');
  245. }
  246. $dependencies[$element['#id']] = TRUE;
  247. }
  248. return $element;
  249. }
  250. /**
  251. * #process callback to see if we need to check_plain() the options.
  252. *
  253. * Since FAPI is inconsistent, the #options are sanitized for you in all cases
  254. * _except_ checkboxes. We have form elements that are sometimes 'select' and
  255. * sometimes 'checkboxes', so we need decide late in the form rendering cycle
  256. * if the options need to be sanitized before they're rendered. This callback
  257. * inspects the type, and if it's still 'checkboxes', does the sanitation.
  258. */
  259. function views_process_check_options($element, $edit, &$form_state, &$form) {
  260. if ($element['#type'] == 'checkboxes' || $element['#type'] == 'checkbox') {
  261. $element['#options'] = array_map('check_plain', $element['#options']);
  262. }
  263. return $element;
  264. }