Commit 716c5abe authored by catch's avatar catch

Issue #1811982 by dawehner, damiankloip: Remove ui preview logic...

Issue #1811982 by dawehner, damiankloip: Remove ui preview logic viewExecuteable::render() and move it to viewUI.
parent 3053c5e9
...@@ -1242,11 +1242,6 @@ public function render($display_id = NULL) { ...@@ -1242,11 +1242,6 @@ public function render($display_id = NULL) {
drupal_theme_initialize(); drupal_theme_initialize();
$config = config('views.settings'); $config = config('views.settings');
$start = microtime(TRUE);
if (!empty($this->live_preview) && $config->get('ui.show.additional_queries')) {
$this->startQueryCapture();
}
$exposed_form = $this->display_handler->getPlugin('exposed_form'); $exposed_form = $this->display_handler->getPlugin('exposed_form');
$exposed_form->pre_render($this->result); $exposed_form->pre_render($this->result);
...@@ -1344,11 +1339,6 @@ public function render($display_id = NULL) { ...@@ -1344,11 +1339,6 @@ public function render($display_id = NULL) {
$function($this, $this->display_handler->output, $cache); $function($this, $this->display_handler->output, $cache);
} }
if (!empty($this->live_preview) && $config->get('ui.show.additional_queries')) {
$this->endQueryCapture();
}
$this->render_time = microtime(TRUE) - $start;
return $this->display_handler->output; return $this->display_handler->output;
} }
...@@ -1716,61 +1706,6 @@ public function getBreadcrumb($set = FALSE) { ...@@ -1716,61 +1706,6 @@ public function getBreadcrumb($set = FALSE) {
return $breadcrumb; return $breadcrumb;
} }
/**
* Set up query capturing.
*
* db_query() stores the queries that it runs in global $queries,
* bit only if dev_query is set to true. In this case, we want
* to temporarily override that setting if it's not and we
* can do that without forcing a db rewrite by just manipulating
* $conf. This is kind of evil but it works.
*/
public function startQueryCapture() {
global $conf, $queries;
if (empty($conf['dev_query'])) {
$this->fix_dev_query = TRUE;
$conf['dev_query'] = TRUE;
}
// Record the last query key used; anything already run isn't
// a query that we are interested in.
$this->last_query_key = NULL;
if (!empty($queries)) {
$keys = array_keys($queries);
$this->last_query_key = array_pop($keys);
}
}
/**
* Add the list of queries run during render to buildinfo.
*
* @see View::start_query_capture()
*/
public function endQueryCapture() {
global $conf, $queries;
if (!empty($this->fix_dev_query)) {
$conf['dev_query'] = FALSE;
}
// make a copy of the array so we can manipulate it with array_splice.
$temp = $queries;
// Scroll through the queries until we get to our last query key.
// Unset anything in our temp array.
if (isset($this->last_query_key)) {
while (list($id, $query) = each($queries)) {
if ($id == $this->last_query_key) {
break;
}
unset($temp[$id]);
}
}
$this->additional_queries = $temp;
}
/** /**
* Creates a duplicate ViewExecutable object. * Creates a duplicate ViewExecutable object.
* *
......
...@@ -114,6 +114,15 @@ class ViewUI implements ViewStorageInterface { ...@@ -114,6 +114,15 @@ class ViewUI implements ViewStorageInterface {
*/ */
protected $executable; protected $executable;
/**
* Stores a list of database queries run beside the main one from views.
*
* @var array
*
* @see \Drupal\Core\Database\Log
*/
protected $additionalQueries;
/** /**
* Constructs a View UI object. * Constructs a View UI object.
* *
...@@ -579,6 +588,29 @@ public function submitItemAdd($form, &$form_state) { ...@@ -579,6 +588,29 @@ public function submitItemAdd($form, &$form_state) {
views_ui_cache_set($this); views_ui_cache_set($this);
} }
/**
* Set up query capturing.
*
* \Drupal\Core\Database\Database stores the queries that it runs, if logging
* is enabled.
*
* @see ViewUI::endQueryCapture()
*/
public function startQueryCapture() {
Database::startLog('views');
}
/**
* Add the list of queries run during render to buildinfo.
*
* @see ViewUI::startQueryCapture()
*/
public function endQueryCapture() {
$queries = Database::getLog('views');
$this->additionalQueries = $queries;
}
public function renderPreview($display_id, $args = array()) { public function renderPreview($display_id, $args = array()) {
// Save the current path so it can be restored before returning from this function. // Save the current path so it can be restored before returning from this function.
$old_q = current_path(); $old_q = current_path();
...@@ -645,7 +677,24 @@ public function renderPreview($display_id, $args = array()) { ...@@ -645,7 +677,24 @@ public function renderPreview($display_id, $args = array()) {
// @todo We'll want to add contextual links specific to editing the View, so // @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. // the suppression may need to be moved deeper into the Preview pipeline.
views_ui_contextual_links_suppress_push(); views_ui_contextual_links_suppress_push();
$show_additional_queries = $config->get('ui.show.additional_queries');
timer_start('views_ui.preview');
if ($show_additional_queries) {
$this->startQueryCapture();
}
// Execute/get the view preview.
$preview = $this->executable->preview($display_id, $args); $preview = $this->executable->preview($display_id, $args);
if ($show_additional_queries) {
$this->endQueryCapture();
}
$this->render_time = timer_stop('views_ui.preview');
views_ui_contextual_links_suppress_pop(); views_ui_contextual_links_suppress_pop();
// Reset variables. // Reset variables.
...@@ -658,12 +707,12 @@ public function renderPreview($display_id, $args = array()) { ...@@ -658,12 +707,12 @@ public function renderPreview($display_id, $args = array()) {
// Get information from the preview for display. // Get information from the preview for display.
if (!empty($this->executable->build_info['query'])) { if (!empty($this->executable->build_info['query'])) {
if ($show_query) { if ($show_query) {
$query = $this->executable->build_info['query']; $query_string = $this->executable->build_info['query'];
// Only the sql default class has a method getArguments. // Only the sql default class has a method getArguments.
$quoted = array(); $quoted = array();
if ($this->executable->query instanceof Sql) { if ($this->executable->query instanceof Sql) {
$quoted = $query->getArguments(); $quoted = $query_string->getArguments();
$connection = Database::getConnection(); $connection = Database::getConnection();
foreach ($quoted as $key => $val) { foreach ($quoted as $key => $val) {
if (is_array($val)) { if (is_array($val)) {
...@@ -674,14 +723,15 @@ public function renderPreview($display_id, $args = array()) { ...@@ -674,14 +723,15 @@ public function renderPreview($display_id, $args = array()) {
} }
} }
} }
$rows['query'][] = array('<strong>' . t('Query') . '</strong>', '<pre>' . check_plain(strtr($query, $quoted)) . '</pre>'); $rows['query'][] = array('<strong>' . t('Query') . '</strong>', '<pre>' . check_plain(strtr($query_string, $quoted)) . '</pre>');
if (!empty($this->executable->additional_queries)) { if (!empty($this->additionalQueries)) {
$queries = '<strong>' . t('These queries were run during view rendering:') . '</strong>'; $queries = '<strong>' . t('These queries were run during view rendering:') . '</strong>';
foreach ($this->executable->additional_queries as $query) { foreach ($this->additionalQueries as $query) {
if ($queries) { if ($queries) {
$queries .= "\n"; $queries .= "\n";
} }
$queries .= t('[@time ms]', array('@time' => intval($query[1] * 100000) / 100)) . ' ' . $query[0]; $query_string = strtr($query['query'], $query['args']);
$queries .= t('[@time ms] @query', array('@time' => round($query['time'] * 100000, 1) / 100000.0, '@query' => $query_string));
} }
$rows['query'][] = array('<strong>' . t('Other queries') . '</strong>', '<pre>' . $queries . '</pre>'); $rows['query'][] = array('<strong>' . t('Other queries') . '</strong>', '<pre>' . $queries . '</pre>');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment