Guest User

Untitled

a guest
Jul 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. <?php
  2.  
  3. function wizard_page() {
  4. $steps = array(
  5. 'step1',
  6. 'step2',
  7. 'step3',
  8. );
  9.  
  10. return drupal_get_form('multistep_form', $steps)
  11. . 'Multistep form';
  12. }
  13.  
  14. //function fpn_customer_become_customer_submit($form, &$form_state) {
  15. //
  16. //}
  17.  
  18.  
  19. function multistep_form(&$form_state, $steps) {
  20. if (!$form_state['storage']['step']) {
  21. reset($steps);
  22. $form_state['storage']['steps'] = $steps;
  23. $form_state['storage']['step'] = $steps[key($steps)];
  24. $form_state['storage']['values'] = array();
  25. }
  26.  
  27. $form = multistep_retrieve_step_form($form_state);
  28. settype($form['#submit'], 'array');
  29. $form['#submit'][] = 'multistep_process_submit';
  30. multistep_form_submit_buttons($form, $form_state);
  31.  
  32. return $form;
  33. }
  34.  
  35. function multistep_retrieve_step_form($form_state) {
  36. $step = $form_state['storage']['step'];
  37. $form = $step($form_state);
  38.  
  39. if (!isset($form['#validate'])) {
  40. if (is_callable($step .'_validate'))
  41. $form['#validate'][] = $step .'_validate';
  42. }
  43.  
  44. if (!isset($form['#submit'])) {
  45. if (is_callable($step .'_submit'))
  46. $form['#submit'][] = $step .'_submit';
  47. }
  48. dpm($form);
  49. return $form;
  50. }
  51.  
  52.  
  53. function multistep_process_submit($form, &$form_state) {
  54. $steps = $form_state['storage']['steps'];
  55. $step = $form_state['storage']['step'];
  56. reset($steps); $first_step = current($steps);
  57. end($steps); $last_step = current($steps);
  58.  
  59. $form_state['storage']['values'][$step] = $form_state['values'];
  60.  
  61. // dpm($form_state);
  62. if ($form_state['values']['op'] == $form['multistep_buttons']['next']['#value']) {
  63. if ($step == $last_step) {
  64. unset($form_state['storage']);
  65. }
  66. else {
  67. $index = array_search($step, $steps);
  68. $form_state['storage']['step'] = $steps[$index + 1];
  69. }
  70. }
  71. else if ($form_state['values']['op'] == $form['multistep_buttons']['back']['#value']) {
  72. $index = array_search($step, $steps);
  73. $form_state['storage']['step'] = $steps[$index - 1];
  74.  
  75. }
  76. }
  77.  
  78. function multistep_form_submit_buttons(&$form, $form_state) {
  79. $steps = $form_state['storage']['steps'];
  80. $step = $form_state['storage']['step'];
  81. reset($steps); $first_step = current($steps);
  82. end($steps); $last_step = current($steps);
  83.  
  84. $buttons = array();
  85. if ($step != $first_step) {
  86. $buttons['back'] = array(
  87. '#type' => 'submit',
  88. '#value' => t('Back'),
  89. );
  90. }
  91. if ($step != $last_step) {
  92. $buttons['next'] = array(
  93. '#type' => 'submit',
  94. '#value' => t('Next'),
  95. );
  96. }
  97. else {
  98. $buttons['next'] = array(
  99. '#type' => 'submit',
  100. '#value' => t('Finish'),
  101. );
  102. }
  103.  
  104. $form['multistep_buttons'] = $buttons;
  105. $form['multistep_buttons']['#tree'] = true;
  106. }
  107.  
  108.  
  109.  
  110.  
  111. function step1($form_state) {
  112. $form['input1'] = array(
  113. '#type' => 'textfield',
  114. '#title' => 'input1',
  115. );
  116.  
  117. return $form;
  118. }
  119.  
  120. function step1_submit($form_state) {
  121. dpm(__FUNCTION__);
  122. }
  123.  
  124. function step1_validate($form_state) {
  125. dpm(__FUNCTION__);
  126. }
  127.  
  128. function step2($form_state) {
  129. $form['input2'] = array(
  130. '#type' => 'textfield',
  131. '#title' => 'input2',
  132. );
  133.  
  134. return $form;
  135. }
  136.  
  137. function step2_submit($form_state) {
  138. dpm(__FUNCTION__);
  139. }
  140.  
  141. function step2_validate($form_state) {
  142. dpm(__FUNCTION__);
  143. }
  144.  
  145. function step3($form_state) {
  146. $form['input3'] = array(
  147. '#type' => 'textfield',
  148. '#title' => 'input3',
  149. );
  150.  
  151. $form['#submit'][] = 'step3___submit';
  152. $form['#validate'][] = 'step3___validate';
  153.  
  154. return $form;
  155.  
  156. }
  157.  
  158. function step3___submit($form_state) {
  159. dpm(__FUNCTION__);
  160. }
  161.  
  162. function step3___validate($form_state) {
  163. dpm(__FUNCTION__);
  164. }
Add Comment
Please, Sign In to add comment