Commit 4f67fb56 authored by dawehner's avatar dawehner Committed by tim.plunkett
Browse files

Issue #1751294 by dawehner: Cleanup pagers: Store pagers on the same levels as query.

parent a639f48c
......@@ -2579,7 +2579,7 @@ function render_pager() {
* Render the 'more' link
*/
function render_more_link() {
if ($this->use_more() && ($this->use_more_always() || (!empty($this->view->query->pager) && $this->view->query->pager->has_more_records()))) {
if ($this->use_more() && ($this->use_more_always() || (!empty($this->view->pager) && $this->view->pager->has_more_records()))) {
$path = $this->get_path();
if ($this->get_option('link_display') == 'custom_url' && $override_path = $this->get_option('link_url')) {
......
......@@ -46,7 +46,7 @@ function render($values) {
// Note: 1 is subtracted from the counter start value below because the
// counter value is incremented by 1 at the end of this function.
$count = is_numeric($this->options['counter_start']) ? $this->options['counter_start'] - 1 : 0;
$pager = $this->view->query->pager;
$pager = $this->view->pager;
// Get the base count of the pager.
if ($pager->use_pager()) {
$count += ($pager->get_items_per_page() * $pager->get_current_page() + $pager->get_offset());
......
......@@ -116,17 +116,6 @@ function set_offset($offset) {
$this->offset = $offset;
}
/**
* Render the pager, if necessary.
*/
function render_pager($exposed_input) {
if (!empty($this->pager) && $this->pager->use_pager()) {
return $this->pager->render($exposed_input);
}
return '';
}
/**
* Create a new grouping for the WHERE or HAVING clause.
*
......
......@@ -92,13 +92,6 @@ class Sql extends QueryPluginBase {
*/
var $get_count_optimized = NULL;
/**
* The current used pager plugin.
*
* @var views_plugin_pager
*/
var $pager = NULL;
/**
* An array mapping table aliases and field names to field aliases.
*/
......@@ -1423,7 +1416,7 @@ function build(&$view) {
$view->init_pager();
// Let the pager modify the query to add limits.
$this->pager->query();
$view->pager->query();
$view->build_info['query'] = $this->query();
$view->build_info['count_query'] = $this->query(TRUE);
......@@ -1481,12 +1474,12 @@ function execute(&$view) {
try {
if ($this->pager->use_count_query() || !empty($view->get_total_rows)) {
$this->pager->execute_count_query($count_query);
if ($view->pager->use_count_query() || !empty($view->get_total_rows)) {
$view->pager->execute_count_query($count_query);
}
// Let the pager modify the query to add limits.
$this->pager->pre_execute($query);
$view->pager->pre_execute($query);
if (!empty($this->limit) || !empty($this->offset)) {
// We can't have an offset without a limit, so provide a very large limit instead.
......@@ -1502,10 +1495,10 @@ function execute(&$view) {
$view->result[] = $item;
}
$this->pager->post_execute($view->result);
$view->pager->post_execute($view->result);
if ($this->pager->use_count_query() || !empty($view->get_total_rows)) {
$view->total_rows = $this->pager->get_total_items();
if ($view->pager->use_count_query() || !empty($view->get_total_rows)) {
$view->total_rows = $view->pager->get_total_items();
}
}
catch (Exception $e) {
......
......@@ -149,9 +149,9 @@ public function testNoLimit() {
$this->assertEqual(count($view->result), 8, 'Make sure that every item beside the first three is returned in the result');
// Check some public functions.
$this->assertFalse($view->query->pager->use_pager());
$this->assertFalse($view->query->pager->use_count_query());
$this->assertEqual($view->query->pager->get_items_per_page(), 0);
$this->assertFalse($view->pager->use_pager());
$this->assertFalse($view->pager->use_count_query());
$this->assertEqual($view->pager->get_items_per_page(), 0);
}
public function viewsPagerNoLimit() {
......@@ -226,8 +226,8 @@ public function testLimit() {
$this->assertEqual(count($view->result), 3, 'Make sure that only a certain count of items is returned');
// Check some public functions.
$this->assertFalse($view->query->pager->use_pager());
$this->assertFalse($view->query->pager->use_count_query());
$this->assertFalse($view->pager->use_pager());
$this->assertFalse($view->pager->use_count_query());
}
public function viewsPagerLimit() {
......@@ -310,7 +310,7 @@ public function testNormalPager() {
$view->display_handler->set_option('pager', $pager);
$this->executeView($view);
$this->assertEqual($view->query->pager->get_items_per_page(), 0);
$this->assertEqual($view->pager->get_items_per_page(), 0);
$this->assertEqual(count($view->result), 11);
}
......@@ -438,7 +438,7 @@ public function testRenderNullPager() {
$view->set_display('default');
$this->executeView($view);
$view->use_ajax = TRUE; // force the value again here
$view->query->pager = NULL;
$view->pager = NULL;
$output = $view->render();
$this->assertEqual(preg_match('/<ul class="pager">/', $output), 0, t('The pager is not rendered.'));
}
......@@ -476,21 +476,21 @@ function testPagerApi() {
$rand_number = rand(1, 5);
$view->set_items_per_page($rand_number);
$rand_number = rand(6, 11);
$view->query->pager->set_items_per_page($rand_number);
$view->pager->set_items_per_page($rand_number);
$this->assertEqual($view->get_items_per_page(), $rand_number, 'Make sure get_items_per_page uses the settings of set_items_per_page.');
$this->assertEqual($view->get_offset(), 0, 'Per default a view has a 0 offset.');
$rand_number = rand(1, 5);
$view->set_offset($rand_number);
$rand_number = rand(6, 11);
$view->query->pager->set_offset($rand_number);
$view->pager->set_offset($rand_number);
$this->assertEqual($view->get_offset(), $rand_number, 'Make sure get_offset uses the settings of set_offset.');
$this->assertEqual($view->get_current_page(), 0, 'Per default the current page is 0.');
$rand_number = rand(1, 5);
$view->set_current_page($rand_number);
$rand_number = rand(6, 11);
$view->query->pager->set_current_page($rand_number);
$view->pager->set_current_page($rand_number);
$this->assertEqual($view->get_current_page(), $rand_number, 'Make sure get_current_page uses the settings of set_current_page.');
}
......
......@@ -154,7 +154,14 @@ class View extends ViewsDbObject {
*/
var $query = NULL;
/**
/**
* The used pager plugin used by the current executed view.
*
* @var Drupal\views\Plugin\views\pager\PagerPluginBase
*/
public $pager = NULL;
/**
* The current used display plugin.
*
* @var views_plugin_display
......@@ -324,10 +331,9 @@ function set_current_page($page) {
$this->current_page = $page;
// If the pager is already initialized, pass it through to the pager.
if (!empty($this->query->pager)) {
return $this->query->pager->set_current_page($page);
if (!empty($this->pager)) {
return $this->pager->set_current_page($page);
}
}
/**
......@@ -335,8 +341,8 @@ function set_current_page($page) {
*/
function get_current_page() {
// If the pager is already initialized, pass it through to the pager.
if (!empty($this->query->pager)) {
return $this->query->pager->get_current_page();
if (!empty($this->pager)) {
return $this->pager->get_current_page();
}
if (isset($this->current_page)) {
......@@ -349,8 +355,8 @@ function get_current_page() {
*/
function get_items_per_page() {
// If the pager is already initialized, pass it through to the pager.
if (!empty($this->query->pager)) {
return $this->query->pager->get_items_per_page();
if (!empty($this->pager)) {
return $this->pager->get_items_per_page();
}
if (isset($this->items_per_page)) {
......@@ -365,8 +371,8 @@ function set_items_per_page($items_per_page) {
$this->items_per_page = $items_per_page;
// If the pager is already initialized, pass it through to the pager.
if (!empty($this->query->pager)) {
$this->query->pager->set_items_per_page($items_per_page);
if (!empty($this->pager)) {
$this->pager->set_items_per_page($items_per_page);
}
}
......@@ -375,8 +381,8 @@ function set_items_per_page($items_per_page) {
*/
function get_offset() {
// If the pager is already initialized, pass it through to the pager.
if (!empty($this->query->pager)) {
return $this->query->pager->get_offset();
if (!empty($this->pager)) {
return $this->pager->get_offset();
}
if (isset($this->offset)) {
......@@ -391,8 +397,8 @@ function set_offset($offset) {
$this->offset = $offset;
// If the pager is already initialized, pass it through to the pager.
if (!empty($this->query->pager)) {
$this->query->pager->set_offset($offset);
if (!empty($this->pager)) {
$this->pager->set_offset($offset);
}
}
......@@ -400,8 +406,8 @@ function set_offset($offset) {
* Determine if the pager actually uses a pager.
*/
function use_pager() {
if (!empty($this->query->pager)) {
return $this->query->pager->use_pager();
if (!empty($this->pager)) {
return $this->pager->use_pager();
}
}
......@@ -699,25 +705,36 @@ function init_handlers() {
* to allow for overrides.
*/
function init_pager() {
if (empty($this->query->pager)) {
$this->query->pager = $this->display_handler->get_plugin('pager');
if (!isset($this->pager)) {
$this->pager = $this->display_handler->get_plugin('pager');
if ($this->query->pager->use_pager()) {
$this->query->pager->set_current_page($this->current_page);
if ($this->pager->use_pager()) {
$this->pager->set_current_page($this->current_page);
}
// These overrides may have been set earlier via $view->set_*
// functions.
if (isset($this->items_per_page)) {
$this->query->pager->set_items_per_page($this->items_per_page);
$this->pager->set_items_per_page($this->items_per_page);
}
if (isset($this->offset)) {
$this->query->pager->set_offset($this->offset);
$this->pager->set_offset($this->offset);
}
}
}
/**
* Render the pager, if necessary.
*/
public function render_pager($exposed_input) {
if (!empty($this->pager) && $this->pager->use_pager()) {
return $this->pager->render($exposed_input);
}
return '';
}
/**
* Create a list of base tables eligible for this view. Used primarily
* for the UI. Display must be already initialized.
......@@ -1158,9 +1175,9 @@ function execute($display_id = NULL) {
$cache = $this->display_handler->get_plugin('cache');
}
if ($cache && $cache->cache_get('results')) {
if ($this->query->pager->use_pager()) {
$this->query->pager->total_items = $this->total_rows;
$this->query->pager->update_page_info();
if ($this->pager->use_pager()) {
$this->pager->total_items = $this->total_rows;
$this->pager->update_page_info();
}
vpr('Used cached results');
}
......@@ -1236,8 +1253,8 @@ function render($display_id = NULL) {
}
// Run pre_render for the pager as it might change the result.
if (!empty($this->query->pager)) {
$this->query->pager->pre_render($this->result);
if (!empty($this->pager)) {
$this->pager->pre_render($this->result);
}
// Initialize the style plugin.
......
......@@ -87,7 +87,7 @@ function template_preprocess_views_view(&$vars) {
if ($view->display_handler->render_pager()) {
$exposed_input = isset($view->exposed_raw_input) ? $view->exposed_raw_input : NULL;
$vars['pager'] = $view->query->render_pager($exposed_input);
$vars['pager'] = $view->render_pager($exposed_input);
}
$vars['attachment_before'] = !empty($view->attachment_before) ? $view->attachment_before : '';
......@@ -134,7 +134,7 @@ function template_preprocess_views_view(&$vars) {
'view_dom_id' => $vars['dom_id'],
// To fit multiple views on a page, the programmer may have
// overridden the display's pager_element.
'pager_element' => isset($view->query->pager) ? $view->query->pager->get_pager_id() : 0,
'pager_element' => isset($view->pager) ? $view->pager->get_pager_id() : 0,
),
),
),
......
......@@ -960,7 +960,7 @@ function hook_views_pre_render(&$view) {
function hook_views_post_render(&$view, &$output, &$cache) {
// When using full pager, disable any time-based caching if there are less
// then 10 results.
if ($view->query->pager instanceof views_plugin_pager_full && $cache->options['type'] == 'time' && count($view->result) < 10) {
if ($view->pager instanceof Drupal\views\Plugin\views\pager\Full && $cache->options['type'] == 'time' && count($view->result) < 10) {
$cache['options']['results_lifespan'] = 0;
$cache['options']['output_lifespan'] = 0;
}
......
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