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