Version 1
[yaffs-website] / web / core / modules / system / tests / modules / ajax_forms_test / src / Form / AjaxFormsTestCommandsForm.php
1 <?php
2
3 namespace Drupal\ajax_forms_test\Form;
4
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Form constructor for the Ajax Command display form.
10  */
11 class AjaxFormsTestCommandsForm extends FormBase {
12
13   /**
14    * {@inheritdoc}.
15    */
16   public function getFormId() {
17     return 'ajax_forms_test_ajax_commands_form';
18   }
19
20   /**
21    * {@inheritdoc}.
22    */
23   public function buildForm(array $form, FormStateInterface $form_state) {
24     $form = [];
25
26     // Shows the 'after' command with a callback generating commands.
27     $form['after_command_example'] = [
28       '#value' => $this->t("AJAX 'After': Click to put something after the div"),
29       '#type' => 'submit',
30       '#ajax' => [
31         'callback' => 'ajax_forms_test_advanced_commands_after_callback',
32       ],
33       '#suffix' => '<div id="after_div">Something can be inserted after this</div>',
34     ];
35
36     // Shows the 'alert' command.
37     $form['alert_command_example'] = [
38       '#value' => $this->t("AJAX 'Alert': Click to alert"),
39       '#type' => 'submit',
40       '#ajax' => [
41         'callback' => 'ajax_forms_test_advanced_commands_alert_callback',
42       ],
43     ];
44
45     // Shows the 'append' command.
46     $form['append_command_example'] = [
47       '#value' => $this->t("AJAX 'Append': Click to append something"),
48       '#type' => 'submit',
49       '#ajax' => [
50         'callback' => 'ajax_forms_test_advanced_commands_append_callback',
51       ],
52       '#suffix' => '<div id="append_div">Append inside this div</div>',
53     ];
54
55
56     // Shows the 'before' command.
57     $form['before_command_example'] = [
58       '#value' => $this->t("AJAX 'before': Click to put something before the div"),
59       '#type' => 'submit',
60       '#ajax' => [
61         'callback' => 'ajax_forms_test_advanced_commands_before_callback',
62       ],
63       '#suffix' => '<div id="before_div">Insert something before this.</div>',
64     ];
65
66     // Shows the 'changed' command without asterisk.
67     $form['changed_command_example'] = [
68       '#value' => $this->t("AJAX changed: Click to mark div changed."),
69       '#type' => 'submit',
70       '#ajax' => [
71         'callback' => 'ajax_forms_test_advanced_commands_changed_callback',
72       ],
73       '#suffix' => '<div id="changed_div"> <div id="changed_div_mark_this">This div can be marked as changed or not.</div></div>',
74     ];
75     // Shows the 'changed' command adding the asterisk.
76     $form['changed_command_asterisk_example'] = [
77       '#value' => $this->t("AJAX changed: Click to mark div changed with asterisk."),
78       '#type' => 'submit',
79       '#ajax' => [
80         'callback' => 'ajax_forms_test_advanced_commands_changed_asterisk_callback',
81       ],
82     ];
83
84     // Shows the Ajax 'css' command.
85     $form['css_command_example'] = [
86       '#value' => $this->t("Set the '#box' div to be blue."),
87       '#type' => 'submit',
88       '#ajax' => [
89         'callback' => 'ajax_forms_test_advanced_commands_css_callback',
90       ],
91       '#suffix' => '<div id="css_div" style="height: 50px; width: 50px; border: 1px solid black"> box</div>',
92     ];
93
94
95     // Shows the Ajax 'data' command. But there is no use of this information,
96     // as this would require a javascript client to use the data.
97     $form['data_command_example'] = [
98       '#value' => $this->t("AJAX data command: Issue command."),
99       '#type' => 'submit',
100       '#ajax' => [
101         'callback' => 'ajax_forms_test_advanced_commands_data_callback',
102       ],
103       '#suffix' => '<div id="data_div">Data attached to this div.</div>',
104     ];
105
106     // Shows the Ajax 'invoke' command.
107     $form['invoke_command_example'] = [
108       '#value' => $this->t("AJAX invoke command: Invoke addClass() method."),
109       '#type' => 'submit',
110       '#ajax' => [
111         'callback' => 'ajax_forms_test_advanced_commands_invoke_callback',
112       ],
113       '#suffix' => '<div id="invoke_div">Original contents</div>',
114     ];
115
116     // Shows the Ajax 'html' command.
117     $form['html_command_example'] = [
118       '#value' => $this->t("AJAX html: Replace the HTML in a selector."),
119       '#type' => 'submit',
120       '#ajax' => [
121         'callback' => 'ajax_forms_test_advanced_commands_html_callback',
122       ],
123       '#suffix' => '<div id="html_div">Original contents</div>',
124     ];
125
126     // Shows the Ajax 'insert' command.
127     $form['insert_command_example'] = [
128       '#value' => $this->t("AJAX insert: Let client insert based on #ajax['method']."),
129       '#type' => 'submit',
130       '#ajax' => [
131         'callback' => 'ajax_forms_test_advanced_commands_insert_callback',
132         'method' => 'prepend',
133       ],
134       '#suffix' => '<div id="insert_div">Original contents</div>',
135     ];
136
137     // Shows the Ajax 'prepend' command.
138     $form['prepend_command_example'] = [
139       '#value' => $this->t("AJAX 'prepend': Click to prepend something"),
140       '#type' => 'submit',
141       '#ajax' => [
142         'callback' => 'ajax_forms_test_advanced_commands_prepend_callback',
143       ],
144       '#suffix' => '<div id="prepend_div">Something will be prepended to this div. </div>',
145     ];
146
147     // Shows the Ajax 'remove' command.
148     $form['remove_command_example'] = [
149       '#value' => $this->t("AJAX 'remove': Click to remove text"),
150       '#type' => 'submit',
151       '#ajax' => [
152         'callback' => 'ajax_forms_test_advanced_commands_remove_callback',
153       ],
154       '#suffix' => '<div id="remove_div"><div id="remove_text">text to be removed</div></div>',
155     ];
156
157     // Shows the Ajax 'restripe' command.
158     $form['restripe_command_example'] = [
159       '#type' => 'submit',
160       '#value' => $this->t("AJAX 'restripe' command"),
161       '#ajax' => [
162         'callback' => 'ajax_forms_test_advanced_commands_restripe_callback',
163       ],
164       '#suffix' => '<div id="restripe_div">
165                     <table id="restripe_table" style="border: 1px solid black" >
166                     <tr id="table-first"><td>first row</td></tr>
167                     <tr ><td>second row</td></tr>
168                     </table>
169                     </div>',
170     ];
171
172     // Demonstrates the Ajax 'settings' command. The 'settings' command has
173     // nothing visual to "show", but it can be tested via SimpleTest and via
174     // Firebug.
175     $form['settings_command_example'] = [
176       '#type' => 'submit',
177       '#value' => $this->t("AJAX 'settings' command"),
178       '#ajax' => [
179         'callback' => 'ajax_forms_test_advanced_commands_settings_callback',
180       ],
181     ];
182
183     // Shows the Ajax 'add_css' command.
184     $form['add_css_command_example'] = [
185       '#type' => 'submit',
186       '#value' => $this->t("AJAX 'add_css' command"),
187       '#ajax' => [
188         'callback' => 'ajax_forms_test_advanced_commands_add_css_callback',
189       ],
190     ];
191
192     $form['submit'] = [
193       '#type' => 'submit',
194       '#value' => $this->t('Submit'),
195     ];
196
197     return $form;
198   }
199
200   /**
201    * {@inheritdoc}.
202    */
203   public function submitForm(array &$form, FormStateInterface $form_state) {
204   }
205
206 }