f27e5e066e790c776d908e05b47b8745e8263cd6
[yaffs-website] / web / core / modules / system / tests / modules / form_test / src / Form / FormTestStoragePageCacheForm.php
1 <?php
2
3 namespace Drupal\form_test\Form;
4
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7
8 class FormTestStoragePageCacheForm extends FormBase {
9
10   /**
11    * {@inheritdoc}
12    */
13   public function getFormId() {
14     return 'form_test_storage_page_cache';
15   }
16
17   /**
18    * {@inheritdoc}
19    */
20   public function buildForm(array $form, FormStateInterface $form_state) {
21     $form['title'] = [
22       '#type' => 'textfield',
23       '#title' => 'Title',
24       '#required' => TRUE,
25     ];
26
27     $form['test_build_id_old'] = [
28       '#type' => 'item',
29       '#title' => 'Old build id',
30       '#markup' => 'No old build id',
31     ];
32
33     $form['submit'] = [
34       '#type' => 'submit',
35       '#value' => 'Save',
36     ];
37
38     $form['rebuild'] = [
39       '#type' => 'submit',
40       '#value' => 'Rebuild',
41       '#submit' => [[$this, 'form_test_storage_page_cache_rebuild']],
42     ];
43
44     $form['#after_build'] = [[$this, 'form_test_storage_page_cache_old_build_id']];
45
46     return $form;
47   }
48
49   /**
50    * Form element #after_build callback: output the old form build-id.
51    */
52   public function form_test_storage_page_cache_old_build_id($form) {
53     if (isset($form['#build_id_old'])) {
54       $form['test_build_id_old']['#plain_text'] = $form['#build_id_old'];
55     }
56     return $form;
57   }
58
59   /**
60    * Form submit callback: Rebuild the form and continue.
61    */
62   public function form_test_storage_page_cache_rebuild($form, FormStateInterface $form_state) {
63     $form_state->setRebuild();
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function validateForm(array &$form, FormStateInterface $form_state) {
70     // Test using form cache when re-displaying a form due to validation
71     // errors.
72     if ($form_state->hasAnyErrors()) {
73       $form_state->setCached();
74     }
75   }
76
77   /**
78    * {@inheritdoc}
79    */
80   public function submitForm(array &$form, FormStateInterface $form_state) {
81     // Nothing must happen.
82   }
83
84 }