Problem/Motivation

When using the Claro theme, custom classes added to pager links do not appear. Claro defines its own class for pager links but does not add any custom classes. Normally, custom classes can be added to themes using template hooks.

Steps to reproduce

  1. Install fresh Drupal setup
  2. Enable Claro theme
  3. Create a view with pager enabled
  4. Add a template_preprocess_pager hook and add a test class (see example below)
  5. Go to the view and inspect the pager markup
  6. Result: The test class is not added to the pager
  7. Expected: The test class is added to the pager

Example test Hook:

/**
 * Implements template_preprocess_pager().
 */
function claro_preprocess_pager(&$vars) {
  if (isset($vars['pager'])) {
    if (count($vars['items'])) {
      foreach ($vars['items'] as $key => $value) {
        // print_r('<pre>');
        // var_dump($value);
        if (!empty($value['attributes'])) {
          $vars['items'][$key]['attributes']->addClass('test-pager-class');
        }
        else {
          if (count($value) > 3) {
            foreach ($value as $subKey => $subValue) {
              if (!empty($subValue['attributes'])) {
                $vars['items'][$key][$subKey]['attributes']->addClass('test-pager-class');
              }
            }
          }
        }
      }
    }
  }
}

Screenshots

Before:

After:

Proposed resolution

Update pager.html.twig to inject custom classes. See other themes for examples.

Remaining tasks

  1. Create patch
  2. Test patch
  3. Review patch
  4. Commit

User interface changes

API changes

Data model changes

Release notes snippet

Comments

dpi created an issue. See original summary.

dpi’s picture

Assigned: dpi » Unassigned
Status: Active » Needs review
StatusFileSize
new3.8 KB
dpi’s picture

Issue summary: View changes
acbramley’s picture

Nice work, code looks good to me! I think we could add a test for this by adding a preprocess hook to a test module?

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

hardik_patel_12’s picture

StatusFileSize
new2.35 KB

Re-rolling patch against 9.1.x-dev. Kindly review a patch.

akshay kashyap’s picture

StatusFileSize
new30.74 KB

I have tested #6 patch it's working fine for me I have attached the screenshot for reference

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

djsagar’s picture

StatusFileSize
new144.36 KB

This issue no longer reproducible in 9.2.x-dev.
Attaching the before patch screenshot for reference.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

kim.pepper’s picture

Issue tags: +#pnx-sprint
vikashsoni’s picture

Applied patch #6 applied successfully

for ref sharing screenshot.....

vikashsoni’s picture

StatusFileSize
new18.5 KB
new14.32 KB

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

kristen pol’s picture

Thanks @vikashsoni but I don't understand what you tested because your before and after screenshots have the same classes. You have to add code to be able to test this properly. See #4 for guidance.

Please add testing steps when testing and ideally embed the screenshots within the comment so it's easier to see the screenshots. If you install the dreditor browser extension, then you'll see an "Embed" button next to each file.

Note that the testing in #7 and #9 also were not done correctly.

I checked and the patch applies cleanly on 9.3, 9.4, and 10 and should be tested on all 3, so tagging. The formatting of the issue summary isn't using the template so could be cleaned up as well.

kristen pol’s picture

Also compared the changes with the other core themes and it's the same approach though Claro is the only one adding additional classes. One other difference is that claro/templates/views/views-mini-pager.html.twig isn't patched and handles the classes differently.

