summaryrefslogtreecommitdiffstats
path: root/core/modules/system/tests/modules/ajax_test/ajax_test.module
blob: eba9efb532e3eb5868b30ecc4f039b39dba70e20 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php

/**
 * @file
 * Helper module for Ajax framework tests.
 */

use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\AlertCommand;
use Drupal\Core\Ajax\OpenDialogCommand;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Ajax\CloseDialogCommand;
use Drupal\Core\Ajax\HtmlCommand;

/**
 * Implements hook_system_theme_info().
 */
function ajax_test_system_theme_info() {
  $themes['test_theme'] = drupal_get_path('module', 'system') . '/tests/themes/test_theme/test_theme.info.yml';
  return $themes;
}

/**
 * Menu callback: Returns an element suitable for use by ajax_render().
 *
 * Additionally ensures that ajax_render() incorporates JavaScript settings
 * generated during the page request by invoking drupal_add_js() with a dummy
 * setting.
 *
 * @deprecated \Drupal\ajax_test\Controller\AjaxTestController::render()
 */
function ajax_test_render() {
  $attached = array(
    '#attached' => array(
      'js' => array(
        0 => array(
          'type' => 'setting',
          'data' => array('ajax' => 'test'),
        ),
      ),
    ),
  );
  drupal_render($attached);
  $response = new AjaxResponse();
  return $response;
}

/**
 * Menu callback: Returns an AjaxResponse; settings command set last.
 *
 * Helps verifying AjaxResponse reorders commands to ensure correct execution.
 *
 * @deprecated \Drupal\ajax_test\Controller\AjaxTestController::order()
 */
function ajax_test_order() {
  $response = new AjaxResponse();
  $path = drupal_get_path('module', 'system');
  // HTML insertion command.
  $response->addCommand(new HtmlCommand('body', 'Hello, world!'));
  $attached = array(
    '#attached' => array(
      'css' => array(
        // Add two CSS files (order should remain the same).
        $path . '/css/system.admin.css' => array(),
        $path . '/css/system.maintenance.css' => array(),
      ),
      'js' => array(
        // Add two JavaScript files (first to the footer, should appear last).
        $path . '/system.modules.js' => array('scope' => 'footer'),
        $path . '/system.js' => array(),
        // Finally, add a JavaScript setting.
        0 => array(
          'type' => 'setting',
          'data' => array('ajax' => 'test'),
        ),
      ),
    ),
  );
  drupal_render($attached);
  return $response;
}

/**
 * Menu callback: Returns AJAX element with #error property set.
 *
 * @deprecated \Drupal\ajax_test\Controller\AjaxTestController::renderError()
 */
function ajax_test_error() {
  $message = '';
  $query = \Drupal::request()->query;
  if ($query->has('message')) {
    $message = $query->get('message');
  }
  $response = new AjaxResponse();
  $response->addCommand(new AlertCommand($message));
  return $response;
}

/**
 * Menu callback: Renders a form elements and links with #ajax['dialog'].
 *
 * @deprecated \Drupal\ajax_test\Controller\AjaxTestController::dialog()
 */
function ajax_test_dialog() {
  // Add two wrapper elements for testing non-modal dialogs. Modal dialogs use
  // the global drupal-modal wrapper by default.
  $build['dialog_wrappers'] = array('#markup' => '<div id="ajax-test-dialog-wrapper-1"></div><div id="ajax-test-dialog-wrapper-2"></div>');

  // Dialog behavior applied to a button.
  $build['form'] = drupal_get_form('ajax_test_dialog_form');

  // Dialog behavior applied to a #type => 'link'.
  $build['link'] = array(
    '#type' => 'link',
    '#title' => 'Link 1 (modal)',
    '#href' => 'ajax-test/dialog-contents',
    '#attributes' => array(
      'class' => array('use-ajax'),
      'data-accepts' => 'application/vnd.drupal-modal',
    ),
  );

  // Dialog behavior applied to links rendered by theme_links().
  $build['links'] = array(
    '#theme' => 'links',
    '#links' => array(
      'link2' => array(
        'title' => 'Link 2 (modal)',
        'href' => 'ajax-test/dialog-contents',
        'attributes' => array(
          'class' => array('use-ajax'),
          'data-accepts' => 'application/vnd.drupal-modal',
          'data-dialog-options' => json_encode(array(
            'width' => 400,
          ))
        ),
      ),
      'link3' => array(
        'title' => 'Link 3 (non-modal)',
        'href' => 'ajax-test/dialog-contents',
        'attributes' => array(
          'class' => array('use-ajax'),
          'data-accepts' => 'application/vnd.drupal-dialog',
          'data-dialog-options' => json_encode(array(
            'target' => 'ajax-test-dialog-wrapper-1',
            'width' => 800,
          ))
        ),
      ),
      'link4' => array(
        'title' => 'Link 4 (close non-modal if open)',
        'href' => 'ajax-test/dialog-close',
        'attributes' => array(
          'class' => array('use-ajax'),
        ),
      ),
      'link5' => array(
        'title' => 'Link 5 (form)',
        'href' => 'ajax-test/dialog-form',
        'attributes' => array(
          'class' => array('use-ajax'),
          'data-accepts' => 'application/vnd.drupal-modal',
        ),
      ),
      'link6' => array(
        'title' => 'Link 6 (entity form)',
        'href' => 'admin/structure/contact/add',
        'attributes' => array(
          'class' => array('use-ajax'),
          'data-accepts' => 'application/vnd.drupal-modal',
          'data-dialog-options' => json_encode(array(
            'width' => 800,
            'height' => 500,
          ))
        ),
      ),
      'link7' => array(
        'title' => 'Link 7 (non-modal, no target)',
        'href' => 'ajax-test/dialog-contents',
        'attributes' => array(
          'class' => array('use-ajax'),
          'data-accepts' => 'application/vnd.drupal-dialog',
          'data-dialog-options' => json_encode(array(
            'width' => 800,
          ))
        ),
      ),
    ),
  );
  return $build;
}

