Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / system / tests / modules / form_test / src / Form / FormTestDisabledElementsForm.php
1 <?php
2
3 namespace Drupal\form_test\Form;
4
5 use Drupal\Core\Datetime\DrupalDateTime;
6 use Drupal\Core\Form\FormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Symfony\Component\HttpFoundation\JsonResponse;
9
10 /**
11  * Builds a form to test disabled elements.
12  *
13  * @internal
14  */
15 class FormTestDisabledElementsForm extends FormBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public function getFormId() {
21     return '_form_test_disabled_elements';
22   }
23
24   /**
25    * {@inheritdoc}
26    */
27   public function buildForm(array $form, FormStateInterface $form_state) {
28     // Elements that take a simple default value.
29     foreach (['textfield', 'textarea', 'search', 'tel', 'hidden'] as $type) {
30       $form[$type] = [
31         '#type' => $type,
32         '#title' => $type,
33         '#default_value' => $type,
34         '#test_hijack_value' => 'HIJACK',
35         '#disabled' => TRUE,
36       ];
37     }
38
39     // Multiple values option elements.
40     foreach (['checkboxes', 'select'] as $type) {
41       $form[$type . '_multiple'] = [
42         '#type' => $type,
43         '#title' => $type . ' (multiple)',
44         '#options' => [
45           'test_1' => 'Test 1',
46           'test_2' => 'Test 2',
47         ],
48         '#multiple' => TRUE,
49         '#default_value' => ['test_2' => 'test_2'],
50         // The keys of #test_hijack_value need to match the #name of the control.
51         // @see FormsTestCase::testDisabledElements()
52         '#test_hijack_value' => $type == 'select' ? ['' => 'test_1'] : ['test_1' => 'test_1'],
53         '#disabled' => TRUE,
54       ];
55     }
56
57     // Single values option elements.
58     foreach (['radios', 'select'] as $type) {
59       $form[$type . '_single'] = [
60         '#type' => $type,
61         '#title' => $type . ' (single)',
62         '#options' => [
63           'test_1' => 'Test 1',
64           'test_2' => 'Test 2',
65         ],
66         '#multiple' => FALSE,
67         '#default_value' => 'test_2',
68         '#test_hijack_value' => 'test_1',
69         '#disabled' => TRUE,
70       ];
71     }
72
73     // Checkbox and radio.
74     foreach (['checkbox', 'radio'] as $type) {
75       $form[$type . '_unchecked'] = [
76         '#type' => $type,
77         '#title' => $type . ' (unchecked)',
78         '#return_value' => 1,
79         '#default_value' => 0,
80         '#test_hijack_value' => 1,
81         '#disabled' => TRUE,
82       ];
83       $form[$type . '_checked'] = [
84         '#type' => $type,
85         '#title' => $type . ' (checked)',
86         '#return_value' => 1,
87         '#default_value' => 1,
88         '#test_hijack_value' => NULL,
89         '#disabled' => TRUE,
90       ];
91     }
92
93     // Weight, number, range.
94     foreach (['weight', 'number', 'range'] as $type) {
95       $form[$type] = [
96         '#type' => $type,
97         '#title' => $type,
98         '#default_value' => 10,
99         '#test_hijack_value' => 5,
100         '#disabled' => TRUE,
101       ];
102     }
103
104     // Color.
105     $form['color'] = [
106       '#type' => 'color',
107       '#title' => 'color',
108       '#default_value' => '#0000ff',
109       '#test_hijack_value' => '#ff0000',
110       '#disabled' => TRUE,
111     ];
112
113     // The #disabled state should propagate to children.
114     $form['disabled_container'] = [
115       '#disabled' => TRUE,
116     ];
117     foreach (['textfield', 'textarea', 'hidden', 'tel', 'url'] as $type) {
118       $form['disabled_container']['disabled_container_' . $type] = [
119         '#type' => $type,
120         '#title' => $type,
121         '#default_value' => $type,
122         '#test_hijack_value' => 'HIJACK',
123       ];
124     }
125
126     // Date.
127     $date = new DrupalDateTime('1978-11-01 10:30:00', 'Europe/Berlin');
128     // Starting with PHP 5.4.30, 5.5.15, JSON encoded DateTime objects include
129     // microseconds. Make sure that the expected value is correct for all
130     // versions by encoding and decoding it again instead of hardcoding it.
131     // See https://github.com/php/php-src/commit/fdb2709dd27c5987c2d2c8aaf0cdbebf9f17f643
132     $expected = json_decode(json_encode($date), TRUE);
133     $form['disabled_container']['disabled_container_datetime'] = [
134       '#type' => 'datetime',
135       '#title' => 'datetime',
136       '#default_value' => $date,
137       '#expected_value' => $expected,
138       '#test_hijack_value' => new DrupalDateTime('1978-12-02 11:30:00', 'Europe/Berlin'),
139       '#date_timezone' => 'Europe/Berlin',
140     ];
141
142     $form['disabled_container']['disabled_container_date'] = [
143       '#type' => 'date',
144       '#title' => 'date',
145       '#default_value' => '2001-01-13',
146       '#expected_value' => '2001-01-13',
147       '#test_hijack_value' => '2013-01-01',
148       '#date_timezone' => 'Europe/Berlin',
149     ];
150
151     // Try to hijack the email field with a valid email.
152     $form['disabled_container']['disabled_container_email'] = [
153       '#type' => 'email',
154       '#title' => 'email',
155       '#default_value' => 'foo@example.com',
156       '#test_hijack_value' => 'bar@example.com',
157     ];
158
159     // Try to hijack the URL field with a valid URL.
160     $form['disabled_container']['disabled_container_url'] = [
161       '#type' => 'url',
162       '#title' => 'url',
163       '#default_value' => 'http://example.com',
164       '#test_hijack_value' => 'http://example.com/foo',
165     ];
166
167     // Text format.
168     $form['text_format'] = [
169       '#type' => 'text_format',
170       '#title' => 'Text format',
171       '#disabled' => TRUE,
172       '#default_value' => 'Text value',
173       '#format' => 'plain_text',
174       '#expected_value' => [
175         'value' => 'Text value',
176         'format' => 'plain_text',
177       ],
178       '#test_hijack_value' => [
179         'value' => 'HIJACK',
180         'format' => 'filtered_html',
181       ],
182     ];
183
184     // Password fields.
185     $form['password'] = [
186       '#type' => 'password',
187       '#title' => 'Password',
188       '#disabled' => TRUE,
189     ];
190     $form['password_confirm'] = [
191       '#type' => 'password_confirm',
192       '#title' => 'Password confirm',
193       '#disabled' => TRUE,
194     ];
195
196     // Files.
197     $form['file'] = [
198       '#type' => 'file',
199       '#title' => 'File',
200       '#disabled' => TRUE,
201     ];
202     $form['managed_file'] = [
203       '#type' => 'managed_file',
204       '#title' => 'Managed file',
205       '#disabled' => TRUE,
206     ];
207
208     // Buttons.
209     $form['image_button'] = [
210       '#type' => 'image_button',
211       '#value' => 'Image button',
212       '#disabled' => TRUE,
213     ];
214     $form['button'] = [
215       '#type' => 'button',
216       '#value' => 'Button',
217       '#disabled' => TRUE,
218     ];
219     $form['submit_disabled'] = [
220       '#type' => 'submit',
221       '#value' => 'Submit',
222       '#disabled' => TRUE,
223     ];
224
225     $form['submit'] = [
226       '#type' => 'submit',
227       '#value' => t('Submit'),
228     ];
229
230     return $form;
231   }
232
233   /**
234    * {@inheritdoc}
235    */
236   public function submitForm(array &$form, FormStateInterface $form_state) {
237     $form_state->setResponse(new JsonResponse($form_state->getValues()));
238   }
239
240 }