Commit 599bf8be authored by mondrake's avatar mondrake Committed by mondrake
Browse files

Issue #3091945 by mondrake: (follow-up) Allow Pagerer to override the pager...

Issue #3091945 by mondrake: (follow-up) Allow Pagerer to override the pager querystring part in URLs and count pages from 1, not 0
parent 55e8a2f6
......@@ -2,35 +2,14 @@
namespace Drupal\pagerer\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Main Pagerer URL settings admin form.
*/
class PagererUrlConfigForm extends ConfigFormBase {
/**
* Constructs a PagererConfigForm object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
*/
public function __construct(ConfigFactoryInterface $config_factory) {
parent::__construct($config_factory);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory')
);
}
/**
* {@inheritdoc}
*/
......
......@@ -15,7 +15,7 @@ class PagererManager extends PagerManager {
/**
* Whether the URL querystring pager key is overridden.
*
* @var boolean
* @var bool
*/
protected $querystringOverride;
......@@ -61,6 +61,13 @@ class PagererManager extends PagerManager {
return $pager;
}
/**
* {@inheritdoc}
*/
public function getUpdatedParameters(array $query, $element, $index) {
return $this->getPagererUpdatedParameters($this->getPager($element), $query, $index);
}
/**
* Saves a pager to the static cache.
*
......@@ -76,13 +83,6 @@ class PagererManager extends PagerManager {
}
}
/**
* {@inheritdoc}
*/
public function getUpdatedParameters(array $query, $element, $index) {
return $this->getPagererUpdatedParameters($this->getPager($element), $query, $index);
}
/**
* Gets the URL query parameter array of a pager link.
*
......@@ -137,19 +137,19 @@ class PagererManager extends PagerManager {
}
if (!$this->querystringOverride) {
// @todo legacy
// Legacy URL format.
$parameters[$this->querystringKey] = implode(',', $page_el);
if (!empty($page_ak)) {
$parameters['page_ak'] = implode(',', $page_ak);
}
}
else {
// @todo pagerer
// Pagerer URL format.
$page = implode(PagererParameters::VALUE_SEPARATOR, $page_el);
if (!empty($page_ak)) {
$ak = implode(PagererParameters::VALUE_SEPARATOR, $page_ak);
}
$parameters[$this->querystringKey] = $page . (isset($ak) ? '-ak-' . $ak : NULL);
$parameters[$this->querystringKey] = $page . (isset($ak) ? PagererParameters::ITEM_SEPARATOR . 'ak' . PagererParameters::ITEM_SEPARATOR . $ak : NULL);
}
// Merge the updated pager query parameters, with any parameters coming from
......@@ -171,10 +171,27 @@ class PagererManager extends PagerManager {
return $parameters;
}
/**
* Returns an adaptive keys fragment for use on the URL.
*
* @param int[] $ak
* The adaptive keys array, in the format 'L,R,X', where L is the adaptive
* lock to left page, R is the adaptive lock to right page, and X is the
* adaptive center lock for calculation of neighborhood.
* @param int $last_page
* The last page of the queryset.
*
* @return string|null
* The 0-based or 1-based adaptive keys fragment, depending on the
* configuration.
*/
protected function adaptiveKeysToUrl(array $ak, int $last_page) {
if (empty($ak)) {
return NULL;
}
if (!isset($ak[2]) && ($ak[0] ?? 0) === 0 && ($ak[1] ?? $last_page) === $last_page) {
return NULL;
}
$tmp[0] = $this->pageIndexToUrl($ak[0] ?? 0);
$tmp[1] = $this->pageIndexToUrl($ak[1] ?? $last_page);
if (isset($ak[2])) {
......@@ -183,6 +200,15 @@ class PagererManager extends PagerManager {
return implode($this->querystringOverride ? '_' : '.', $tmp);
}
/**
* Returns a page index for use on the URL.
*
* @param int $page
* The target page.
*
* @return int
* A 0-based or 1-based page index, depending on the configuration.
*/
protected function pageIndexToUrl(int $page): int {
return $page + $this->base;
}
......
......@@ -11,8 +11,16 @@ use Drupal\Tests\comment\Functional\CommentInterfaceTest;
*/
class CorePagerReplaceCommentInterfaceTest extends CommentInterfaceTest {
/**
* The URL for Pagerer admin UI page.
*
* @var string
*/
protected $pagererAdmin = 'admin/config/user-interface/pagerer';
/**
* {@inheritdoc}
*/
public static $modules = ['dblog', 'pagerer', 'comment'];
/**
......
<?php
namespace Drupal\Tests\pagerer\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\system\Functional\Pager\PagerTest;
/**
* Test replacement of Drupal core pager.
*
* @group Pagerer
*/
class CorePagerReplaceTest extends PagerTest {
protected $pagererAdmin = 'admin/config/user-interface/pagerer';
public static $modules = [
'dblog',
'image',
'pager_test',
'pagerer',
'pagerer_test',
];
/**
* {@inheritdoc}
*/
public function setUp() {
BrowserTestBase::setUp();
// Insert 300 log messages.
$logger = $this->container->get('logger.factory')->get('pager_test');
for ($i = 0; $i < 300; $i++) {
$logger->debug($this->randomString());
}
$this->drupalLogin($this->drupalCreateUser([
'access site reports',
'administer image styles',
'administer site configuration',
]));
$edit = [
'label' => 'core_replace',
'id' => 'core_replace',
];
$this->drupalPostForm($this->pagererAdmin . '/preset/add', $edit, 'Create');
$edit = [
'core_override_preset' => 'core_replace',
];
$this->drupalPostForm($this->pagererAdmin, $edit, 'Save configuration');
}
/**
* Test that pagerer specific cache tags have been added.
*/
public function testPagerQueryParametersAndCacheContext() {
parent::testPagerQueryParametersAndCacheContext();
$this->assertCacheTag('config:pagerer.settings');
$this->assertCacheTag('config:pagerer.preset.core_replace');
}
}
<?php
namespace Drupal\Tests\pagerer\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\system\Functional\Pager\PagerTest;
/**
* Test replacement of Drupal core pager.
*
* @group Pagerer
*/
class CorePagerReplaceTest extends PagerTest {
/**
* The URL for Pagerer admin UI page.
*
* @var string
*/
protected $pagererAdmin = 'admin/config/user-interface/pagerer';
/**
* {@inheritdoc}
*/
public static $modules = [
'dblog',
'image',
'pager_test',
'pagerer',
'pagerer_test',
];
/**
* {@inheritdoc}
*/
public function setUp() {
BrowserTestBase::setUp();
// Insert 300 log messages.
$logger = $this->container->get('logger.factory')->get('pager_test');
for ($i = 0; $i < 300; $i++) {
$logger->debug($this->randomString());
}
$this->drupalLogin($this->drupalCreateUser([
'access site reports',
'administer image styles',
'administer site configuration',
]));
// Replace the core pager.
$edit = [
'label' => 'core_replace',
'id' => 'core_replace',
];
$this->drupalPostForm($this->pagererAdmin . '/preset/add', $edit, 'Create');
$edit = [
'core_override_preset' => 'core_replace',
];
$this->drupalPostForm($this->pagererAdmin, $edit, 'Save configuration');
}
/**
* Test that pagerer specific cache tags have been added.
*/
public function testPagerQueryParametersAndCacheContext() {
parent::testPagerQueryParametersAndCacheContext();
$this->assertCacheTag('config:pagerer.settings');
$this->assertCacheTag('config:pagerer.preset.core_replace');
}
}
This diff is collapsed.
......@@ -11,6 +11,11 @@ use Drupal\Tests\views\FunctionalJavascript\PaginationAJAXTest;
*/
class CorePagerReplacePaginationAJAXTest extends PaginationAJAXTest {
/**
* The URL for Pagerer admin UI page.
*
* @var string
*/
protected $pagererAdmin = 'admin/config/user-interface/pagerer';
/**
......
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