AjaxTestController.php 5.98 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php

/**
 * @file
 * Contains \Drupal\ajax_test\Controller\AjaxTestController.
 */

namespace Drupal\ajax_test\Controller;

10
use Drupal\Core\Ajax\AjaxResponse;
11
12
use Drupal\Core\Ajax\AlertCommand;
use Drupal\Core\Ajax\CloseDialogCommand;
13
use Drupal\Core\Ajax\HtmlCommand;
14
use Drupal\Core\Url;
15
use Symfony\Component\HttpFoundation\Request;
16

17
18
19
20
21
22
/**
 * Provides content for dialog tests.
 */
class AjaxTestController {

  /**
23
24
25
26
   * Example content for dialog testing.
   *
   * @return array
   *   Renderable array of AJAX dialog contents.
27
   */
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  public static function dialogContents() {
    // This is a regular render array; the keys do not have special meaning.
    $content = array(
      '#title' => 'AJAX Dialog contents',
      'content' => array(
        '#markup' => 'Example message',
      ),
      'cancel' => array(
        '#type' => 'link',
        '#title' => 'Cancel',
        '#url' => Url::fromRoute('<front>'),
        '#attributes' => array(
          // This is a special class to which JavaScript assigns dialog closing
          // behavior.
          'class' => array('dialog-cancel'),
        ),
      ),
    );

    return $content;
48
49
50
  }

  /**
51
52
   * Returns a render array that will be rendered by AjaxRenderer.
   *
53
54
   * Verifies that the response incorporates JavaScript settings generated
   * during the page request by adding a dummy setting.
55
56
   */
  public function render() {
57
58
59
60
61
62
63
64
65
66
    return [
      '#attached' => [
        'library' => [
          'core/drupalSettings',
        ],
        'drupalSettings' => [
          'ajax' => 'test',
        ],
      ],
    ];
67
68
69
  }

  /**
70
71
72
   * Returns an AjaxResponse; settings command set last.
   *
   * Helps verifying AjaxResponse reorders commands to ensure correct execution.
73
74
   */
  public function order() {
75
76
77
78
79
80
81
    $response = new AjaxResponse();
    // HTML insertion command.
    $response->addCommand(new HtmlCommand('body', 'Hello, world!'));
    $build['#attached']['library'][] = 'ajax_test/order';
    $response->setAttachments($build['#attached']);

    return $response;
82
83
84
  }

  /**
85
86
87
88
89
90
91
   * Returns an AjaxResponse with alert command.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request object.
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   The JSON response object.
92
   */
93
94
95
96
97
98
99
100
101
  public function renderError(Request $request) {
    $message = '';
    $query = $request->query;
    if ($query->has('message')) {
      $message = $query->get('message');
    }
    $response = new AjaxResponse();
    $response->addCommand(new AlertCommand($message));
    return $response;
102
103
104
  }

  /**
105
   * Returns a render array of form elements and links for dialog.
106
107
   */
  public function dialog() {
108
109
110
111
112
113
114
115
116
117
118
    // 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::formBuilder()->getForm('Drupal\ajax_test\Form\AjaxTestDialogForm');

    // Dialog behavior applied to a #type => 'link'.
    $build['link'] = array(
      '#type' => 'link',
      '#title' => 'Link 1 (modal)',
119
      '#url' => Url::fromRoute('ajax_test.dialog_contents'),
120
121
      '#attributes' => array(
        'class' => array('use-ajax'),
122
        'data-dialog-type' => 'modal',
123
124
125
      ),
    );

126
    // Dialog behavior applied to links rendered by links.html.twig.
127
128
129
130
131
    $build['links'] = array(
      '#theme' => 'links',
      '#links' => array(
        'link2' => array(
          'title' => 'Link 2 (modal)',
132
          'url' => Url::fromRoute('ajax_test.dialog_contents'),
133
134
          'attributes' => array(
            'class' => array('use-ajax'),
135
            'data-dialog-type' => 'modal',
136
137
138
139
140
141
142
            'data-dialog-options' => json_encode(array(
              'width' => 400,
            ))
          ),
        ),
        'link3' => array(
          'title' => 'Link 3 (non-modal)',
143
          'url' => Url::fromRoute('ajax_test.dialog_contents'),
144
145
          'attributes' => array(
            'class' => array('use-ajax'),
146
            'data-dialog-type' => 'dialog',
147
148
149
150
151
152
153
154
            'data-dialog-options' => json_encode(array(
              'target' => 'ajax-test-dialog-wrapper-1',
              'width' => 800,
            ))
          ),
        ),
        'link4' => array(
          'title' => 'Link 4 (close non-modal if open)',
155
          'url' => Url::fromRoute('ajax_test.dialog_close'),
156
157
          'attributes' => array(
            'class' => array('use-ajax'),
158
            'data-dialog-type' => 'modal',
159
160
161
162
          ),
        ),
        'link5' => array(
          'title' => 'Link 5 (form)',
163
          'url' => Url::fromRoute('ajax_test.dialog_form'),
164
165
          'attributes' => array(
            'class' => array('use-ajax'),
166
            'data-dialog-type' => 'modal',
167
168
169
170
          ),
        ),
        'link6' => array(
          'title' => 'Link 6 (entity form)',
171
          'url' => Url::fromRoute('contact.form_add'),
172
173
          'attributes' => array(
            'class' => array('use-ajax'),
174
            'data-dialog-type' => 'modal',
175
176
177
178
179
180
181
182
            'data-dialog-options' => json_encode(array(
              'width' => 800,
              'height' => 500,
            ))
          ),
        ),
        'link7' => array(
          'title' => 'Link 7 (non-modal, no target)',
183
          'url' => Url::fromRoute('ajax_test.dialog_contents'),
184
185
          'attributes' => array(
            'class' => array('use-ajax'),
186
            'data-dialog-type' => 'dialog',
187
188
189
190
191
192
193
194
195
            'data-dialog-options' => json_encode(array(
              'width' => 800,
            ))
          ),
        ),
      ),
    );

    return $build;
196
197
198
  }

  /**
199
200
201
202
   * Returns an AjaxResponse with command to close dialog.
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   The JSON response object.
203
204
   */
  public function dialogClose() {
205
206
207
    $response = new AjaxResponse();
    $response->addCommand(new CloseDialogCommand('#ajax-test-dialog-wrapper-1'));
    return $response;
208
209
210
  }

}