Issue #3067584 by idebr, Niklan, andypost: Pager template renders attributes that do not exist

merge-requests/55/head
catch 2019-07-18 15:16:20 +01:00
parent 74ddb4b305
commit 087e020d56
3 changed files with 33 additions and 0 deletions

View File

@ -5,6 +5,7 @@
* Functions to aid in presenting database results as a set of pages.
*/
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Component\Utility\Html;
@ -219,6 +220,7 @@ function template_preprocess_pager(&$variables) {
// Create the "first" and "previous" links if we are not on the first page.
if ($pager_page_array[$element] > 0) {
$items['first'] = [];
$items['first']['attributes'] = new Attribute();
$options = [
'query' => pager_query_add_page($parameters, $element, 0),
];
@ -228,6 +230,7 @@ function template_preprocess_pager(&$variables) {
}
$items['previous'] = [];
$items['previous']['attributes'] = new Attribute();
$options = [
'query' => pager_query_add_page($parameters, $element, $pager_page_array[$element] - 1),
];
@ -248,6 +251,7 @@ function template_preprocess_pager(&$variables) {
'query' => pager_query_add_page($parameters, $element, $i - 1),
];
$items['pages'][$i]['href'] = Url::fromRoute($route_name, $route_parameters, $options)->toString();
$items['pages'][$i]['attributes'] = new Attribute();
if ($i == $pager_current) {
$variables['current'] = $i;
}
@ -261,6 +265,7 @@ function template_preprocess_pager(&$variables) {
// Create the "next" and "last" links if we are not on the last page.
if ($pager_page_array[$element] < ($pager_max - 1)) {
$items['next'] = [];
$items['next']['attributes'] = new Attribute();
$options = [
'query' => pager_query_add_page($parameters, $element, $pager_page_array[$element] + 1),
];
@ -270,6 +275,7 @@ function template_preprocess_pager(&$variables) {
}
$items['last'] = [];
$items['last']['attributes'] = new Attribute();
$options = [
'query' => pager_query_add_page($parameters, $element, $pager_max - 1),
];

View File

@ -0,0 +1,22 @@
<?php
/**
* @file
* Hook implementations for this module.
*/
/**
* Implements hook_preprocess_HOOK().
*/
function pager_test_preprocess_pager(&$variables) {
foreach ($variables['items']['pages'] as $index => &$pager_item) {
$pager_item['attributes']['pager-test'] = 'yes';
}
unset($pager_item);
foreach (['first', 'previous', 'next', 'last'] as $special_pager_item) {
if (isset($variables['items'][$special_pager_item])) {
$variables['items'][$special_pager_item]['attributes']['pager-test'] = $special_pager_item;
}
}
}

View File

@ -250,6 +250,7 @@ class PagerTest extends BrowserTestBase {
$this->assertClass($element, 'pager__item', "Element for page $page has .pager__item class.");
$link = $element->find('css', 'a');
$this->assertTrue($link, "Link to page $page found.");
$this->assertEqual($link->getAttribute('pager-test'), 'yes', "Pager link for page $page has an attribute set in pager_test_preprocess_pager()");
$destination = $link->getAttribute('href');
$this->assertEqual($destination, '?page=' . ($page - 1));
}
@ -264,6 +265,7 @@ class PagerTest extends BrowserTestBase {
$link = $first->find('css', 'a');
$this->assertTrue($link, 'Link to first page found.');
$this->assertNoClass($link, 'is-active', 'Link to first page is not active.');
$this->assertEqual($link->getAttribute('pager-test'), 'first', 'Pager link for first page has an attribute set in pager_test_preprocess_pager()');
$destination = $link->getAttribute('href');
$this->assertEqual($destination, '?page=0');
}
@ -272,6 +274,7 @@ class PagerTest extends BrowserTestBase {
$link = $previous->find('css', 'a');
$this->assertTrue($link, 'Link to previous page found.');
$this->assertNoClass($link, 'is-active', 'Link to previous page is not active.');
$this->assertEqual($link->getAttribute('pager-test'), 'previous', 'Pager link for previous page has an attribute set in pager_test_preprocess_pager()');
$destination = $link->getAttribute('href');
// URL query string param is 0-indexed, $current_page is 1-indexed.
$this->assertEqual($destination, '?page=' . ($current_page - 2));
@ -281,6 +284,7 @@ class PagerTest extends BrowserTestBase {
$link = $next->find('css', 'a');
$this->assertTrue($link, 'Link to next page found.');
$this->assertNoClass($link, 'is-active', 'Link to next page is not active.');
$this->assertEqual($link->getAttribute('pager-test'), 'next', 'Pager link for next page has an attribute set in pager_test_preprocess_pager()');
$destination = $link->getAttribute('href');
// URL query string param is 0-indexed, $current_page is 1-indexed.
$this->assertEqual($destination, '?page=' . $current_page);
@ -290,6 +294,7 @@ class PagerTest extends BrowserTestBase {
$this->assertClass($last, 'pager__item--last', 'Element for last page has .pager__item--last class.');
$this->assertTrue($link, 'Link to last page found.');
$this->assertNoClass($link, 'is-active', 'Link to last page is not active.');
$this->assertEqual($link->getAttribute('pager-test'), 'last', 'Pager link for last page has an attribute set in pager_test_preprocess_pager()');
$destination = $link->getAttribute('href');
// URL query string param is 0-indexed.
$this->assertEqual($destination, '?page=' . ($total_pages - 1));