Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / system / tests / modules / ajax_forms_test / src / Form / AjaxFormsTestSimpleForm.php
1 <?php
2
3 namespace Drupal\ajax_forms_test\Form;
4
5 use Drupal\Core\Form\FormBase;
6 use Drupal\ajax_forms_test\Callbacks;
7 use Drupal\Core\Form\FormStateInterface;
8
9 /**
10  * Form builder: Builds a form that triggers a simple AJAX callback.
11  */
12 class AjaxFormsTestSimpleForm extends FormBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   public function getFormId() {
18     return 'ajax_forms_test_simple_form';
19   }
20
21   /**
22    * {@inheritdoc}
23    */
24   public function buildForm(array $form, FormStateInterface $form_state) {
25     $object = new Callbacks();
26
27     $form = [];
28     $form['select'] = [
29       '#title' => $this->t('Color'),
30       '#type' => 'select',
31       '#options' => [
32         'red' => 'red',
33         'green' => 'green',
34         'blue' => 'blue',
35       ],
36       '#ajax' => [
37         'callback' => [$object, 'selectCallback'],
38       ],
39       '#suffix' => '<div id="ajax_selected_color">No color yet selected</div>',
40     ];
41
42     $form['checkbox'] = [
43       '#type' => 'checkbox',
44       '#title' => $this->t('Test checkbox'),
45       '#ajax' => [
46         'callback' => [$object, 'checkboxCallback'],
47       ],
48       '#suffix' => '<div id="ajax_checkbox_value">No action yet</div>',
49     ];
50     $form['submit'] = [
51       '#type' => 'submit',
52       '#value' => $this->t('submit'),
53     ];
54
55     // This is for testing invalid callbacks that should return a 500 error in
56     // \Drupal\Core\Form\FormAjaxResponseBuilderInterface::buildResponse().
57     $invalid_callbacks = [
58       'null' => NULL,
59       'empty' => '',
60       'nonexistent' => 'some_function_that_does_not_exist',
61     ];
62     foreach ($invalid_callbacks as $key => $value) {
63       $form['select_' . $key . '_callback'] = [
64         '#type' => 'select',
65         '#title' => $this->t('Test %key callbacks', ['%key' => $key]),
66         '#options' => ['red' => 'red'],
67         '#ajax' => ['callback' => $value],
68       ];
69     }
70
71     $form['test_group'] = [
72       '#type' => 'details',
73       '#title' => $this->t('Test group'),
74       '#open' => TRUE,
75     ];
76
77     // Test ajax element in a #group.
78     $form['checkbox_in_group_wrapper'] = [
79       '#type' => 'container',
80       '#attributes' => ['id' => 'checkbox-wrapper'],
81       '#group' => 'test_group',
82       'checkbox_in_group' => [
83         '#type' => 'checkbox',
84         '#title' => $this->t('AJAX checkbox in a group'),
85         '#ajax' => [
86           'callback' => [$object, 'checkboxGroupCallback'],
87           'wrapper' => 'checkbox-wrapper',
88         ],
89       ],
90       'nested_group' => [
91         '#type' => 'details',
92         '#title' => $this->t('Nested group'),
93         '#open' => TRUE,
94       ],
95       'checkbox_in_nested' => [
96         '#type' => 'checkbox',
97         '#group' => 'nested_group',
98         '#title' => $this->t('AJAX checkbox in a nested group'),
99         '#ajax' => [
100           'callback' => [$object, 'checkboxGroupCallback'],
101           'wrapper' => 'checkbox-wrapper',
102         ],
103       ],
104     ];
105
106     $form['another_checkbox_in_nested'] = [
107       '#type' => 'checkbox',
108       '#group' => 'nested_group',
109       '#title' => $this->t('Another AJAX checkbox in a nested group'),
110     ];
111
112     return $form;
113   }
114
115   /**
116    * {@inheritdoc}
117    */
118   public function submitForm(array &$form, FormStateInterface $form_state) {
119   }
120
121 }