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.
15 class FormTestDisabledElementsForm extends FormBase {
20 public function getFormId() {
21 return '_form_test_disabled_elements';
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) {
33 '#default_value' => $type,
34 '#test_hijack_value' => 'HIJACK',
39 // Multiple values option elements.
40 foreach (['checkboxes', 'select'] as $type) {
41 $form[$type . '_multiple'] = [
43 '#title' => $type . ' (multiple)',
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'],
57 // Single values option elements.
58 foreach (['radios', 'select'] as $type) {
59 $form[$type . '_single'] = [
61 '#title' => $type . ' (single)',
67 '#default_value' => 'test_2',
68 '#test_hijack_value' => 'test_1',
73 // Checkbox and radio.
74 foreach (['checkbox', 'radio'] as $type) {
75 $form[$type . '_unchecked'] = [
77 '#title' => $type . ' (unchecked)',
79 '#default_value' => 0,
80 '#test_hijack_value' => 1,
83 $form[$type . '_checked'] = [
85 '#title' => $type . ' (checked)',
87 '#default_value' => 1,
88 '#test_hijack_value' => NULL,
93 // Weight, number, range.
94 foreach (['weight', 'number', 'range'] as $type) {
98 '#default_value' => 10,
99 '#test_hijack_value' => 5,
108 '#default_value' => '#0000ff',
109 '#test_hijack_value' => '#ff0000',
113 // The #disabled state should propagate to children.
114 $form['disabled_container'] = [
117 foreach (['textfield', 'textarea', 'hidden', 'tel', 'url'] as $type) {
118 $form['disabled_container']['disabled_container_' . $type] = [
121 '#default_value' => $type,
122 '#test_hijack_value' => 'HIJACK',
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',
142 $form['disabled_container']['disabled_container_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',
151 // Try to hijack the email field with a valid email.
152 $form['disabled_container']['disabled_container_email'] = [
155 '#default_value' => 'foo@example.com',
156 '#test_hijack_value' => 'bar@example.com',
159 // Try to hijack the URL field with a valid URL.
160 $form['disabled_container']['disabled_container_url'] = [
163 '#default_value' => 'http://example.com',
164 '#test_hijack_value' => 'http://example.com/foo',
168 $form['text_format'] = [
169 '#type' => 'text_format',
170 '#title' => 'Text format',
172 '#default_value' => 'Text value',
173 '#format' => 'plain_text',
174 '#expected_value' => [
175 'value' => 'Text value',
176 'format' => 'plain_text',
178 '#test_hijack_value' => [
180 'format' => 'filtered_html',
185 $form['password'] = [
186 '#type' => 'password',
187 '#title' => 'Password',
190 $form['password_confirm'] = [
191 '#type' => 'password_confirm',
192 '#title' => 'Password confirm',
202 $form['managed_file'] = [
203 '#type' => 'managed_file',
204 '#title' => 'Managed file',
209 $form['image_button'] = [
210 '#type' => 'image_button',
211 '#value' => 'Image button',
216 '#value' => 'Button',
219 $form['submit_disabled'] = [
221 '#value' => 'Submit',
227 '#value' => t('Submit'),
236 public function submitForm(array &$form, FormStateInterface $form_state) {
237 $form_state->setResponse(new JsonResponse($form_state->getValues()));