summaryrefslogtreecommitdiffstats
path: root/core/modules/system/tests/modules/ajax_forms_test/ajax_forms_test.module
blob: 262572be9a7d980500cbf099d93b2bd1b8a9e736 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php

/**
 * @file
 * Simpletest mock module for Ajax forms testing.
 */

use Drupal\Core\Ajax\AddCssCommand;
use Drupal\Core\Ajax\AfterCommand;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\AlertCommand;
use Drupal\Core\Ajax\AppendCommand;
use Drupal\Core\Ajax\BeforeCommand;
use Drupal\Core\Ajax\ChangedCommand;
use Drupal\Core\Ajax\CssCommand;
use Drupal\Core\Ajax\DataCommand;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Ajax\InsertCommand;
use Drupal\Core\Ajax\InvokeCommand;
use Drupal\Core\Ajax\PrependCommand;
use Drupal\Core\Ajax\RemoveCommand;
use Drupal\Core\Ajax\RestripeCommand;
use Drupal\Core\Ajax\SettingsCommand;
use Drupal\Core\Form\FormStateInterface;

/**
 * Ajax form callback: Selects 'after'.
 */
function ajax_forms_test_advanced_commands_after_callback($form, FormStateInterface $form_state) {
  $selector = '#after_div';

  $response = new AjaxResponse();
  $response->addCommand(new AfterCommand($selector, "This will be placed after"));
  return $response;
}

/**
 * Ajax form callback: Selects 'alert'.
 */
function ajax_forms_test_advanced_commands_alert_callback($form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $response->addCommand(new AlertCommand('Alert'));
  return $response;
}

/**
 * Ajax form callback: Selects 'append'.
 */
function ajax_forms_test_advanced_commands_append_callback($form, FormStateInterface $form_state) {
  $selector = '#append_div';
  $response = new AjaxResponse();
  $response->addCommand(new AppendCommand($selector, "Appended text"));
  return $response;
}

/**
 * Ajax form callback: Selects 'before'.
 */
function ajax_forms_test_advanced_commands_before_callback($form, FormStateInterface $form_state) {
  $selector = '#before_div';
  $response = new AjaxResponse();
  $response->addCommand(new BeforeCommand($selector, "Before text"));
  return $response;
}

/**
 * Ajax form callback: Selects 'changed'.
 */
function ajax_forms_test_advanced_commands_changed_callback($form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $response->addCommand(new ChangedCommand('#changed_div'));
  return $response;
}

/**
 * Ajax form callback: Selects 'changed' with asterisk marking inner div.
 */
function ajax_forms_test_advanced_commands_changed_asterisk_callback($form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $response->addCommand(new ChangedCommand('#changed_div', '#changed_div_mark_this'));
  return $response;
}

/**
 * Ajax form callback: Selects 'css'.
 */
function ajax_forms_test_advanced_commands_css_callback($form, FormStateInterface $form_state) {
  $selector = '#css_div';
  $color = 'blue';

  $response = new AjaxResponse();
  $response->addCommand(new CssCommand($selector, ['background-color' => $color]));
  return $response;
}

/**
 * Ajax form callback: Selects 'data'.
 */
function ajax_forms_test_advanced_commands_data_callback($form, FormStateInterface $form_state) {
  $selector = '#data_div';
  $response = new AjaxResponse();
  $response->addCommand(new DataCommand($selector, 'testkey', 'testvalue'));
  return $response;
}

/**
 * Ajax form callback: Selects 'invoke'.
 */
function ajax_forms_test_advanced_commands_invoke_callback($form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $response->addCommand(new InvokeCommand('#invoke_div', 'addClass', ['error']));
  return $response;
}

/**
 * Ajax form callback: Selects 'html'.
 */
function ajax_forms_test_advanced_commands_html_callback($form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $response->addCommand(new HtmlCommand('#html_div', 'replacement text'));
  return $response;
}

/**
 * Ajax form callback: Selects 'insert'.
 */
function ajax_forms_test_advanced_commands_insert_callback($form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $response->addCommand(new InsertCommand('#insert_div', 'insert replacement text'));
  return $response;
}

/**
 * Ajax form callback: Selects 'prepend'.
 */
function ajax_forms_test_advanced_commands_prepend_callback($form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $response->addCommand(new PrependCommand('#prepend_div', "prepended text"));
  return $response;
}

/**
 * Ajax form callback: Selects 'remove'.
 */
function ajax_forms_test_advanced_commands_remove_callback($form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $response->addCommand(new RemoveCommand('#remove_text'));
  return $response;
}

/**
 * Ajax form callback: Selects 'restripe'.
 */
function ajax_forms_test_advanced_commands_restripe_callback($form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $response->addCommand(new RestripeCommand('#restripe_table'));
  return $response;
}

/**
 * Ajax form callback: Selects 'settings'.
 */
function ajax_forms_test_advanced_commands_settings_callback($form, FormStateInterface $form_state) {
  $setting['ajax_forms_test']['foo'] = 42;
  $response = new AjaxResponse();
  $response->addCommand(new SettingsCommand($setting));
  return $response;
}

/**
 * Ajax callback for 'add_css'.
 */
function ajax_forms_test_advanced_commands_add_css_callback($form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $response->addCommand(new AddCssCommand('my/file.css'));
  return $response;
}

/**
 * Ajax form callback: Selects the 'drivertext' element of the validation form.
 */
function ajax_forms_test_validation_form_callback($form, FormStateInterface $form_state) {
  drupal_set_message("ajax_forms_test_validation_form_callback invoked");
  drupal_set_message(t("Callback: drivertext=%drivertext, spare_required_field=%spare_required_field", ['%drivertext' => $form_state->getValue('drivertext'), '%spare_required_field' => $form_state->getValue('spare_required_field')]));
  return ['#markup' => '<div id="message_area">ajax_forms_test_validation_form_callback at ' . date('c') . '</div>'];
}

/**
 * Ajax form callback: Selects the 'drivernumber' element of the validation form.
 */
function ajax_forms_test_validation_number_form_callback($form, FormStateInterface $form_state) {
  drupal_set_message("ajax_forms_test_validation_number_form_callback invoked");
  drupal_set_message(t("Callback: drivernumber=%drivernumber, spare_required_field=%spare_required_field", ['%drivernumber' => $form_state->getValue('drivernumber'), '%spare_required_field' => $form_state->getValue('spare_required_field')]));
  return ['#markup' => '<div id="message_area_number">ajax_forms_test_validation_number_form_callback at ' . date('c') . '</div>'];
}

/**
 * AJAX form callback: Selects for the ajax_forms_test_lazy_load_form() form.
 */
function ajax_forms_test_lazy_load_form_ajax($form, FormStateInterface $form_state) {
  $build = [
    '#markup' => 'new content',
  ];

  if ($form_state->getValue('add_files')) {
    $build['#attached']['library'][] = 'system/admin';
    $build['#attached']['library'][] = 'system/drupal.system';
    $build['#attached']['drupalSettings']['ajax_forms_test_lazy_load_form_submit'] = 'executed';
  }

  return $build;
}