3 namespace Drupal\form_test\Form;
5 use Drupal\Core\Datetime\DrupalDateTime;
6 use Drupal\Core\Form\FormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Symfony\Component\HttpFoundation\JsonResponse;
11 * Builds a form to test disabled elements.
13 class FormTestDisabledElementsForm extends FormBase {
18 public function getFormId() {
19 return '_form_test_disabled_elements';
25 public function buildForm(array $form, FormStateInterface $form_state) {
26 // Elements that take a simple default value.
27 foreach (['textfield', 'textarea', 'search', 'tel', 'hidden'] as $type) {
31 '#default_value' => $type,
32 '#test_hijack_value' => 'HIJACK',
37 // Multiple values option elements.
38 foreach (['checkboxes', 'select'] as $type) {
39 $form[$type . '_multiple'] = [
41 '#title' => $type . ' (multiple)',
47 '#default_value' => ['test_2' => 'test_2'],
48 // The keys of #test_hijack_value need to match the #name of the control.
49 // @see FormsTestCase::testDisabledElements()
50 '#test_hijack_value' => $type == 'select' ? ['' => 'test_1'] : ['test_1' => 'test_1'],
55 // Single values option elements.
56 foreach (['radios', 'select'] as $type) {
57 $form[$type . '_single'] = [
59 '#title' => $type . ' (single)',
65 '#default_value' => 'test_2',
66 '#test_hijack_value' => 'test_1',
71 // Checkbox and radio.
72 foreach (['checkbox', 'radio'] as $type) {
73 $form[$type . '_unchecked'] = [
75 '#title' => $type . ' (unchecked)',
77 '#default_value' => 0,
78 '#test_hijack_value' => 1,
81 $form[$type . '_checked'] = [
83 '#title' => $type . ' (checked)',
85 '#default_value' => 1,
86 '#test_hijack_value' => NULL,
91 // Weight, number, range.
92 foreach (['weight', 'number', 'range'] as $type) {
96 '#default_value' => 10,
97 '#test_hijack_value' => 5,
106 '#default_value' => '#0000ff',
107 '#test_hijack_value' => '#ff0000',
111 // The #disabled state should propagate to children.
112 $form['disabled_container'] = [
115 foreach (['textfield', 'textarea', 'hidden', 'tel', 'url'] as $type) {
116 $form['disabled_container']['disabled_container_' . $type] = [
119 '#default_value' => $type,
120 '#test_hijack_value' => 'HIJACK',
125 $date = new DrupalDateTime('1978-11-01 10:30:00', 'Europe/Berlin');
126 // Starting with PHP 5.4.30, 5.5.15, JSON encoded DateTime objects include
127 // microseconds. Make sure that the expected value is correct for all
128 // versions by encoding and decoding it again instead of hardcoding it.
129 // See https://github.com/php/php-src/commit/fdb2709dd27c5987c2d2c8aaf0cdbebf9f17f643
130 $expected = json_decode(json_encode($date), TRUE);
131 $form['disabled_container']['disabled_container_datetime'] = [
132 '#type' => 'datetime',
133 '#title' => 'datetime',
134 '#default_value' => $date,
135 '#expected_value' => $expected,
136 '#test_hijack_value' => new DrupalDateTime('1978-12-02 11:30:00', 'Europe/Berlin'),
137 '#date_timezone' => 'Europe/Berlin',
140 $form['disabled_container']['disabled_container_date'] = [
143 '#default_value' => '2001-01-13',
144 '#expected_value' => '2001-01-13',
145 '#test_hijack_value' => '2013-01-01',
146 '#date_timezone' => 'Europe/Berlin',
149 // Try to hijack the email field with a valid email.
150 $form['disabled_container']['disabled_container_email'] = [
153 '#default_value' => 'foo@example.com',
154 '#test_hijack_value' => 'bar@example.com',
157 // Try to hijack the URL field with a valid URL.
158 $form['disabled_container']['disabled_container_url'] = [
161 '#default_value' => 'http://example.com',
162 '#test_hijack_value' => 'http://example.com/foo',
166 $form['text_format'] = [
167 '#type' => 'text_format',
168 '#title' => 'Text format',
170 '#default_value' => 'Text value',
171 '#format' => 'plain_text',
172 '#expected_value' => [
173 'value' => 'Text value',
174 'format' => 'plain_text',
176 '#test_hijack_value' => [
178 'format' => 'filtered_html',
183 $form['password'] = [
184 '#type' => 'password',
185 '#title' => 'Password',
188 $form['password_confirm'] = [
189 '#type' => 'password_confirm',
190 '#title' => 'Password confirm',
200 $form['managed_file'] = [
201 '#type' => 'managed_file',
202 '#title' => 'Managed file',
207 $form['image_button'] = [
208 '#type' => 'image_button',
209 '#value' => 'Image button',
214 '#value' => 'Button',
217 $form['submit_disabled'] = [
219 '#value' => 'Submit',
225 '#value' => t('Submit'),
234 public function submitForm(array &$form, FormStateInterface $form_state) {
235 $form_state->setResponse(new JsonResponse($form_state->getValues()));