/**
 * Form builder: Renders buttons with #ajax['dialog'].
 */
function ajax_test_dialog_form($form, &$form_state) {
  // In order to use WebTestBase::drupalPostAjaxForm() to POST from a link, we need
  // to have a dummy field we can set in WebTestBase::drupalPostForm() else it won't
  // submit anything.
  $form['textfield'] = array(
    '#type' => 'hidden'
  );
  $form['button1'] = array(
    '#type' => 'submit',
    '#name' => 'button1',
    '#value' => 'Button 1 (modal)',
    '#ajax' => array(
      'callback' => 'ajax_test_dialog_form_callback_modal',
    ),
  );
  $form['button2'] = array(
    '#type' => 'submit',
    '#name' => 'button2',
    '#value' => 'Button 2 (non-modal)',
    '#ajax' => array(
      'callback' => 'ajax_test_dialog_form_callback_nonmodal',
    ),
  );
  return $form;
}

/**
 * Non-AJAX behavior of the dialog buttons.
 */
function ajax_test_dialog_form_submit($form, &$form_state) {
  $form_state['redirect_route']['route_name'] = 'ajax_test.dialog_contents';
}

/**
 * AJAX callback handler for ajax_test_dialog_form().
 */
function ajax_test_dialog_form_callback_modal($form, &$form_state) {
  return _ajax_test_dialog(TRUE);
}

/**
 * AJAX callback handler for ajax_test_dialog_form().
 */
function ajax_test_dialog_form_callback_nonmodal($form, &$form_state) {
  return _ajax_test_dialog(FALSE);
}

/**
 * Util to render dialog in ajax callback.
 *
 * @param bool $is_modal
 *   (optional) TRUE if modal, FALSE if plain dialog. Defaults to FALSE.
 */
function _ajax_test_dialog($is_modal = FALSE) {
  $content = ajax_test_dialog_contents();
  $response = new AjaxResponse();
  $title = t('AJAX Dialog contents');
  $html = drupal_render($content);
  if ($is_modal) {
    $response->addCommand(new OpenModalDialogCommand($title, $html));
  }
  else {
    $selector = '#ajax-test-dialog-wrapper-1';
    $response->addCommand(new OpenDialogCommand($selector, $title, $html));
  }
  return $response;
}

/**
 * Returns example content for dialog tests.
 */
function ajax_test_dialog_contents() {
  // This is a regular render array; the keys do not have special meaning.
  $content = array(
    'content' => array(
      '#markup' => 'Example message',
    ),
    'cancel' => array(
      '#type' => 'link',
      '#title' => 'Cancel',
      '#href' => '',
      '#attributes' => array(
        // This is a special class to which JavaScript assigns dialog closing
        // behavior.
        'class' => array('dialog-cancel'),
      ),
    ),
  );

  return $content;
}

/**
 * Menu callback: Close the ajax dialog.
 *
 * @deprecated \Drupal\ajax_test\Controller\AjaxTestController::dialogClose()
 */
function ajax_test_dialog_close() {
  $response = new AjaxResponse();
  $response->addCommand(new CloseDialogCommand('#ajax-test-dialog-wrapper-1'));
  return $response;
}