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