[drupal-9.3.x-dev/9.3.x] [themes]$ grep -r "items.first.attributes|without('href', 'title')" .
./stable/templates/navigation/pager.html.twig:          <a href="{{ items.first.href }}" title="{{ 'Go to first page'|t }}"{{ items.first.attributes|without('href', 'title') }}>
./stable9/templates/navigation/pager.html.twig:          <a href="{{ items.first.href }}" title="{{ 'Go to first page'|t }}"{{ items.first.attributes|without('href', 'title') }}>
./claro/templates/pager.html.twig:          <a href="{{ items.first.href }}" title="{{ 'Go to first page'|t }}"{{ items.first.attributes|without('href', 'title').addClass('pager__link pager__link--action-link') }}>
./classy/templates/navigation/pager.html.twig:          <a href="{{ items.first.href }}" title="{{ 'Go to first page'|t }}"{{ items.first.attributes|without('href', 'title') }}>
./starterkit_theme/templates/navigation/pager.html.twig:          <a href="{{ items.first.href }}" title="{{ 'Go to first page'|t }}"{{ items.first.attributes|without('href', 'title') }}>
[drupal-9.3.x-dev/9.3.x] [themes]$ grep -r "items.previous.attributes|without('href', 'title', 'rel')" .
./stable/templates/navigation/pager.html.twig:          <a href="{{ items.previous.href }}" title="{{ 'Go to previous page'|t }}" rel="prev"{{ items.previous.attributes|without('href', 'title', 'rel') }}>
./stable/templates/views/views-mini-pager.html.twig:          <a href="{{ items.previous.href }}" title="{{ 'Go to previous page'|t }}" rel="prev"{{ items.previous.attributes|without('href', 'title', 'rel') }}>
./stable9/templates/navigation/pager.html.twig:          <a href="{{ items.previous.href }}" title="{{ 'Go to previous page'|t }}" rel="prev"{{ items.previous.attributes|without('href', 'title', 'rel') }}>
./stable9/templates/views/views-mini-pager.html.twig:          <a href="{{ items.previous.href }}" title="{{ 'Go to previous page'|t }}" rel="prev"{{ items.previous.attributes|without('href', 'title', 'rel') }}>
./bartik/templates/classy/views/views-mini-pager.html.twig:          <a href="{{ items.previous.href }}" title="{{ 'Go to previous page'|t }}" rel="prev"{{ items.previous.attributes|without('href', 'title', 'rel') }}>
./claro/templates/pager.html.twig:          <a href="{{ items.previous.href }}" title="{{ 'Go to previous page'|t }}" rel="prev"{{ items.previous.attributes|without('href', 'title', 'rel').addClass('pager__link pager__link--action-link') }}>
./classy/templates/navigation/pager.html.twig:          <a href="{{ items.previous.href }}" title="{{ 'Go to previous page'|t }}" rel="prev"{{ items.previous.attributes|without('href', 'title', 'rel') }}>
./classy/templates/views/views-mini-pager.html.twig:          <a href="{{ items.previous.href }}" title="{{ 'Go to previous page'|t }}" rel="prev"{{ items.previous.attributes|without('href', 'title', 'rel') }}>
./seven/templates/classy/views/views-mini-pager.html.twig:          <a href="{{ items.previous.href }}" title="{{ 'Go to previous page'|t }}" rel="prev"{{ items.previous.attributes|without('href', 'title', 'rel') }}>
./starterkit_theme/templates/navigation/pager.html.twig:          <a href="{{ items.previous.href }}" title="{{ 'Go to previous page'|t }}" rel="prev"{{ items.previous.attributes|without('href', 'title', 'rel') }}>
./starterkit_theme/templates/views/views-mini-pager.html.twig:          <a href="{{ items.previous.href }}" title="{{ 'Go to previous page'|t }}" rel="prev"{{ items.previous.attributes|without('href', 'title', 'rel') }}>
[drupal-9.3.x-dev/9.3.x] [themes]$ grep -r "item.attributes|without('href', 'title')" .
./stable/templates/navigation/pager.html.twig:          <a href="{{ item.href }}" title="{{ title }}"{{ item.attributes|without('href', 'title') }}>
./stable9/templates/navigation/pager.html.twig:          <a href="{{ item.href }}" title="{{ title }}"{{ item.attributes|without('href', 'title') }}>
./claro/templates/pager.html.twig:          <a href="{{ item.href }}" title="{{ title }}"{{ item.attributes|without('href', 'title').addClass(['pager__link', current == key ? ' is-active']) }}>
./classy/templates/navigation/pager.html.twig:          <a href="{{ item.href }}" title="{{ title }}"{{ item.attributes|without('href', 'title') }}>
./starterkit_theme/templates/navigation/pager.html.twig:          <a href="{{ item.href }}" title="{{ title }}"{{ item.attributes|without('href', 'title') }}>
kristen pol’s picture

