Commit b7a6866c authored by alexpott's avatar alexpott

Issue #1588138 by mondrake: pager_query_add_page() overrides parameters passed programmatically

parent 629d4cbf
......@@ -304,8 +304,11 @@ function pager_query_add_page(array $query, $element, $index) {
if ($new_page = implode(',', pager_load_array($page_new[$element], $element, explode(',', $page)))) {
$query['page'] = $new_page;
}
if ($query_pager = pager_get_query_parameters()) {
$query = array_merge($query, $query_pager);
// Merge the query parameters passed to this function with the parameters
// from the current request. In case of collision, the parameters passed
// into this function take precedence.
if ($current_request_query = pager_get_query_parameters()) {
$query = array_merge($current_request_query, $query);
}
return $query;
}
......
......@@ -2,7 +2,7 @@
/**
* @file
* Definition of Drupal\system\Tests\Pager\PagerTest.
* Contains \Drupal\system\Tests\Pager\PagerTest.
*/
namespace Drupal\system\Tests\Pager;
......@@ -21,7 +21,7 @@ class PagerTest extends WebTestBase {
*
* @var array
*/
public static $modules = array('dblog');
public static $modules = array('dblog', 'pager_test');
protected $profile = 'testing';
......@@ -62,6 +62,25 @@ function testActiveClass() {
$this->assertPagerItems($current_page);
}
/**
* Test proper functioning of the query parameters.
*/
protected function testPagerQueryParameters() {
// First page.
$this->drupalGet('pager-test/query-parameters');
$this->assertText(t('Pager calls: 0'), 'Initial call to pager shows 0 calls.');
// Go to last page, the count of pager calls need to go to 1.
$elements = $this->xpath('//li[contains(@class, :class)]/a', array(':class' => 'pager__item--last'));
$this->drupalGet($GLOBALS['base_root'] . $elements[0]['href'], array('external' => TRUE));
$this->assertText(t('Pager calls: 1'), 'First link call to pager shows 1 calls.');
// Go back to first page, the count of pager calls need to go to 2.
$elements = $this->xpath('//li[contains(@class, :class)]/a', array(':class' => 'pager__item--first'));
$this->drupalGet($GLOBALS['base_root'] . $elements[0]['href'], array('external' => TRUE));
$this->assertText(t('Pager calls: 2'), 'Second link call to pager shows 2 calls.');
}
/**
* Asserts pager items and links.
*
......
type: module
core: 8.x
name: 'Pager Test'
description: 'Support module for pager tests.'
package: Testing
version: VERSION
pager_test.query_parameters:
path: '/pager-test/query-parameters'
defaults:
_title: 'Pager using query parameters for testing'
_controller: '\Drupal\pager_test\Controller\PagerTestController::queryParameters'
requirements:
_access: 'TRUE'
<?php
/**
* @file
* Contains \Drupal\pager_test\Controller\PagerTestController.
*/
namespace Drupal\pager_test\Controller;
use Drupal\Core\Controller\ControllerBase;
/**
* Controller routine for testing the pager.
*/
class PagerTestController extends ControllerBase {
/**
* Returns a pager with 'parameters' variable.
*
* The 'pager_calls' parameter counts the calls to the pager, subsequent
* to the initial call.
*/
public function queryParameters() {
// Example query.
$header_0 = array(
array('data' => 'wid'),
array('data' => 'type'),
array('data' => 'timestamp'),
);
$query_0 = db_select('watchdog', 'd')->extend('Drupal\Core\Database\Query\PagerSelectExtender')->element(0);
$query_0->fields('d', array('wid', 'type', 'timestamp'));
$result_0 = $query_0
->limit(5)
->orderBy('d.wid')
->execute();
$rows_0 = array();
foreach ($result_0 as $row) {
$rows_0[] = array('data' => (array) $row);
}
$build['pager_table_0'] = array(
'#theme' => 'table',
'#header' => $header_0,
'#rows' => $rows_0,
'#empty' => $this->t("There are no watchdog records found in the db"),
);
// Counter of calls to the current pager.
$query_params = pager_get_query_parameters();
$pager_calls = isset($query_params['pager_calls']) ? ($query_params['pager_calls'] ? $query_params['pager_calls'] : 0) : 0;
$build['l_pager_pager_0'] = array('#markup' => $this->t('Pager calls: @pager_calls', array('@pager_calls' => $pager_calls)));
// Pager.
$build['pager_pager_0'] = array(
'#theme' => 'pager',
'#element' => 0,
'#parameters' => array(
'pager_calls' => ++$pager_calls,
),
);
return $build;
}
}
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