5ba65e8013a66ed372158617e17430f1ba7f8ffc
[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    * Returns a render array that will be rendered by AjaxRenderer.
47    *
48    * Verifies that the response incorporates JavaScript settings generated
49    * during the page request by adding a dummy setting.
50    */
51   public function render() {
52     return [
53       '#attached' => [
54         'library' => [
55           'core/drupalSettings',
56         ],
57         'drupalSettings' => [
58           'ajax' => 'test',
59         ],
60       ],
61     ];
62   }
63
64   /**
65    * Returns the used theme.
66    */
67   public function theme() {
68     return [
69       '#markup' => 'Current theme: ' . \Drupal::theme()->getActiveTheme()->getName(),
70     ];
71   }
72
73   /**
74    * Returns an AjaxResponse; settings command set last.
75    *
76    * Helps verifying AjaxResponse reorders commands to ensure correct execution.
77    */
78   public function order() {
79     $response = new AjaxResponse();
80     // HTML insertion command.
81     $response->addCommand(new HtmlCommand('body', 'Hello, world!'));
82     $build['#attached']['library'][] = 'ajax_test/order';
83     $response->setAttachments($build['#attached']);
84
85     return $response;
86   }
87
88   /**
89    * Returns an AjaxResponse with alert command.
90    *
91    * @param \Symfony\Component\HttpFoundation\Request $request
92    *   The current request object.
93    *
94    * @return \Drupal\Core\Ajax\AjaxResponse
95    *   The JSON response object.
96    */
97   public function renderError(Request $request) {
98     $message = '';
99     $query = $request->query;
100     if ($query->has('message')) {
101       $message = $query->get('message');
102     }
103     $response = new AjaxResponse();
104     $response->addCommand(new AlertCommand($message));
105     return $response;
106   }
107
108   /**
109    * Returns a render array of form elements and links for dialog.
110    */
111   public function dialog() {
112     // Add two wrapper elements for testing non-modal dialogs. Modal dialogs use
113     // the global drupal-modal wrapper by default.
114     $build['dialog_wrappers'] = ['#markup' => '<div id="ajax-test-dialog-wrapper-1"></div><div id="ajax-test-dialog-wrapper-2"></div>'];
115
116     // Dialog behavior applied to a button.
117     $build['form'] = \Drupal::formBuilder()->getForm('Drupal\ajax_test\Form\AjaxTestDialogForm');
118
119     // Dialog behavior applied to a #type => 'link'.
120     $build['link'] = [
121       '#type' => 'link',
122       '#title' => 'Link 1 (modal)',
123       '#url' => Url::fromRoute('ajax_test.dialog_contents'),
124       '#attributes' => [
125         'class' => ['use-ajax'],
126         'data-dialog-type' => 'modal',
127       ],
128     ];
129
130     // Dialog behavior applied to links rendered by links.html.twig.
131     $build['links'] = [
132       '#theme' => 'links',
133       '#links' => [
134         'link2' => [
135           'title' => 'Link 2 (modal)',
136           'url' => Url::fromRoute('ajax_test.dialog_contents'),
137           'attributes' => [
138             'class' => ['use-ajax'],
139             'data-dialog-type' => 'modal',
140             'data-dialog-options' => json_encode([
141               'width' => 400,
142             ])
143           ],
144         ],
145         'link3' => [
146           'title' => 'Link 3 (non-modal)',
147           'url' => Url::fromRoute('ajax_test.dialog_contents'),
148           'attributes' => [
149             'class' => ['use-ajax'],
150             'data-dialog-type' => 'dialog',
151             'data-dialog-options' => json_encode([
152               'target' => 'ajax-test-dialog-wrapper-1',
153               'width' => 800,
154             ])
155           ],
156         ],
157         'link4' => [
158           'title' => 'Link 4 (close non-modal if open)',
159           'url' => Url::fromRoute('ajax_test.dialog_close'),
160           'attributes' => [
161             'class' => ['use-ajax'],
162             'data-dialog-type' => 'modal',
163           ],
164         ],
165         'link5' => [
166           'title' => 'Link 5 (form)',
167           'url' => Url::fromRoute('ajax_test.dialog_form'),
168           'attributes' => [
169             'class' => ['use-ajax'],
170             'data-dialog-type' => 'modal',
171           ],
172         ],
173         'link6' => [
174           'title' => 'Link 6 (entity form)',
175           'url' => Url::fromRoute('contact.form_add'),
176           'attributes' => [
177             'class' => ['use-ajax'],
178             'data-dialog-type' => 'modal',
179             'data-dialog-options' => json_encode([
180               'width' => 800,
181               'height' => 500,
182             ])
183           ],
184         ],
185         'link7' => [
186           'title' => 'Link 7 (non-modal, no target)',
187           'url' => Url::fromRoute('ajax_test.dialog_contents'),
188           'attributes' => [
189             'class' => ['use-ajax'],
190             'data-dialog-type' => 'dialog',
191             'data-dialog-options' => json_encode([
192               'width' => 800,
193             ])
194           ],
195         ],
196         'link8' => [
197           'title' => 'Link 8 (ajax)',
198           'url' => Url::fromRoute('ajax_test.admin.theme'),
199           'attributes' => [
200             'class' => ['use-ajax'],
201             'data-dialog-type' => 'modal',
202             'data-dialog-options' => json_encode([
203               'width' => 400,
204             ]),
205           ],
206         ],
207       ],
208     ];
209
210     return $build;
211   }
212
213   /**
214    * Returns an AjaxResponse with command to close dialog.
215    *
216    * @return \Drupal\Core\Ajax\AjaxResponse
217    *   The JSON response object.
218    */
219   public function dialogClose() {
220     $response = new AjaxResponse();
221     $response->addCommand(new CloseDialogCommand('#ajax-test-dialog-wrapper-1'));
222     return $response;
223   }
224
225 }