SHOW:
|
|
- or go back to the newest paste.
1 | <?php | |
2 | ||
3 | function test_menu() { | |
4 | $items = array(); | |
5 | $items['test'] = array( | |
6 | 'title' => 'Test', | |
7 | 'page callback' => 'test_page_handler', | |
8 | 'access callback' => TRUE, | |
9 | ); | |
10 | return $items; | |
11 | } | |
12 | ||
13 | function test_page_handler() { | |
14 | - | $output = drupal_get_form('test_simple_form'); |
14 | + | $output = drupal_get_form('test_simple_form', $data); |
15 | return $output; | |
16 | } | |
17 | ||
18 | function test_simple_form(&$form_state) { | |
19 | // If the form has already been built, and we already have the data, don't load it again. | |
20 | if (isset($form_state['values']['expensive_data'])) { | |
21 | $data = $form_state['values']['expensive_data']; | |
22 | } | |
23 | else { | |
24 | // Otherwise get the data from source. | |
25 | $data = very_expensive_data_operation(); | |
26 | } | |
27 | // Store the data | |
28 | $form['expensive_data'] = array( | |
29 | '#type' => 'value', | |
30 | '#value' => $data | |
31 | ); | |
32 | ||
33 | $form['create'] = array( | |
34 | '#type' => 'submit', | |
35 | '#value' => t('Submit'), | |
36 | ); | |
37 | return $form; | |
38 | } | |
39 | ||
40 | function test_simple_form_submit(&$form, &$form_state) { | |
41 | - | // do something |
41 | + | $form_state['rebuild'] = TRUE; |
42 | drupal_set_message('Submit'); | |
43 | } | |
44 | ||
45 | function very_expensive_data_operation() { | |
46 | drupal_set_message('Running VEDO ' . time()); | |
47 | sleep(1); | |
48 | return array(1,2,3,4,5,6,7,8,9); | |
49 | } |