Version 1
[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       '#ajax' => [
36         'callback' => [$object, 'selectCallback'],
37       ],
38       '#suffix' => '<div id="ajax_selected_color">No color yet selected</div>',
39     ];
40
41     $form['checkbox'] = [
42       '#type' => 'checkbox',
43       '#title' => $this->t('Test checkbox'),
44       '#ajax' => [
45         'callback' => [$object, 'checkboxCallback'],
46       ],
47       '#suffix' => '<div id="ajax_checkbox_value">No action yet</div>',
48     ];
49     $form['submit'] = [
50       '#type' => 'submit',
51       '#value' => $this->t('submit'),
52     ];
53
54     // This is for testing invalid callbacks that should return a 500 error in
55     // \Drupal\Core\Form\FormAjaxResponseBuilderInterface::buildResponse().
56     $invalid_callbacks = [
57       'null' => NULL,
58       'empty' => '',
59       'nonexistent' => 'some_function_that_does_not_exist',
60     ];
61     foreach ($invalid_callbacks as $key => $value) {
62       $form['select_' . $key . '_callback'] = [
63         '#type' => 'select',
64         '#title' => $this->t('Test %key callbacks', ['%key' => $key]),
65         '#options' => ['red' => 'red'],
66         '#ajax' => ['callback' => $value],
67       ];
68     }
69
70     $form['test_group'] = [
71       '#type' => 'details',
72       '#title' => $this->t('Test group'),
73       '#open' => TRUE,
74     ];
75
76     // Test ajax element in a #group.
77     $form['checkbox_in_group_wrapper'] = [
78       '#type' => 'container',
79       '#attributes' => ['id' => 'checkbox-wrapper'],
80       '#group' => 'test_group',
81       'checkbox_in_group' => [
82         '#type' => 'checkbox',
83         '#title' => $this->t('AJAX checkbox in a group'),
84         '#ajax' => [
85           'callback' => [$object, 'checkboxGroupCallback'],
86           'wrapper' => 'checkbox-wrapper',
87         ],
88       ],
89       'nested_group' => [
90         '#type' => 'details',
91         '#title' => $this->t('Nested group'),
92         '#open' => TRUE,
93       ],
94       'checkbox_in_nested' => [
95         '#type' => 'checkbox',
96         '#group' => 'nested_group',
97         '#title' => $this->t('AJAX checkbox in a nested group'),
98         '#ajax' => [
99           'callback' => [$object, 'checkboxGroupCallback'],
100           'wrapper' => 'checkbox-wrapper',
101         ],
102       ],
103     ];
104
105     $form['another_checkbox_in_nested'] = [
106       '#type' => 'checkbox',
107       '#group' => 'nested_group',
108       '#title' => $this->t('Another AJAX checkbox in a nested group'),
109     ];
110
111
112     return $form;
113   }
114
115   /**
116    * {@inheritdoc}
117    */
118   public function submitForm(array &$form, FormStateInterface $form_state) {
119   }
120
121 }