3 namespace Drupal\ajax_test\Controller;
5 use Drupal\Core\Ajax\AjaxResponse;
6 use Drupal\Core\Ajax\AlertCommand;
7 use Drupal\Core\Ajax\CloseDialogCommand;
8 use Drupal\Core\Ajax\HtmlCommand;
10 use Symfony\Component\HttpFoundation\Request;
13 * Provides content for dialog tests.
15 class AjaxTestController {
18 * Example content for dialog testing.
21 * Renderable array of AJAX dialog contents.
23 public static function dialogContents() {
24 // This is a regular render array; the keys do not have special meaning.
26 '#title' => '<em>AJAX Dialog & contents</em>',
28 '#markup' => 'Example message',
33 '#url' => Url::fromRoute('<front>'),
35 // This is a special class to which JavaScript assigns dialog closing
37 'class' => ['dialog-cancel'],
46 * Returns a render array that will be rendered by AjaxRenderer.
48 * Verifies that the response incorporates JavaScript settings generated
49 * during the page request by adding a dummy setting.
51 public function render() {
55 'core/drupalSettings',
65 * Returns the used theme.
67 public function theme() {
69 '#markup' => 'Current theme: ' . \Drupal::theme()->getActiveTheme()->getName(),
74 * Returns an AjaxResponse; settings command set last.
76 * Helps verifying AjaxResponse reorders commands to ensure correct execution.
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']);
89 * Returns an AjaxResponse with alert command.
91 * @param \Symfony\Component\HttpFoundation\Request $request
92 * The current request object.
94 * @return \Drupal\Core\Ajax\AjaxResponse
95 * The JSON response object.
97 public function renderError(Request $request) {
99 $query = $request->query;
100 if ($query->has('message')) {
101 $message = $query->get('message');
103 $response = new AjaxResponse();
104 $response->addCommand(new AlertCommand($message));
109 * Returns a render array of form elements and links for dialog.
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>'];
116 // Dialog behavior applied to a button.
117 $build['form'] = \Drupal::formBuilder()->getForm('Drupal\ajax_test\Form\AjaxTestDialogForm');
119 // Dialog behavior applied to a #type => 'link'.
122 '#title' => 'Link 1 (modal)',
123 '#url' => Url::fromRoute('ajax_test.dialog_contents'),
125 'class' => ['use-ajax'],
126 'data-dialog-type' => 'modal',
130 // Dialog behavior applied to links rendered by links.html.twig.
135 'title' => 'Link 2 (modal)',
136 'url' => Url::fromRoute('ajax_test.dialog_contents'),
138 'class' => ['use-ajax'],
139 'data-dialog-type' => 'modal',
140 'data-dialog-options' => json_encode([
146 'title' => 'Link 3 (non-modal)',
147 'url' => Url::fromRoute('ajax_test.dialog_contents'),
149 'class' => ['use-ajax'],
150 'data-dialog-type' => 'dialog',
151 'data-dialog-options' => json_encode([
152 'target' => 'ajax-test-dialog-wrapper-1',
158 'title' => 'Link 4 (close non-modal if open)',
159 'url' => Url::fromRoute('ajax_test.dialog_close'),
161 'class' => ['use-ajax'],
162 'data-dialog-type' => 'modal',
166 'title' => 'Link 5 (form)',
167 'url' => Url::fromRoute('ajax_test.dialog_form'),
169 'class' => ['use-ajax'],
170 'data-dialog-type' => 'modal',
174 'title' => 'Link 6 (entity form)',
175 'url' => Url::fromRoute('contact.form_add'),
177 'class' => ['use-ajax'],
178 'data-dialog-type' => 'modal',
179 'data-dialog-options' => json_encode([
186 'title' => 'Link 7 (non-modal, no target)',
187 'url' => Url::fromRoute('ajax_test.dialog_contents'),
189 'class' => ['use-ajax'],
190 'data-dialog-type' => 'dialog',
191 'data-dialog-options' => json_encode([
197 'title' => 'Link 8 (ajax)',
198 'url' => Url::fromRoute('ajax_test.admin.theme'),
200 'class' => ['use-ajax'],
201 'data-dialog-type' => 'modal',
202 'data-dialog-options' => json_encode([
214 * Returns an AjaxResponse with command to close dialog.
216 * @return \Drupal\Core\Ajax\AjaxResponse
217 * The JSON response object.
219 public function dialogClose() {
220 $response = new AjaxResponse();
221 $response->addCommand(new CloseDialogCommand('#ajax-test-dialog-wrapper-1'));