AjaxTestController.php 4.8 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
11
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\HtmlCommand;
12
13
use Drupal\Core\Url;

14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
 * Provides content for dialog tests.
 */
class AjaxTestController {

  /**
   * Returns example content for dialog testing.
   */
  public function dialogContents() {
    // Re-use the utility method that returns the example content.
    return ajax_test_dialog_contents();
  }

  /**
28
29
30
31
32
   * 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.
33
34
   */
  public function render() {
35
36
37
38
39
40
41
42
43
44
    return [
      '#attached' => [
        'library' => [
          'core/drupalSettings',
        ],
        'drupalSettings' => [
          'ajax' => 'test',
        ],
      ],
    ];
45
46
47
  }

  /**
48
49
50
   * Returns an AjaxResponse; settings command set last.
   *
   * Helps verifying AjaxResponse reorders commands to ensure correct execution.
51
52
   */
  public function order() {
53
54
55
56
57
58
59
    $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;
60
61
62
63
64
65
66
67
68
69
70
71
72
  }

  /**
   * @todo Remove ajax_test_error().
   */
  public function renderError() {
    return ajax_test_error();
  }

  /**
   * @todo Remove ajax_test_dialog().
   */
  public function dialog() {
73
74
75
76
77
78
79
80
81
82
83
    // 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)',
84
      '#url' => Url::fromRoute('ajax_test.dialog_contents'),
85
86
87
88
89
90
      '#attributes' => array(
        'class' => array('use-ajax'),
        'data-accepts' => 'application/vnd.drupal-modal',
      ),
    );

91
    // Dialog behavior applied to links rendered by links.html.twig.
92
93
94
95
96
    $build['links'] = array(
      '#theme' => 'links',
      '#links' => array(
        'link2' => array(
          'title' => 'Link 2 (modal)',
97
          'url' => Url::fromRoute('ajax_test.dialog_contents'),
98
99
100
101
102
103
104
105
106
107
          'attributes' => array(
            'class' => array('use-ajax'),
            'data-accepts' => 'application/vnd.drupal-modal',
            'data-dialog-options' => json_encode(array(
              'width' => 400,
            ))
          ),
        ),
        'link3' => array(
          'title' => 'Link 3 (non-modal)',
108
          'url' => Url::fromRoute('ajax_test.dialog_contents'),
109
110
111
112
113
114
115
116
117
118
119
          'attributes' => array(
            'class' => array('use-ajax'),
            'data-accepts' => 'application/vnd.drupal-dialog',
            'data-dialog-options' => json_encode(array(
              'target' => 'ajax-test-dialog-wrapper-1',
              'width' => 800,
            ))
          ),
        ),
        'link4' => array(
          'title' => 'Link 4 (close non-modal if open)',
120
          'url' => Url::fromRoute('ajax_test.dialog_close'),
121
122
123
124
125
126
          'attributes' => array(
            'class' => array('use-ajax'),
          ),
        ),
        'link5' => array(
          'title' => 'Link 5 (form)',
127
          'url' => Url::fromRoute('ajax_test.dialog_form'),
128
129
130
131
132
133
134
          'attributes' => array(
            'class' => array('use-ajax'),
            'data-accepts' => 'application/vnd.drupal-modal',
          ),
        ),
        'link6' => array(
          'title' => 'Link 6 (entity form)',
135
          'url' => Url::fromRoute('contact.form_add'),
136
137
138
139
140
141
142
143
144
145
146
          'attributes' => array(
            'class' => array('use-ajax'),
            'data-accepts' => 'application/vnd.drupal-modal',
            'data-dialog-options' => json_encode(array(
              'width' => 800,
              'height' => 500,
            ))
          ),
        ),
        'link7' => array(
          'title' => 'Link 7 (non-modal, no target)',
147
          'url' => Url::fromRoute('ajax_test.dialog_contents'),
148
149
150
151
152
153
154
155
156
157
158
159
          'attributes' => array(
            'class' => array('use-ajax'),
            'data-accepts' => 'application/vnd.drupal-dialog',
            'data-dialog-options' => json_encode(array(
              'width' => 800,
            ))
          ),
        ),
      ),
    );

    return $build;
160
161
162
163
164
165
166
167
168
169
  }

  /**
   * @todo Remove ajax_test_dialog_close().
   */
  public function dialogClose() {
    return ajax_test_dialog_close();
  }

}