Title: Custom classes added to pager links dont work with Claro » Custom classes for pager links do not work with Claro theme

Fixing title.

PSA: You have to add code to be able to test this properly. See #4 for guidance.

joshua1234511’s picture

Issue summary: View changes
Issue tags: -Needs manual testing
StatusFileSize
new132.15 KB
new159.22 KB

Tested the Patch provided in #6
Works Correctly on v9.3.0 v9.4.0
Steps Followed
- Install fresh Drupal setup
- Enable Carlo theme
- Create a view with pager enabled
- Added a template_preprocess_pager hook and added a test class
- Inspect the html code the preprocessed class won't be added.
- Applied the patch
- Cleared cache
- The class is added to the links.
Before Patch
Before
After Patch
After

Updated the issue summary with steps to reproduce

kristen pol’s picture

Thanks @joshua1234511. This looks good! The issue summary could use a bit more of an update but otherwise this looks RTBC.

kristen pol’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs issue summary update

Updated the issue summary and moving to RTBC based on the above.

quietone’s picture

Version: 9.4.x-dev » 10.0.x-dev
StatusFileSize
new2.35 KB

This is testing against 9.1.x and failing. Let's get this on 10.0.x.

The patch still applies so I am re-uploading and testing it.

duaelfr’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new1.72 KB
new3.83 KB

Patch in #6 and #21 are not complete.
Classes are added to all pager links except next page and last page ones.

Here is a fixed patch.

joshua1234511’s picture

Status: Needs review » Reviewed & tested by the community

Tested the patch from #22
Against v9.4.0 and v10.0.x
- Enable Carlo theme
- Created a view with pager enabled
- Added a template_preprocess_pager hook and added a test class
- Inspect the html code the preprocessed class won't be added.
- Applied the patch
- Cleared cache
- The class is added to the links.

Based of above manual testing marking this asRTBC

larowlan’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests, +Novice

I agree with @acbramley in #4, I think we can add an automated test here.

We already have pager_test_preprocess_pager, and that is adding custom attributes as well as a route for testing a pager.

So it should be a matter of adding 'pager_test' module to ClaroTest and adding a new test method that hits that route and asserts the attribute is output.

See \Drupal\Tests\system\Functional\Pager\PagerTest for reference code that is testing the stark theme.

smustgrave’s picture

Status: Needs work » Needs review
StatusFileSize
new8.04 KB
new4.08 KB

Took a crack at the test cases

smustgrave’s picture

StatusFileSize
new8.04 KB

Empty space

smustgrave’s picture

StatusFileSize
new7.8 KB
new0 bytes
larowlan’s picture

