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',
150 // Try to hijack the email field with a valid email.
151 $form['disabled_container']['disabled_container_email'] = [
154 '#default_value' => 'foo@example.com',
155 '#test_hijack_value' => 'bar@example.com',
158 // Try to hijack the URL field with a valid URL.
159 $form['disabled_container']['disabled_container_url'] = [
162 '#default_value' => 'http://example.com',
163 '#test_hijack_value' => 'http://example.com/foo',
167 $form['text_format'] = [
168 '#type' => 'text_format',
169 '#title' => 'Text format',
171 '#default_value' => 'Text value',
172 '#format' => 'plain_text',
173 '#expected_value' => [
174 'value' => 'Text value',
175 'format' => 'plain_text',
177 '#test_hijack_value' => [
179 'format' => 'filtered_html',
184 $form['password'] = [
185 '#type' => 'password',
186 '#title' => 'Password',
189 $form['password_confirm'] = [
190 '#type' => 'password_confirm',
191 '#title' => 'Password confirm',
201 $form['managed_file'] = [
202 '#type' => 'managed_file',
203 '#title' => 'Managed file',
208 $form['image_button'] = [
209 '#type' => 'image_button',
210 '#value' => 'Image button',
215 '#value' => 'Button',
218 $form['submit_disabled'] = [
220 '#value' => 'Submit',
226 '#value' => t('Submit'),
235 public function submitForm(array &$form, FormStateInterface $form_state) {
236 $form_state->setResponse(new JsonResponse($form_state->getValues()));