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