916831e96b503e38d8f3f82c1a1adad526dd3c44
[yaffs-website] / web / core / modules / system / tests / modules / batch_test / src / Form / BatchTestMultiStepForm.php
1 <?php
2
3 namespace Drupal\batch_test\Form;
4
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Generate form of id batch_test_multistep_form.
10  */
11 class BatchTestMultiStepForm extends FormBase {
12
13   /**
14    * {@inheritdoc}
15    */
16   public function getFormId() {
17     return 'batch_test_multistep_form';
18   }
19
20   /**
21    * {@inheritdoc}
22    */
23   public function buildForm(array $form, FormStateInterface $form_state) {
24     $step = $form_state->get('step');
25     if (empty($step)) {
26       $step = 1;
27       $form_state->set('step', $step);
28     }
29
30     $form['step_display'] = [
31       '#markup' => 'step ' . $step . '<br/>',
32     ];
33     $form['submit'] = [
34       '#type' => 'submit',
35       '#value' => 'Submit',
36     ];
37
38     // This is a POST form with multiple steps that does not transition from one
39     // step to the next via POST requests, but via GET requests, because it uses
40     // Batch API to advance through the steps.
41     $form['#cache']['max-age'] = 0;
42
43     return $form;
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   public function submitForm(array &$form, FormStateInterface $form_state) {
50     batch_test_stack(NULL, TRUE);
51
52     $step = $form_state->get('step');
53     switch ($step) {
54       case 1:
55         batch_set(_batch_test_batch_1());
56         break;
57       case 2:
58         batch_set(_batch_test_batch_2());
59         break;
60     }
61
62     if ($step < 2) {
63       $form_state->set('step', ++$step);
64       $form_state->setRebuild();
65     }
66
67     $form_state->setRedirect('batch_test.redirect');
68   }
69
70 }