AjaxTestController.php 6.01 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
53
54
55
   * Returns a render array that will be rendered by AjaxRenderer.
   *
   * Ensures that \Drupal\Core\Ajax\AjaxResponse::ajaxRender()
   * incorporates JavaScript settings generated during the page request by
   * adding a dummy setting.
56
57
   */
  public function render() {
58
59
60
61
62
63
64
65
66
67
    return [
      '#attached' => [
        'library' => [
          'core/drupalSettings',
        ],
        'drupalSettings' => [
          'ajax' => 'test',
        ],
      ],
    ];
68
69
70
  }

  /**
71
72
73
   * Returns an AjaxResponse; settings command set last.
   *
   * Helps verifying AjaxResponse reorders commands to ensure correct execution.
74
75
   */
  public function order() {
76
77
78
79
80
81
82
    $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;
83
84
85
  }

  /**
86
87
88
89
90
91
92
   * 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.
93
   */
94
95
96
97
98
99
100
101
102
  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;
103
104
105
  }

  /**
106
   * Returns a render array of form elements and links for dialog.
107
108
   */
  public function dialog() {
109
110
111
112
113
114
115
116
117
118
119
    // 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)',
120
      '#url' => Url::fromRoute('ajax_test.dialog_contents'),
121
122
      '#attributes' => array(
        'class' => array('use-ajax'),
123
        'data-dialog-type' => 'modal',
124
125
126
      ),
    );

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

    return $build;
197
198
199
  }

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

}