diff --git a/core/includes/pager.inc b/core/includes/pager.inc index 843a3a092714..4c588419f66e 100644 --- a/core/includes/pager.inc +++ b/core/includes/pager.inc @@ -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), ]; diff --git a/core/modules/system/tests/modules/pager_test/pager_test.module b/core/modules/system/tests/modules/pager_test/pager_test.module new file mode 100644 index 000000000000..d30d3d45bcac --- /dev/null +++ b/core/modules/system/tests/modules/pager_test/pager_test.module @@ -0,0 +1,22 @@ + &$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; + } + } +} diff --git a/core/modules/system/tests/src/Functional/Pager/PagerTest.php b/core/modules/system/tests/src/Functional/Pager/PagerTest.php index e0d5c5434462..44828259e193 100644 --- a/core/modules/system/tests/src/Functional/Pager/PagerTest.php +++ b/core/modules/system/tests/src/Functional/Pager/PagerTest.php @@ -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));