Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / system / tests / modules / form_test / src / Form / FormTestStorageForm.php
1 <?php
2
3 namespace Drupal\form_test\Form;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Form\FormBase;
7 use Drupal\Core\Form\FormStateInterface;
8
9 /**
10  * A multistep form for testing the form storage.
11  *
12  * It uses two steps for editing a virtual "thing". Any changes to it are saved
13  * in the form storage and have to be present during any step. By setting the
14  * request parameter "cache" the form can be tested with caching enabled, as
15  * it would be the case, if the form would contain some #ajax callbacks.
16  */
17 class FormTestStorageForm extends FormBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   public function getFormId() {
23     return 'form_test_storage_form';
24   }
25
26   /**
27    * {@inheritdoc}
28    */
29   public function buildForm(array $form, FormStateInterface $form_state) {
30     if ($form_state->isRebuilding()) {
31       $form_state->setUserInput([]);
32     }
33     // Initialize
34     $storage = $form_state->getStorage();
35     if (empty($storage)) {
36       $user_input = $form_state->getUserInput();
37       if (empty($user_input)) {
38         $_SESSION['constructions'] = 0;
39       }
40       // Put the initial thing into the storage
41       $storage = [
42         'thing' => [
43           'title' => 'none',
44           'value' => '',
45         ],
46       ];
47       $form_state->setStorage($storage);
48     }
49     // Count how often the form is constructed.
50     $_SESSION['constructions']++;
51     drupal_set_message("Form constructions: " . $_SESSION['constructions']);
52
53     $form['title'] = [
54       '#type' => 'textfield',
55       '#title' => 'Title',
56       '#default_value' => $storage['thing']['title'],
57       '#required' => TRUE,
58     ];
59     $form['value'] = [
60       '#type' => 'textfield',
61       '#title' => 'Value',
62       '#default_value' => $storage['thing']['value'],
63       '#element_validate' => ['::elementValidateValueCached'],
64     ];
65     $form['continue_button'] = [
66       '#type' => 'button',
67       '#value' => 'Reset',
68       // Rebuilds the form without keeping the values.
69     ];
70     $form['continue_submit'] = [
71       '#type' => 'submit',
72       '#value' => 'Continue submit',
73       '#submit' => ['::continueSubmitForm'],
74     ];
75     $form['submit'] = [
76       '#type' => 'submit',
77       '#value' => 'Save',
78     ];
79
80     // @todo Remove this in https://www.drupal.org/node/2524408, because form
81     //   cache immutability is no longer necessary, because we no longer cache
82     //   forms during safe HTTP methods. In the meantime, because
83     //   Drupal\system\Tests\Form still has test coverage for a poisoned form
84     //   cache following a GET request, trick $form_state into caching the form
85     //   to keep that test working until we either remove it or change it in
86     //   that issue.
87     if ($this->getRequest()->get('immutable')) {
88       $form_state->addBuildInfo('immutable', TRUE);
89       if ($this->getRequest()->get('cache') && $this->getRequest()->isMethodCacheable()) {
90         $form_state->setRequestMethod('FAKE');
91         $form_state->setCached();
92       }
93     }
94
95     return $form;
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public function validateForm(array &$form, FormStateInterface $form_state) {
102     if ($this->getRequest()->get('cache')) {
103       // Manually activate caching, so we can test that the storage keeps working
104       // when it's enabled.
105       $form_state->setCached();
106     }
107   }
108
109   /**
110    * Form element validation handler for 'value' element.
111    *
112    * Tests updating of cached form storage during validation.
113    */
114   public function elementValidateValueCached($element, FormStateInterface $form_state) {
115     // If caching is enabled and we receive a certain value, change the storage.
116     // This presumes that another submitted form value triggers a validation error
117     // elsewhere in the form. Form API should still update the cached form storage
118     // though.
119     if ($this->getRequest()->get('cache') && $form_state->getValue('value') == 'change_title') {
120       $form_state->set(['thing', 'changed'], TRUE);
121     }
122   }
123
124   /**
125    * {@inheritdoc}
126    */
127   public function continueSubmitForm(array &$form, FormStateInterface $form_state) {
128     $form_state->set(['thing', 'title'], $form_state->getValue('title'));
129     $form_state->set(['thing', 'value'], $form_state->getValue('value'));
130     $form_state->setRebuild();
131   }
132
133   /**
134    * {@inheritdoc}
135    */
136   public function submitForm(array &$form, FormStateInterface $form_state) {
137     drupal_set_message("Title: " . Html::escape($form_state->getValue('title')));
138     drupal_set_message("Form constructions: " . $_SESSION['constructions']);
139     if ($form_state->has(['thing', 'changed'])) {
140       drupal_set_message("The thing has been changed.");
141     }
142     $form_state->setRedirect('<front>');
143   }
144
145 }