Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / system / tests / modules / ajax_test / src / Controller / AjaxTestController.php
1 <?php
2
3 namespace Drupal\ajax_test\Controller;
4
5 use Drupal\Core\Ajax\AjaxResponse;
6 use Drupal\Core\Ajax\AlertCommand;
7 use Drupal\Core\Ajax\CloseDialogCommand;
8 use Drupal\Core\Ajax\HtmlCommand;
9 use Drupal\Core\Url;
10 use Symfony\Component\HttpFoundation\Request;
11
12 /**
13  * Provides content for dialog tests.
14  */
15 class AjaxTestController {
16
17   /**
18    * Example content for dialog testing.
19    *
20    * @return array
21    *   Renderable array of AJAX dialog contents.
22    */
23   public static function dialogContents() {
24     // This is a regular render array; the keys do not have special meaning.
25     $content = [
26       '#title' => '<em>AJAX Dialog & contents</em>',
27       'content' => [
28         '#markup' => 'Example message',
29       ],
30       'cancel' => [
31         '#type' => 'link',
32         '#title' => 'Cancel',
33         '#url' => Url::fromRoute('<front>'),
34         '#attributes' => [
35           // This is a special class to which JavaScript assigns dialog closing
36           // behavior.
37           'class' => ['dialog-cancel'],
38         ],
39       ],
40     ];
41
42     return $content;
43   }
44
45   /**
46    * Example content for testing the wrapper of the response.
47    *
48    * @param string $type
49    *   Type of response.
50    *
51    * @return array
52    *   Renderable array of AJAX response contents.
53    */
54   public function renderTypes($type) {
55     return [
56       '#title' => '<em>AJAX Dialog & contents</em>',
57       'content' => [
58         '#type' => 'inline_template',
59         '#template' => $this->getRenderTypes()[$type]['render'],
60       ],
61     ];
62   }
63
64   /**
65    * Returns a render array of links that directly Drupal.ajax().
66    *
67    * @return array
68    *   Renderable array of AJAX response contents.
69    */
70   public function insertLinksBlockWrapper() {
71     $methods = [
72       'html',
73       'replaceWith',
74     ];
75
76     $build['links'] = [
77       'ajax_target' => [
78         '#markup' => '<div class="ajax-target-wrapper"><div id="ajax-target">Target</div></div>',
79       ],
80       'links' => [
81         '#theme' => 'links',
82         '#attached' => ['library' => ['ajax_test/ajax_insert']],
83       ],
84     ];
85     foreach ($methods as $method) {
86       foreach ($this->getRenderTypes() as $type => $item) {
87         $class = 'ajax-insert';
88         $build['links']['links']['#links']["$method-$type"] = [
89           'title' => "Link $method $type",
90           'url' => Url::fromRoute('ajax_test.ajax_render_types', ['type' => $type]),
91           'attributes' => [
92             'class' => [$class],
93             'data-method' => $method,
94             'data-effect' => $item['effect'],
95           ],
96         ];
97       }
98     }
99     return $build;
100   }
101
102   /**
103    * Returns a render array of links that directly Drupal.ajax().
104    *
105    * @return array
106    *   Renderable array of AJAX response contents.
107    */
108   public function insertLinksInlineWrapper() {
109     $methods = [
110       'html',
111       'replaceWith',
112     ];
113
114     $build['links'] = [
115       'ajax_target' => [
116         '#markup' => '<div class="ajax-target-wrapper"><span id="ajax-target-inline">Target inline</span></div>',
117       ],
118       'links' => [
119         '#theme' => 'links',
120         '#attached' => ['library' => ['ajax_test/ajax_insert']],
121       ],
122     ];
123     foreach ($methods as $method) {
124       foreach ($this->getRenderTypes() as $type => $item) {
125         $class = 'ajax-insert-inline';
126         $build['links']['links']['#links']["$method-$type"] = [
127           'title' => "Link $method $type",
128           'url' => Url::fromRoute('ajax_test.ajax_render_types', ['type' => $type]),
129           'attributes' => [
130             'class' => [$class],
131             'data-method' => $method,
132             'data-effect' => $item['effect'],
133           ],
134         ];
135       }
136     }
137     return $build;
138   }
139
140   /**
141    * Returns a render array that will be rendered by AjaxRenderer.
142    *
143    * Verifies that the response incorporates JavaScript settings generated
144    * during the page request by adding a dummy setting.
145    */
146   public function render() {
147     return [
148       '#attached' => [
149         'library' => [
150           'core/drupalSettings',
151         ],
152         'drupalSettings' => [
153           'ajax' => 'test',
154         ],
155       ],
156     ];
157   }
158
159   /**
160    * Returns the used theme.
161    */
162   public function theme() {
163     return [
164       '#markup' => 'Current theme: ' . \Drupal::theme()->getActiveTheme()->getName(),
165     ];
166   }
167
168   /**
169    * Returns an AjaxResponse; settings command set last.
170    *
171    * Helps verifying AjaxResponse reorders commands to ensure correct execution.
172    */
173   public function order() {
174     $response = new AjaxResponse();
175     // HTML insertion command.
176     $response->addCommand(new HtmlCommand('body', 'Hello, world!'));
177     $build['#attached']['library'][] = 'ajax_test/order';
178     $response->setAttachments($build['#attached']);
179
180     return $response;
181   }
182
183   /**
184    * Returns an AjaxResponse with alert command.
185    *
186    * @param \Symfony\Component\HttpFoundation\Request $request
187    *   The current request object.
188    *
189    * @return \Drupal\Core\Ajax\AjaxResponse
190    *   The JSON response object.
191    */
192   public function renderError(Request $request) {
193     $message = '';
194     $query = $request->query;
195     if ($query->has('message')) {
196       $message = $query->get('message');
197     }
198     $response = new AjaxResponse();
199     $response->addCommand(new AlertCommand($message));
200     return $response;
201   }
202
203   /**
204    * Returns a render array of form elements and links for dialog.
205    */
206   public function dialog() {
207     // Add two wrapper elements for testing non-modal dialogs. Modal dialogs use
208     // the global drupal-modal wrapper by default.
209     $build['dialog_wrappers'] = ['#markup' => '<div id="ajax-test-dialog-wrapper-1"></div><div id="ajax-test-dialog-wrapper-2"></div>'];
210
211     // Dialog behavior applied to a button.
212     $build['form'] = \Drupal::formBuilder()->getForm('Drupal\ajax_test\Form\AjaxTestDialogForm');
213
214     // Dialog behavior applied to a #type => 'link'.
215     $build['link'] = [
216       '#type' => 'link',
217       '#title' => 'Link 1 (modal)',
218       '#url' => Url::fromRoute('ajax_test.dialog_contents'),
219       '#attributes' => [
220         'class' => ['use-ajax'],
221         'data-dialog-type' => 'modal',
222       ],
223     ];
224
225     // Dialog behavior applied to links rendered by links.html.twig.
226     $build['links'] = [
227       '#theme' => 'links',
228       '#links' => [
229         'link2' => [
230           'title' => 'Link 2 (modal)',
231           'url' => Url::fromRoute('ajax_test.dialog_contents'),
232           'attributes' => [
233             'class' => ['use-ajax'],
234             'data-dialog-type' => 'modal',
235             'data-dialog-options' => json_encode([
236               'width' => 400,
237             ]),
238           ],
239         ],
240         'link3' => [
241           'title' => 'Link 3 (non-modal)',
242           'url' => Url::fromRoute('ajax_test.dialog_contents'),
243           'attributes' => [
244             'class' => ['use-ajax'],
245             'data-dialog-type' => 'dialog',
246             'data-dialog-options' => json_encode([
247               'target' => 'ajax-test-dialog-wrapper-1',
248               'width' => 800,
249             ]),
250           ],
251         ],
252         'link4' => [
253           'title' => 'Link 4 (close non-modal if open)',
254           'url' => Url::fromRoute('ajax_test.dialog_close'),
255           'attributes' => [
256             'class' => ['use-ajax'],
257             'data-dialog-type' => 'modal',
258           ],
259         ],
260         'link5' => [
261           'title' => 'Link 5 (form)',
262           'url' => Url::fromRoute('ajax_test.dialog_form'),
263           'attributes' => [
264             'class' => ['use-ajax'],
265             'data-dialog-type' => 'modal',
266           ],
267         ],
268         'link6' => [
269           'title' => 'Link 6 (entity form)',
270           'url' => Url::fromRoute('contact.form_add'),
271           'attributes' => [
272             'class' => ['use-ajax'],
273             'data-dialog-type' => 'modal',
274             'data-dialog-options' => json_encode([
275               'width' => 800,
276               'height' => 500,
277             ]),
278           ],
279         ],
280         'link7' => [
281           'title' => 'Link 7 (non-modal, no target)',
282           'url' => Url::fromRoute('ajax_test.dialog_contents'),
283           'attributes' => [
284             'class' => ['use-ajax'],
285             'data-dialog-type' => 'dialog',
286             'data-dialog-options' => json_encode([
287               'width' => 800,
288             ]),
289           ],
290         ],
291         'link8' => [
292           'title' => 'Link 8 (ajax)',
293           'url' => Url::fromRoute('ajax_test.admin.theme'),
294           'attributes' => [
295             'class' => ['use-ajax'],
296             'data-dialog-type' => 'modal',
297             'data-dialog-options' => json_encode([
298               'width' => 400,
299             ]),
300           ],
301         ],
302       ],
303     ];
304
305     return $build;
306   }
307
308   /**
309    * Returns an AjaxResponse with command to close dialog.
310    *
311    * @return \Drupal\Core\Ajax\AjaxResponse
312    *   The JSON response object.
313    */
314   public function dialogClose() {
315     $response = new AjaxResponse();
316     $response->addCommand(new CloseDialogCommand('#ajax-test-dialog-wrapper-1'));
317     return $response;
318   }
319
320   /**
321    * Render types.
322    *
323    * @return array
324    *   Render types.
325    */
326   protected function getRenderTypes() {
327     $render_single_root = [
328       'pre-wrapped-div' => '<div class="pre-wrapped">pre-wrapped<script> var test;</script></div>',
329       'pre-wrapped-span' => '<span class="pre-wrapped">pre-wrapped<script> var test;</script></span>',
330       'pre-wrapped-whitespace' => ' <div class="pre-wrapped-whitespace">pre-wrapped-whitespace</div>' . "\r\n",
331       'not-wrapped' => 'not-wrapped',
332       'comment-string-not-wrapped' => '<!-- COMMENT -->comment-string-not-wrapped',
333       'comment-not-wrapped' => '<!-- COMMENT --><div class="comment-not-wrapped">comment-not-wrapped</div>',
334       'svg' => '<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10"><rect x="0" y="0" height="10" width="10" fill="green"/></svg>',
335       'empty' => '',
336     ];
337     $render_multiple_root = [
338       'mixed' => ' foo <!-- COMMENT -->  foo bar<div class="a class"><p>some string</p></div> additional not wrapped strings, <!-- ANOTHER COMMENT --> <p>final string</p>',
339       'top-level-only' => '<div>element #1</div><div>element #2</div>',
340       'top-level-only-pre-whitespace' => ' <div>element #1</div><div>element #2</div> ',
341       'top-level-only-middle-whitespace-span' => '<span>element #1</span> <span>element #2</span>',
342       'top-level-only-middle-whitespace-div' => '<div>element #1</div> <div>element #2</div>',
343     ];
344
345     $render_info = [];
346     foreach ($render_single_root as $key => $render) {
347       $render_info[$key] = ['render' => $render, 'effect' => 'fade'];
348     }
349     foreach ($render_multiple_root as $key => $render) {
350       $render_info[$key] = ['render' => $render, 'effect' => 'none'];
351       $render_info["$key--effect"] = ['render' => $render, 'effect' => 'fade'];
352     }
353
354     return $render_info;
355   }
356
357 }