Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / system / tests / modules / form_test / src / Form / FormTestCheckboxesRadiosForm.php
1 <?php
2
3 namespace Drupal\form_test\Form;
4
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Symfony\Component\HttpFoundation\JsonResponse;
8
9 /**
10  * Form constructor to test expansion of #type checkboxes and radios.
11  *
12  * @internal
13  */
14 class FormTestCheckboxesRadiosForm extends FormBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   public function getFormId() {
20     return 'form_test_checkboxes_radios';
21   }
22
23   /**
24    * {@inheritdoc}
25    */
26   public function buildForm(array $form, FormStateInterface $form_state, $customize = FALSE) {
27     // Expand #type checkboxes, setting custom element properties for some but not
28     // all options.
29     $form['checkboxes'] = [
30       '#type' => 'checkboxes',
31       '#title' => 'Checkboxes',
32       '#options' => [
33         0 => 'Zero',
34         'foo' => 'Foo',
35         1 => 'One',
36         'bar' => $this->t('<em>Bar - checkboxes</em>'),
37         '>' => "<em>Special Char</em><script>alert('checkboxes');</script>",
38       ],
39     ];
40     if ($customize) {
41       $form['checkboxes'] += [
42         'foo' => [
43           '#description' => 'Enable to foo.',
44         ],
45         1 => [
46           '#weight' => 10,
47         ],
48       ];
49     }
50
51     // Expand #type radios, setting custom element properties for some but not
52     // all options.
53     $form['radios'] = [
54       '#type' => 'radios',
55       '#title' => 'Radios',
56       '#options' => [
57         0 => 'Zero',
58         'foo' => 'Foo',
59         1 => 'One',
60         'bar' => '<em>Bar - radios</em>',
61         '>' => "<em>Special Char</em><script>alert('radios');</script>",
62       ],
63     ];
64     if ($customize) {
65       $form['radios'] += [
66         'foo' => [
67           '#description' => 'Enable to foo.',
68         ],
69         1 => [
70           '#weight' => 10,
71         ],
72       ];
73     }
74
75     $form['submit'] = ['#type' => 'submit', '#value' => 'Submit'];
76
77     return $form;
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public function submitForm(array &$form, FormStateInterface $form_state) {
84     $form_state->setResponse(new JsonResponse($form_state->getValues()));
85   }
86
87 }