Advertisement
Guest User

test form submit

a guest
Jun 7th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.10 KB | None | 0 0
  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');
  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
  42. }
  43.  
  44. function very_expensive_data_operation() {
  45.   drupal_set_message('Running VEDO ' . time());
  46.   sleep(1);
  47.   return array(1,2,3,4,5,6,7,8,9);
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement