source: sipes/modules_contrib/ctools/includes/form.inc @ 92213c1

stableversion-3.0
Last change on this file since 92213c1 was 2efe680, checked in by José Gregorio Puentes <jpuentes@…>, 8 años ago

se actualizo el modulo

  • Propiedad mode establecida a 100755
File size: 13.8 KB
Línea 
1<?php
2
3/**
4 * @file
5 * CTools' replacements for Drupal's form functions.
6 *
7 * Primarily, ctools_build_form is meant to be a replacement for drupal_get_form().
8 *
9 * Instead of sending arguments through to the form builder, a form state array
10 * is passed through. This form_state can contain any arguments needed, and it can
11 * also be used to return data to the calling function.
12 *
13 * This can allow cleaner separation of the form from the storage mechanism.
14 */
15
16/**
17 * Build a form, similar to drupal_get_form(). However, arguments
18 * to the form builder are not sent through. Instead, the $form_state
19 * can be given all the necessary data to fully utilize the form.
20 */
21function ctools_build_form($form_id, &$form_state) {
22  // Ensure that we have some defaults.
23
24  // These are defaults only; if already set they will not be overridden.
25  $form_state += array('storage' => NULL, 'submitted' => FALSE, 'input' => $_POST, 'method' => 'post');
26
27  $args = isset($form_state['args']) ? $form_state['args'] : array();
28  $cacheable = FALSE;
29
30  if (isset($_SESSION['batch_form_state'])) {
31    // We've been redirected here after a batch processing : the form has
32    // already been processed, so we grab the post-process $form_state value
33    // and move on to form display. See _batch_finished() function.
34    $form_state = $_SESSION['batch_form_state'];
35    unset($_SESSION['batch_form_state']);
36  }
37  else {
38    // If the incoming $form_state['input'] contains a form_build_id, we'll check the
39    // cache for a copy of the form in question. If it's there, we don't
40    // have to rebuild the form to proceed. In addition, if there is stored
41    // form_state data from a previous step, we'll retrieve it so it can
42    // be passed on to the form processing code.
43    if (isset($form_state['input']['form_id']) && $form_state['input']['form_id'] == $form_id && !empty($form_state['input']['form_build_id'])) {
44      $form_build_id = $form_state['input']['form_build_id'];
45      $form = form_get_cache($form_build_id, $form_state);
46      if (!empty($form['#no_cache']) || empty($form)) {
47        unset($form);
48      }
49    }
50
51    // If the previous bit of code didn't result in a populated $form
52    // object, we're hitting the form for the first time and we need
53    // to build it from scratch.
54    if (!isset($form)) {
55      $form_state['post'] = $form_state['input'];
56      // This allows us to do some interesting form embedding stuff without
57      // messing up the form IDs too badly.
58      if (isset($form_state['wrapper callback']) && function_exists($form_state['wrapper callback'])) {
59        // If there is a wrapper callback, we do not use drupal_retrieve_form.
60        // Instead, we call $form_id builder function directly. This means the args
61        // are *different* for forms used this way, which may not be ideal but
62        // is necessary right now.
63        $form = array();
64        $form_state['wrapper callback']($form, $form_state);
65        if (function_exists($form_id)) {
66          $form_id($form, $form_state);
67        }
68      }
69      else {
70        // Use a copy of the function's arguments for manipulation
71        $args_temp = $args;
72        array_unshift($args_temp, 'placeholder');
73        $args_temp[0] = &$form_state; // replaces the placeholder.
74        array_unshift($args_temp, $form_id);
75
76        $form = call_user_func_array('drupal_retrieve_form', $args_temp);
77      }
78
79      $form_build_id = 'form-' . md5(mt_rand());
80      $form['#build_id'] = $form_build_id;
81
82
83      if ($form_state['method'] == 'get' && !isset($form['#method'])) {
84        $form['#method'] = 'get';
85      }
86
87      drupal_prepare_form($form_id, $form, $form_state);
88      // Store a copy of the unprocessed form for caching and indicate that it
89      // is cacheable if #cache will be set.
90      $original_form = $form;
91      $cacheable = TRUE;
92      unset($form_state['post']);
93    }
94    $form['#post'] = $form_state['input'];
95
96    // Now that we know we have a form, we'll process it (validating,
97    // submitting, and handling the results returned by its submission
98    // handlers. Submit handlers accumulate data in the form_state by
99    // altering the $form_state variable, which is passed into them by
100    // reference.
101    ctools_process_form($form_id, $form, $form_state);
102    // If we were told not to redirect, but not told to re-render, return
103    // here.
104    if (!empty($form_state['executed']) && empty($form_state['rerender'])) {
105      return;
106    }
107    if ($cacheable && !empty($form['#cache']) && empty($form['#no_cache'])) {
108      // Caching is done past drupal_process_form so #process callbacks can
109      // set #cache. By not sending the form state, we avoid storing
110      // $form_state['storage'].
111      form_set_cache($form_build_id, $original_form, NULL);
112    }
113  }
114
115  // Most simple, single-step forms will be finished by this point --
116  // drupal_process_form() usually redirects to another page (or to
117  // a 'fresh' copy of the form) once processing is complete. If one
118  // of the form's handlers has set $form_state['redirect'] to FALSE,
119  // the form will simply be re-rendered with the values still in its
120  // fields.
121  //
122  // If $form_state['storage'] or $form_state['rebuild'] have been
123  // set by any submit or validate handlers, however, we know that
124  // we're in a complex multi-part process of some sort and the form's
125  // workflow is NOT complete. We need to construct a fresh copy of
126  // the form, passing in the latest $form_state in addition to any
127  // other variables passed into drupal_get_form().
128  //
129  // If this function is being used to perform an '#ahah' callback
130  // to rebuild some or all of a ctools wizard form step, be sure that
131  // $form_state['wrapper callback'], $form_state['form_info'],
132  // $form_state['step'], $form_state['no_redirect'], and $form_state['rebuild']
133  // are properly set.
134
135  if (!empty($form_state['rebuild']) || !empty($form_state['storage'])) {
136    $form = ctools_rebuild_form($form_id, $form_state, $args, $form_build_id);
137  }
138
139  // If whoever is calling this wants the $form array (so that it can render it
140  // another way, for example) then return it.
141  if (!empty($form_state['want form'])) {
142    return $form;
143  }
144
145  // Do not render certain items if requested not to:
146  if (!empty($form_state['drop tokens'])) {
147    unset($form['#id']);
148    unset($form['#build_id']);
149    unset($form['#token']);
150    unset($form['form_token']);
151  }
152  // If we haven't redirected to a new location by now, we want to
153  // render whatever form array is currently in hand.
154  // Do not render certain items if requested not to:
155  if (!empty($form_state['drop tokens'])) {
156    unset($form['form_id']);
157    unset($form['form_build_id']);
158    unset($form['form_token']);
159  }
160  return drupal_render_form($form_id, $form);
161}
162
163/**
164 * ctools' replacement of drupal_rebuild_form.
165 *
166 * This change merely respects a form's wishes not to be cached.
167 */
168function ctools_rebuild_form($form_id, &$form_state, $args, $form_build_id = NULL) {
169  // Remove the first argument. This is $form_id.when called from
170  // drupal_get_form and the original $form_state when called from some AHAH
171  // callback. Neither is needed. After that, put in the current state.
172  array_unshift($args, 'placeholder');
173  $args[0] = &$form_state; // Replaces placeholder.
174  // And the form_id.
175  array_unshift($args, $form_id);
176
177  if (isset($form_state['wrapper callback']) && function_exists($form_state['wrapper callback'])) {
178    // If there is a wrapper callback, we do not use drupal_retrieve_form.
179    // Instead, we call $form_id builder function directly. This means the args
180    // are *different* for forms used this way, which may not be ideal but
181    // is necessary right now.
182    $form = array();
183    $form_state['wrapper callback']($form, $form_state);
184    if (function_exists($form_id)) {
185      $form_id($form, $form_state);
186    }
187  }
188  else {
189    $form = call_user_func_array('drupal_retrieve_form', $args);
190  }
191
192  if (!isset($form_build_id)) {
193    // We need a new build_id for the new version of the form.
194    $form_build_id = 'form-' . md5(mt_rand());
195  }
196  $form['#build_id'] = $form_build_id;
197  // Flush form ID element cache because we may be rebuilding
198  // a form in a way that Drupal's FAPI isn't used to which
199  // causes unnecessary form ID changes.
200  form_clean_id(NULL, TRUE);
201  drupal_prepare_form($form_id, $form, $form_state);
202
203  if (empty($form['#no_cache'])) {
204    // Now, we cache the form structure so it can be retrieved later for
205    // validation. If $form_state['storage'] is populated, we'll also cache
206    // it so that it can be used to resume complex multi-step processes.
207    form_set_cache($form_build_id, $form, $form_state);
208  }
209
210  // Originally this called drupal_process_form, but all that happens there
211  // is form_builder and then submission; and the rebuilt form is not
212  // allowed to submit. Therefore, just do this:
213  $form['#post'] = $form_state['input'];
214  $form = form_builder($form_id, $form, $form_state);
215
216  return $form;
217}
218
219/**
220 * ctools' replacement for drupal_process_form that accepts commands
221 * not to redirect, as well as forcing processing of 'get' method forms.
222 */
223function ctools_process_form($form_id, &$form, &$form_state) {
224  // submitting, and handling the results returned by its submission
225  // handlers. Submit handlers accumulate data in the form_state by
226  // altering the $form_state variable, which is passed into them by
227  // reference.
228  $form_state['values'] = array();
229
230  // With $_GET, these forms are always submitted.
231  if ($form_state['method'] == 'get') {
232    if (!isset($form['#post']['form_build_id'])) {
233      $form['#post']['form_build_id'] = $form['#build_id'];
234    }
235    if (!isset($form['#post']['form_id'])) {
236      $form['#post']['form_id'] = $form_id;
237    }
238    if (!isset($form['#post']['form_token']) && isset($form['#token'])) {
239      $form['#post']['form_token'] = drupal_get_token($form['#token']);
240    }
241  }
242
243  $form = form_builder($form_id, $form, $form_state);
244  // Only process the form if it is programmed or the form_id coming
245  // from the POST data is set and matches the current form_id.
246
247  if ((!empty($form['#programmed'])) || (!empty($form['#post']) && (isset($form['#post']['form_id']) && ($form['#post']['form_id'] == $form_id)))) {
248    ctools_validate_form($form_id, $form, $form_state);
249
250    // form_clean_id() maintains a cache of element IDs it has seen,
251    // so it can prevent duplicates. We want to be sure we reset that
252    // cache when a form is processed, so scenerios that result in
253    // the form being built behind the scenes and again for the
254    // browser don't increment all the element IDs needlessly.
255    form_clean_id(NULL, TRUE);
256
257    if ((!empty($form_state['submitted'])) && !form_get_errors() && empty($form_state['rebuild'])) {
258      $form_state['redirect'] = NULL;
259      form_execute_handlers('submit', $form, $form_state);
260
261      // We'll clear out the cached copies of the form and its stored data
262      // here, as we've finished with them. The in-memory copies are still
263      // here, though.
264      if (variable_get('cache', CACHE_DISABLED) == CACHE_DISABLED && !empty($form_state['values']['form_build_id'])) {
265        cache_clear_all('form_' . $form_state['values']['form_build_id'], 'cache_form');
266        cache_clear_all('storage_' . $form_state['values']['form_build_id'], 'cache_form');
267      }
268
269      // If batches were set in the submit handlers, we process them now,
270      // possibly ending execution. We make sure we do not react to the batch
271      // that is already being processed (if a batch operation performs a
272      // drupal_execute).
273      if ($batch = &batch_get() && !isset($batch['current_set'])) {
274        // The batch uses its own copies of $form and $form_state for
275        // late execution of submit handers and post-batch redirection.
276        $batch['form'] = $form;
277        $batch['form_state'] = $form_state;
278        $batch['progressive'] = !$form['#programmed'];
279        batch_process();
280        // Execution continues only for programmatic forms.
281        // For 'regular' forms, we get redirected to the batch processing
282        // page. Form redirection will be handled in _batch_finished(),
283        // after the batch is processed.
284      }
285
286      // If no submit handlers have populated the $form_state['storage']
287      // bundle, and the $form_state['rebuild'] flag has not been set,
288      // we're finished and should redirect to a new destination page
289      // if one has been set (and a fresh, unpopulated copy of the form
290      // if one hasn't). If the form was called by drupal_execute(),
291      // however, we'll skip this and let the calling function examine
292      // the resulting $form_state bundle itself.
293      if (!$form['#programmed'] && empty($form_state['rebuild']) && empty($form_state['storage'])) {
294        if (!empty($form_state['no_redirect'])) {
295          $form_state['executed'] = TRUE;
296        }
297        else {
298          drupal_redirect_form($form, $form_state['redirect']);
299        }
300      }
301    }
302  }
303}
304
305/**
306 * The original version of drupal_validate_form does not have an override for
307 * the static check to only validate a form id once. Unfortunately, we need
308 * to be able to override this.
309 */
310function ctools_validate_form($form_id, $form, &$form_state) {
311  static $validated_forms = array();
312
313  if (isset($validated_forms[$form_id]) && empty($form_state['must_validate'])) {
314    return;
315  }
316
317  // If the session token was set by drupal_prepare_form(), ensure that it
318  // matches the current user's session.
319  if (isset($form['#token'])) {
320    if (!drupal_valid_token($form_state['values']['form_token'], $form['#token'])) {
321      // Setting this error will cause the form to fail validation.
322      form_set_error('form_token', t('Validation error, please try again. If this error persists, please contact the site administrator.'));
323    }
324  }
325
326  if (!empty($form_state['clicked_button']['#skip validation'])) {
327    return;
328  }
329
330  _form_validate($form, $form_state, $form_id);
331  $validated_forms[$form_id] = TRUE;
332}
333
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.