Created
June 20, 2011 13:44
-
-
Save maciejzgadzaj/1035621 to your computer and use it in GitHub Desktop.
Basic Drupal multipart form example with Previous and Next buttons
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Implementation of hook_menu(). | |
| */ | |
| function test_menu() { | |
| $items = array(); | |
| // Form page. | |
| $items['test/form'] = array( | |
| 'title' => t('Test Form'), | |
| 'page callback' => 'test_page', | |
| 'page arguments' => array(), | |
| 'access arguments' => array('access content'), | |
| ); | |
| // Success page. | |
| $items['test/success'] = array( | |
| 'title' => t('Test successful!'), | |
| 'page callback' => 'test_success', | |
| 'page arguments' => array(), | |
| 'access arguments' => array('access content'), | |
| ); | |
| return $items; | |
| } | |
| /** | |
| * Page used to display the form (all steps). | |
| */ | |
| function test_page() { | |
| return drupal_get_form('test_form'); | |
| } | |
| /** | |
| * Simple confirmation page displayed after finishing the form. | |
| */ | |
| function test_success() { | |
| return 'OK!'; | |
| } | |
| /** | |
| * Form generation. | |
| */ | |
| function test_form($form_state) { | |
| // Get the current step, either from previously posted value, or just set it to 1. | |
| $step = (isset($form_state['storage']['step'])) ? $form_state['storage']['step'] : 1; | |
| switch ($step) { | |
| case 1: | |
| // Custom page title. | |
| drupal_set_title('Step 1'); | |
| // Current step declaration. | |
| $form['step'] = array( | |
| '#type' => 'hidden', | |
| '#value' => 1, | |
| ); | |
| // All the real form fields. | |
| $form['step1_field1'] = array( | |
| '#title' => 'Step 1 Field 1', | |
| '#type' => 'textfield', | |
| '#default_value' => $form_state['storage']['values'][1]['step1_field1'], | |
| ); | |
| // Submit button(s). | |
| // Only 'Next' here, as we are on the first step. | |
| $form['next'] = array( | |
| '#type' => 'submit', | |
| '#value' => 'Next', | |
| ); | |
| // Not important - used only for displaying values posted so far. | |
| $form['posted_values'] = array( | |
| '#type' => 'markup', | |
| '#value' => _test_display_posted_values($form_state), | |
| ); | |
| break; | |
| case 2: | |
| // Custom page title. | |
| drupal_set_title('Step 2'); | |
| // Current step declaration. | |
| $form['step'] = array( | |
| '#type' => 'hidden', | |
| '#value' => 2, | |
| ); | |
| // All the real form fields. | |
| $form['step2_field1'] = array( | |
| '#title' => 'Step 2 Field 1', | |
| '#type' => 'textfield', | |
| '#default_value' => $form_state['storage']['values'][2]['step2_field1'], | |
| ); | |
| // Submit button(s). | |
| // Both 'Prev' and 'Next' on all middle steps. | |
| $form['prev'] = array( | |
| '#type' => 'submit', | |
| '#value' => 'Prev', | |
| ); | |
| $form['next'] = array( | |
| '#type' => 'submit', | |
| '#value' => 'Next', | |
| ); | |
| // Not important - used only for displaying values posted so far. | |
| $form['posted_values'] = array( | |
| '#type' => 'markup', | |
| '#value' => _test_display_posted_values($form_state), | |
| ); | |
| break; | |
| case 3: | |
| // Custom page title. | |
| drupal_set_title('Step 3'); | |
| // Current step declaration. | |
| $form['step'] = array( | |
| '#type' => 'hidden', | |
| '#value' => 3, | |
| ); | |
| // All the real form fields. | |
| $form['step3_field1'] = array( | |
| '#title' => 'Step 3 Field 1', | |
| '#type' => 'textfield', | |
| '#default_value' => $form_state['storage']['values'][3]['step3_field1'], | |
| ); | |
| // Submit button(s). | |
| // 'Prev' and 'Submit' here, as we are on the last step. | |
| $form['prev'] = array( | |
| '#type' => 'submit', | |
| '#value' => 'Prev', | |
| ); | |
| $form['submit'] = array( | |
| '#type' => 'submit', | |
| '#value' => 'Finish', | |
| ); | |
| // Not important - used only for displaying values posted so far. | |
| $form['posted_values'] = array( | |
| '#type' => 'markup', | |
| '#value' => _test_display_posted_values($form_state), | |
| ); | |
| break; | |
| } | |
| return $form; | |
| } | |
| function test_form_submit($form, &$form_state) { | |
| // Save all posted values for $form_state['storage'] | |
| // to make them available in all other steps. | |
| $form_state['storage']['values'][$form_state['values']['step']] = $form_state['values']; | |
| // Deal with current step number based on which submit button was clicked. | |
| switch ($form_state['values']['op']) { | |
| case 'Next': | |
| // Do not finally submit the form yet, rebuild it instead. | |
| $form_state['rebuild'] = TRUE; | |
| // Go to next step. | |
| $form_state['storage']['step'] = ++$form_state['values']['step']; | |
| break; | |
| case 'Prev': | |
| // Do not finally submit the form yet, rebuild it instead. | |
| $form_state['rebuild'] = TRUE; | |
| // Go to previous step. | |
| $form_state['storage']['step'] = --$form_state['values']['step']; | |
| break; | |
| case 'Finish': | |
| // Do something with all form values ($form_state['storage']) before unsetting it. | |
| // ... | |
| // And then unset it. | |
| unset($form_state['storage']); | |
| // This was the final step, so redirect to success page. | |
| $form_state['redirect'] = 'test/success'; | |
| break; | |
| } | |
| } | |
| function _test_display_posted_values($form_state) { | |
| $output = '<p>Posted values:</p>' | |
| . '<p>Step 1 field 1: ' . $form_state['storage']['values'][1]['step1_field1'] . '</p>' | |
| . '<p>Step 2 field 1: ' . $form_state['storage']['values'][2]['step2_field1'] . '</p>' | |
| . '<p>Step 3 field 1: ' . $form_state['storage']['values'][3]['step3_field1'] . '</p>'; | |
| return $output; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment