3 namespace Drupal\webprofiler\Views;
5 use Drupal\views\ViewExecutable;
6 use Drupal\views\Views;
7 use Symfony\Component\HttpFoundation\Response;
10 * Class TraceableViewExecutable
12 class TraceableViewExecutable extends ViewExecutable {
17 protected $render_time;
20 * Gets the build time.
24 public function getBuildTime() {
25 return $this->build_time;
29 * Gets the execute time.
33 public function getExecuteTime() {
34 return $this->execute_time;
38 * Gets the render time.
42 public function getRenderTime() {
43 return $this->render_time;
49 public function render($display_id = NULL) {
50 $start = microtime(TRUE);
52 $this->execute($display_id);
54 // Check to see if the build failed.
55 if (!empty($this->build_info['fail'])) {
58 if (!empty($this->build_info['denied'])) {
62 /** @var \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginInterface $exposed_form */
63 $exposed_form = $this->display_handler->getPlugin('exposed_form');
64 $exposed_form->preRender($this->result);
66 $module_handler = \Drupal::moduleHandler();
68 // @TODO In the longrun, it would be great to execute a view without
69 // the theme system at all. See https://www.drupal.org/node/2322623.
70 $active_theme = \Drupal::theme()->getActiveTheme();
71 $themes = array_keys($active_theme->getBaseThemes());
72 $themes[] = $active_theme->getName();
74 // Check for already-cached output.
75 /** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache */
76 if (!empty($this->live_preview)) {
77 $cache = Views::pluginManager('cache')->createInstance('none');
80 $cache = $this->display_handler->getPlugin('cache');
83 // Run preRender for the pager as it might change the result.
84 if (!empty($this->pager)) {
85 $this->pager->preRender($this->result);
88 // Initialize the style plugin.
91 if (!isset($this->response)) {
92 // Set the response so other parts can alter it.
93 $this->response = new Response('', 200);
96 // Give field handlers the opportunity to perform additional queries
97 // using the entire resultset prior to rendering.
98 if ($this->style_plugin->usesFields()) {
99 foreach ($this->field as $id => $handler) {
100 if (!empty($this->field[$id])) {
101 $this->field[$id]->preRender($this->result);
106 $this->style_plugin->preRender($this->result);
108 // Let each area handler have access to the result set.
109 $areas = ['header', 'footer'];
110 // Only call preRender() on the empty handlers if the result is empty.
111 if (empty($this->result)) {
114 foreach ($areas as $area) {
115 foreach ($this->{$area} as $handler) {
116 $handler->preRender($this->result);
120 // Let modules modify the view just prior to rendering it.
121 $module_handler->invokeAll('views_pre_render', [$this]);
123 // Let the themes play too, because pre render is a very themey thing.
124 foreach ($themes as $theme_name) {
125 $function = $theme_name . '_views_pre_render';
126 if (function_exists($function)) {
131 $this->display_handler->output = $this->display_handler->render();
133 $exposed_form->postRender($this->display_handler->output);
135 $cache->postRender($this->display_handler->output);
137 // Let modules modify the view output after it is rendered.
138 $module_handler->invokeAll('views_post_render', [
140 &$this->display_handler->output,
144 // Let the themes play too, because post render is a very themey thing.
145 foreach ($themes as $theme_name) {
146 $function = $theme_name . '_views_post_render';
147 if (function_exists($function)) {
148 $function($this, $this->display_handler->output, $cache);
152 $this->render_time = microtime(TRUE) - $start;
154 return $this->display_handler->output;