Advertisement
Guest User

spower.module

a guest
May 19th, 2013
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.28 KB | None | 0 0
  1. <?php
  2.  
  3. function spower_menu() {
  4.   $items = array();
  5.   $items['spower'] = array(
  6.     'title' => t('My form'),
  7.     'page callback' => 'spower_form',
  8.     'access arguments' => array('access content'),
  9.     'description' => t('My form'),
  10.     'type' => MENU_CALLBACK,
  11.   );
  12.   return $items;
  13. }
  14.  
  15. function spower_form() {
  16.   return drupal_get_form('spower_my_form');
  17. }
  18.  
  19. function spower_my_form($form, &$form_state, $no_js_use = FALSE) {
  20.   $form['name'] = array(
  21.     '#type' => 'fieldset',
  22.     '#title' => t('Name'),
  23.     '#collapsible' => FALSE,
  24.     '#collapsed' => FALSE,
  25.   );
  26.  
  27.    $form['#tree'] = TRUE;
  28.    $form['names_fieldset'] = array(
  29.     '#type' => 'fieldset',
  30.     '#title' => t('People coming to the picnic'),
  31.     // Set up the wrapper so that AJAX will be able to replace the fieldset.
  32.     '#prefix' => '<div id="names-fieldset-wrapper">',
  33.     '#suffix' => '</div>',
  34.   );
  35.  
  36.   $form['name']['first'] = array(
  37.     '#type' => 'textfield',
  38.     '#title' => t('First name'),
  39.     '#required' => TRUE,
  40.     '#default_value' => "First name",
  41.     '#description' => "Please enter your first name.",
  42.     '#size' => 20,
  43.     '#maxlength' => 20,
  44.   );
  45.   $form['name']['last'] = array(
  46.     '#type' => 'textfield',
  47.     '#title' => t('Last name'),
  48.     '#required' => TRUE,
  49.   );
  50.   $form['name']['year_of_birth'] = array(
  51.     '#type' => 'textfield',
  52.     '#title' => "Year of birth",
  53.     '#description' => 'Format is "YYYY"',
  54.   );  
  55.  
  56.  
  57.   // Build the fieldset with the proper number of names. We'll use
  58.   // $form_state['num_names'] to determine the number of textfields to build.
  59.   if (empty($form_state['num_names'])) {
  60.     $form_state['num_names'] = 1;
  61.   }
  62.   for ($i = 0; $i < $form_state['num_names']; $i++) {
  63.     $form['names_fieldset']['name'][$i] = array(
  64.       '#type' => 'textfield',
  65.       '#title' => t('Name'),
  66.     );
  67.   }
  68.   $form['names_fieldset']['add_name'] = array(
  69.     '#type' => 'submit',
  70.     '#value' => t('Add one more'),
  71.     '#submit' => array('spower_add_more_add_one'),
  72.     // See the examples in ajax_example.module for more details on the
  73.     // properties of #ajax.
  74.     '#ajax' => array(
  75.       'callback' => 'spower_add_more_callback',
  76.       'wrapper' => 'names-fieldset-wrapper',
  77.     ),
  78.   );
  79.   if ($form_state['num_names'] > 1) {
  80.     $form['names_fieldset']['remove_name'] = array(
  81.       '#type' => 'submit',
  82.       '#value' => t('Remove one'),
  83.       '#submit' => array('spower_add_more_remove_one'),
  84.       '#ajax' => array(
  85.         'callback' => 'spower_add_more_callback',
  86.         'wrapper' => 'names-fieldset-wrapper',
  87.       ),
  88.     );
  89.   }
  90.  
  91.  
  92.   $form['submit'] = array(
  93.     '#type' => 'submit',
  94.     '#value' => 'Submit',
  95.     '#ajax' => array(
  96.               'callback' => 'spower_ajax_callback',
  97.               'wrapper' => 'tabs-3',
  98.           ),
  99.      
  100.   );
  101.   return $form;
  102. }
  103.  
  104. function spower_theme($existing, $type, $theme, $path) {
  105.   $base = array(
  106.     'render element' => 'form',
  107.   );
  108.  
  109.   return array(
  110.     'spower_my_form' => $base + array(
  111.       'template' => 'spower',
  112.     ),
  113.   );
  114. }
  115.  
  116. function spower_my_form_validate($form, &$form_state) {
  117.     $year_of_birth = $form_state['values']['year_of_birth'];
  118.     if ($year_of_birth && ($year_of_birth < 1900 || $year_of_birth > 2000)) {
  119.         form_set_error('year_of_birth', 'Enter a year between 1900 and 2000.');
  120.     }
  121. }
  122.  
  123. // Adds a submit handler/function to our form to send a successful
  124. // completion message to the screen.
  125.  
  126.  
  127. function spower_my_form_submit($form, &$form_state) {
  128.     drupal_set_message(t('The form has been submitted.'));
  129. };
  130.  
  131. function spower_ajax_callback($form, $form_state){
  132.     return "<div id=\"tabs-3\">hello world</div>";
  133.    
  134. }
  135.  
  136. function spower_add_more_callback($form, $form_state) {
  137.   return $form['names_fieldset'];
  138. }
  139.  
  140. /**
  141.  * Submit handler for the "add-one-more" button.
  142.  *
  143.  * Increments the max counter and causes a rebuild.
  144.  */
  145. function spower_add_more_add_one($form, &$form_state) {
  146.   $form_state['num_names']++;
  147.   $form_state['rebuild'] = TRUE;
  148. }
  149.  
  150. /**
  151.  * Submit handler for the "remove one" button.
  152.  *
  153.  * Decrements the max counter and causes a form rebuild.
  154.  */
  155. function spower_add_more_remove_one($form, &$form_state) {
  156.   if ($form_state['num_names'] > 1) {
  157.     $form_state['num_names']--;
  158.   }
  159.   $form_state['rebuild'] = TRUE;
  160. }
  161.  
  162. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement