ViewUI::renderPreview

public ViewUI::renderPreview($display_id, $args = array())

File

core/modules/views_ui/src/ViewUI.php, line 515

Class

ViewUI
Stores UI related temporary settings.

Namespace

Drupal\views_ui

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
public function renderPreview($display_id, $args = array()) {
  // Save the current path so it can be restored before returning from this function.
  $request_stack = \Drupal::requestStack();
  $current_request = $request_stack->getCurrentRequest();
  $executable = $this->getExecutable();
 
  // Determine where the query and performance statistics should be output.
  $config = \Drupal::config('views.settings');
  $show_query = $config->get('ui.show.sql_query.enabled');
  $show_info = $config->get('ui.show.preview_information');
  $show_location = $config->get('ui.show.sql_query.where');
 
  $show_stats = $config->get('ui.show.performance_statistics');
  if ($show_stats) {
    $show_stats = $config->get('ui.show.sql_query.where');
  }
 
  $combined = $show_query && $show_stats;
 
  $rows = array('query' => array(), 'statistics' => array());
 
  $errors = $executable->validate();
  $executable->destroy();
  if (empty($errors)) {
    $this->ajax = TRUE;
    $executable->live_preview = TRUE;
 
    // AJAX happens via HTTP POST but everything expects exposed data to
    // be in GET. Copy stuff but remove ajax-framework specific keys.
    // If we're clicking on links in a preview, though, we could actually
    // have some input in the query parameters, so we merge request() and
    // query() to ensure we get it all.
    $exposed_input = array_merge(\Drupal::request()->request->all(), \Drupal::request()->query->all());
    foreach (array('view_name', 'view_display_id', 'view_args', 'view_path', 'view_dom_id', 'pager_element', 'view_base_path', AjaxResponseSubscriber::AJAX_REQUEST_PARAMETER, 'ajax_page_state', 'form_id', 'form_build_id', 'form_token') as $key) {
      if (isset($exposed_input[$key])) {
        unset($exposed_input[$key]);
      }
    }
    $executable->setExposedInput($exposed_input);
 
    if (!$executable->setDisplay($display_id)) {
      return [
        '#markup' => t('Invalid display id @display', array('@display' => $display_id)),
      ];
    }
 
    $executable->setArguments($args);
 
    // Store the current view URL for later use:
    if ($executable->hasUrl() && $executable->display_handler->getOption('path')) {
      $path = $executable->getUrl();
    }
 
    // Make view links come back to preview.
 
    // Also override the current path so we get the pager, and make sure the
    // Request object gets all of the proper values from $_SERVER.
    $request = Request::createFromGlobals();
    $request->attributes->set(RouteObjectInterface::ROUTE_NAME, 'entity.view.preview_form');
    $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, \Drupal::service('router.route_provider')->getRouteByName('entity.view.preview_form'));
    $request->attributes->set('view', $this->storage);
    $request->attributes->set('display_id', $display_id);
    $raw_parameters = new ParameterBag();
    $raw_parameters->set('view', $this->id());
    $raw_parameters->set('display_id', $display_id);
    $request->attributes->set('_raw_variables', $raw_parameters);
 
    foreach ($args as $key => $arg) {
      $request->attributes->set('arg_' . $key, $arg);
    }
    $request_stack->push($request);
 
    // Suppress contextual links of entities within the result set during a
    // Preview.
    // @todo We'll want to add contextual links specific to editing the View, so
    //   the suppression may need to be moved deeper into the Preview pipeline.
    views_ui_contextual_links_suppress_push();
 
    $show_additional_queries = $config->get('ui.show.additional_queries');
 
    Timer::start('entity.view.preview_form');
 
    if ($show_additional_queries) {
      $this->startQueryCapture();
    }
 
    // Execute/get the view preview.
    $preview = $executable->preview($display_id, $args);
 
    if ($show_additional_queries) {
      $this->endQueryCapture();
    }
 
    $this->render_time = Timer::stop('entity.view.preview_form') ['time'];
 
    views_ui_contextual_links_suppress_pop();
 
    // Prepare the query information and statistics to show either above or
    // below the view preview.
    // Initialise the empty rows arrays so we can safely merge them later.
    $rows['query'] = [];
    $rows['statistics'] = [];
    if ($show_info || $show_query || $show_stats) {
      // Get information from the preview for display.
      if (!empty($executable->build_info['query'])) {
        if ($show_query) {
          $query_string = $executable->build_info['query'];
          // Only the sql default class has a method getArguments.
          $quoted = array();
 
          if ($executable->query instanceof Sql) {
            $quoted = $query_string->getArguments();
            $connection = Database::getConnection();
            foreach ($quoted as $key => $val) {
              if (is_array($val)) {
                $quoted[$key] = implode(', ', array_map(array($connection, 'quote'), $val));
              }
              else {
                $quoted[$key] = $connection->quote($val);
              }
            }
          }
          $rows['query'][] = array(
            array(
              'data' => array(
                '#type' => 'inline_template',
                '#template' => "<strong>{% trans 'Query' %}</strong>",
              ),
            ),
            array(
              'data' => array(
                '#type' => 'inline_template',
                '#template' => '<pre>{{ query }}</pre>',
                '#context' => array('query' => strtr($query_string, $quoted)),
              ),
            ),
          );
          if (!empty($this->additionalQueries)) {
            $queries[] = array(
              '#prefix' => '<strong>',
              '#markup' => t('These queries were run during view rendering:'),
              '#suffix' => '</strong>',
            );
            foreach ($this->additionalQueries as $query) {
              $query_string = strtr($query['query'], $query['args']);
              $queries[] = array(
                '#prefix' => "\n",
                '#markup' => t('[@time ms] @query', array('@time' => round($query['time'] * 100000, 1) / 100000.0, '@query' => $query_string)),
              );
            }
 
            $rows['query'][] = array(
              array(
                'data' => array(
                  '#type' => 'inline_template',
                  '#template' => "<strong>{% trans 'Other queries' %}</strong>",
                ),
              ),
              array(
                'data' => array(
                  '#prefix' => '<pre>',
                  'queries' => $queries,
                  '#suffix' => '</pre>',
                ),
              ),
            );
          }
        }
        if ($show_info) {
          $rows['query'][] = array(
            array(
              'data' => array(
                '#type' => 'inline_template',
                '#template' => "<strong>{% trans 'Title' %}</strong>",
              ),
            ),
            Xss::filterAdmin($executable->getTitle()),
          );
          if (isset($path)) {
            // @todo Views should expect and store a leading /. See:
            //   https://www.drupal.org/node/2423913
            $path = \Drupal::l($path->toString(), $path);
          }
          else {
            $path = t('This display has no path.');
          }
          $rows['query'][] = array(
            array(
              'data' => array(
                '#prefix' => '<strong>',
                '#markup' => t('Path'),
                '#suffix' => '</strong>',
              ),
            ),
            array(
              'data' => array(
                '#markup' => $path,
              ),
            )
          );
        }
        if ($show_stats) {
          $rows['statistics'][] = array(
            array(
              'data' => array(
                '#type' => 'inline_template',
                '#template' => "<strong>{% trans 'Query build time' %}</strong>",
              ),
            ),
            t('@time ms', array('@time' => intval($executable->build_time * 100000) / 100)),
          );
 
          $rows['statistics'][] = array(
            array(
              'data' => array(
                '#type' => 'inline_template',
                '#template' => "<strong>{% trans 'Query execute time' %}</strong>",
              ),
            ),
            t('@time ms', array('@time' => intval($executable->execute_time * 100000) / 100)),
          );
 
          $rows['statistics'][] = array(
            array(
              'data' => array(
                '#type' => 'inline_template',
                '#template' => "<strong>{% trans 'View render time' %}</strong>",
              ),
            ),
            t('@time ms', array('@time' => intval($this->render_time * 100) / 100)),
          );
        }
        \Drupal::moduleHandler()->alter('views_preview_info', $rows, $executable);
      }
      else {
        // No query was run. Display that information in place of either the
        // query or the performance statistics, whichever comes first.
        if ($combined || ($show_location === 'above')) {
          $rows['query'][] = array(
            array(
              'data' => array(
                '#prefix' => '<strong>',
                '#markup' => t('Query'),
                '#suffix' => '</strong>',
              ),
            ),
            array(
              'data' => array(
                '#markup' => t('No query was run'),
              ),
            ),
          );
        }
        else {
          $rows['statistics'][] = array(
            array(
              'data' => array(
                '#prefix' => '<strong>',
                '#markup' => t('Query'),
                '#suffix' => '</strong>',
              ),
            ),
            array(
              'data' => array(
                '#markup' => t('No query was run'),
              ),
            ),
          );
        }
      }
    }
  }
  else {
    foreach ($errors as $display_errors) {
      foreach ($display_errors as $error) {
        drupal_set_message($error, 'error');
      }
    }
    $preview = ['#markup' => t('Unable to preview due to validation errors.')];
  }
 
  // Assemble the preview, the query info, and the query statistics in the
  // requested order.
  $table = array(
    '#type' => 'table',
    '#prefix' => '<div class="views-query-info">',
    '#suffix' => '</div>',
    '#rows' => array_merge($rows['query'], $rows['statistics']),
  );
 
  if ($show_location == 'above') {
    $output = [
      'table' => $table,
      'preview' => $preview,
    ];
  }
  else {
    $output = [
      'preview' => $preview,
      'table' => $table,
    ];
  }
 
  // Ensure that we just remove an additional request we pushed earlier.
  // This could happen if $errors was not empty.
  if ($request_stack->getCurrentRequest() != $current_request) {
    $request_stack->pop();
  }
  return $output;
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.