Version 1
[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 class FormTestCheckboxesRadiosForm extends FormBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   public function getFormId() {
18     return 'form_test_checkboxes_radios';
19   }
20
21   /**
22    * {@inheritdoc}
23    */
24   public function buildForm(array $form, FormStateInterface $form_state, $customize = FALSE) {
25     // Expand #type checkboxes, setting custom element properties for some but not
26     // all options.
27     $form['checkboxes'] = [
28       '#type' => 'checkboxes',
29       '#title' => 'Checkboxes',
30       '#options' => [
31         0 => 'Zero',
32         'foo' => 'Foo',
33         1 => 'One',
34         'bar' => $this->t('<em>Bar - checkboxes</em>'),
35         '>' => "<em>Special Char</em><script>alert('checkboxes');</script>",
36       ],
37     ];
38     if ($customize) {
39       $form['checkboxes'] += [
40         'foo' => [
41           '#description' => 'Enable to foo.',
42         ],
43         1 => [
44           '#weight' => 10,
45         ],
46       ];
47     }
48
49     // Expand #type radios, setting custom element properties for some but not
50     // all options.
51     $form['radios'] = [
52       '#type' => 'radios',
53       '#title' => 'Radios',
54       '#options' => [
55         0 => 'Zero',
56         'foo' => 'Foo',
57         1 => 'One',
58         'bar' => '<em>Bar - radios</em>',
59         '>' => "<em>Special Char</em><script>alert('radios');</script>",
60       ],
61     ];
62     if ($customize) {
63       $form['radios'] += [
64         'foo' => [
65           '#description' => 'Enable to foo.',
66         ],
67         1 => [
68           '#weight' => 10,
69         ],
70       ];
71     }
72
73     $form['submit'] = ['#type' => 'submit', '#value' => 'Submit'];
74
75     return $form;
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public function submitForm(array &$form, FormStateInterface $form_state) {
82     $form_state->setResponse(new JsonResponse($form_state->getValues()));
83   }
84
85 }