Status: Needs review » Needs work
Issue tags: -Needs tests
  1. +++ b/core/tests/Drupal/FunctionalTests/Theme/ClaroTest.php
    @@ -70,4 +71,76 @@ public function testIsUninstallable() {
    +    $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',
    +    ]));
    +
    +    $this->drupalGet('admin/reports/dblog', ['query' => ['page' => 1]]);
    

    pager_test module provides a route with a pager, so you shouldn't need dblog or to insert error messages to test it

  2. +++ b/core/tests/Drupal/FunctionalTests/Theme/ClaroTest.php
    @@ -70,4 +71,76 @@ public function testIsUninstallable() {
    +    if (isset($first)) {
    +      $this->assertClass($first, 'pager__item--first', 'Element for first page has .pager__item--first class.');
    +      $this->assertClass($first, 'pager__item--action', 'Element for first page has .pager__item--action class.');
    +      $link = $first->find('css', 'a');
    +      $this->assertClass($link, 'pager__link--action-link', 'Link for element first has .pager__link--action-link class');
    +    }
    +    if (isset($previous)) {
    +      $this->assertClass($previous, 'pager__item--previous', 'Element for previous page has .pager__item--previous class.');
    +      $this->assertClass($first, 'pager__item--action', 'Element for previous page has .pager__item--action class.');
    +      $link = $previous->find('css', 'a');
    +      $this->assertClass($link, 'pager__link--action-link', 'Link for element previous has .pager__link--action-link class');
    +    }
    +    if (isset($next)) {
    +      $this->assertClass($next, 'pager__item--next', 'Element for next page has .pager__item--next class.');
    +      $this->assertClass($first, 'pager__item--action', 'Element for next page has .pager__item--action class.');
    +      $link = $next->find('css', 'a');
    +      $this->assertClass($link, 'pager__link--action-link', 'Link for element next has .pager__link--action-link class');
    +    }
    

    We're not testing for what was causing the issue here, that attributes wasn't being used.

    The pager test module adds an attribute to the pager via preprocess, that's all we need to test is output with claro

larowlan’s picture

@smustgrave pointed out on slack that I'm wrong about the above, the test controller etc and pager test all rely on dblog and seeding log entries.

So ignore item 1 above

smustgrave’s picture

Status: Needs work » Needs review
StatusFileSize
new4.83 KB
new5.66 KB
larowlan’s picture

Thanks, can we get a test-only version of the patch too (to show that the new test coverage catches the bug)

smustgrave’s picture

StatusFileSize
new1.83 KB

Also going to rerun the tests on #30 for 10.0.x and 9.5.x

smustgrave’s picture

Looking more closely at it I think the patch in #22 is all we need https://www.drupal.org/files/issues/2022-06-09/3123666-22.patch. Classes were printed out but the templates weren't done in best practices so not sure a test is needed.

larowlan’s picture

StatusFileSize
new2.05 KB
new2.83 KB
new6.65 KB

This should be red/green

The significant element here is a custom class I think

The last submitted patch, 34: 3123666-34-fail.patch, failed testing. View results

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Reviewed and looks good

larowlan’s picture

Status: Reviewed & tested by the community » Needs review

Let's see if we can get an independent RTBC here given we've worked on a lot of the new changes, I'll ask in #bugsmash

acbramley’s picture

Status: Needs review » Reviewed & tested by the community
+++ b/core/tests/Drupal/FunctionalTests/Theme/ClaroTest.php
@@ -70,4 +72,30 @@ public function testIsUninstallable() {
+      $this->assertStringContainsString('lizards', $link->getAttribute('class'));

Nit: Is it better to use hasClass() here?

Otherwise looks great.

larowlan’s picture

Nit: Is it better to use hasClass() here?

I didn't know that existed, so yes definitely better.

larowlan’s picture

Status: Reviewed & tested by the community » Needs work

For #38

arunkumark’s picture

Status: Needs work » Needs review
StatusFileSize
new7.83 KB

As per comment #38, the patch has been re-rolled

Status: Needs review » Needs work

The last submitted patch, 41: 3123666-41-pass.patch, failed testing. View results

arunkumark’s picture

Status: Needs work » Needs review
StatusFileSize
new6.61 KB

The patch has been re-rolled.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Going to mark reviewed since it was a small patch. @arunkumark next time can you please upload an interdiff so we can see the changes. Without it someone has to compare line by line.

quietone’s picture

Issue tags: -Novice

@arunkumark, yes, always add a diff or interdiff. Otherwise you are leaving it up to reviewers or a committer to do that work.

@smustgrave, did you review the patch in #43? I suspect you did but you have only commented on the size of the patch.

smustgrave’s picture

I did. Appears to be similar to #34 but with hasClass added per #38

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 43: 3123666-43.patch, failed testing. View results

smustgrave’s picture

Status: Needs work » Reviewed & tested by the community

seems to be a random unrelated failure

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 43: 3123666-43.patch, failed testing. View results

acbramley’s picture

Version: 10.0.x-dev » 10.1.x-dev
Status: Needs work » Needs review
StatusFileSize
new6.63 KB

Rerolled against 10.1.x

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Moving back to RTBC

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 50: 3123666-50.patch, failed testing. View results

acbramley’s picture

Status: Needs work » Reviewed & tested by the community

Unrelated random failure.

  • lauriii committed 108a6efd on 10.1.x
    Issue #3123666 by smustgrave, larowlan, arunkumark, DuaelFr, acbramley,...
lauriii’s picture

Status: Reviewed & tested by the community » Fixed

Committed 108a6ef and pushed to 10.1.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.