0

Is there a way to start the page count from 1 instead of 0 using views pager? Maybe using aliases or some module?

By default, the first page is displayed with the URL parameter ?page=0, the second page with ?page=1 and so on. The pagination works good, but in my opinion it would be more coherent having the page one for the url with the parameter ?page=1, the page 2 with ?page=2, etc.

I found a similar question here, but it's about Drupal 7 and I'm on Drupal 8: Views pager - start paging from 1

Another discussion here: https://www.drupal.org/node/1818040, but without a solution.

1 Answer 1

1

To change it just on the UI, you can adapt the number in template_preprocess_pager in your theme's .theme file.

Something like:

function template_preprocess_pager(&$variables) {
  foreach ($variables['items']['pages'] as $key => $value) {
    $variables['items']['pages'][$key]['text'] = $key +1;
  }
}

To change the query params, you could use hook_url_inbound_alter and hook_url_outbound_alter to manipulate the queryparams, but this was D7. In D8 you would need to go in this direction to manipulate them: How do I implement the equivalent of hook_url_inbound_alter()?

4
  • Thank you. In the html (returned by the pager template) I already have right page numbers, starting from 1. It is "wrong" in the URL: if I click on the link for the page 1 the URL parameter is page=0. Did the preprocess function fix the URL parameter also?
    – Andrea
    Commented Apr 28, 2016 at 22:33
  • I edited the question to better explain my problem.
    – Andrea
    Commented Apr 28, 2016 at 22:42
  • 1
    NO - the url will stay the same, this is only UI (display) correction ;) I made an update about manipulating the queryparam.
    – rémy
    Commented Apr 29, 2016 at 7:11
  • Thanks. I will make a try manipulating query parameters.
    – Andrea
    Commented Apr 29, 2016 at 7:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.