1

When we create a View of pages we can give it a pager. That pager typically appears underneath the view. This can cause an accessibility problem of a user mistakenly clicking to go to a next page without understanding what the current page is, especially if the page is very long and the the fold of the new page doesn't indicate what is the current page on the pager series.

The pager is rendered as in the following screenshots.

screenshot

screenshot

How can I show the user the current page above the view as well?

9
  • Welcome to Drupal Answers! The pager shown in a view already shows the current page, even when the mini pager is used. For example, with Drupal 10 and its default theme, it appears like this. Probably, the used theme is not showing correctly the pager.
    – apaderno
    Nov 12 at 17:00
  • @apaderno my theme is olivero and the pager is shown fine underneath the main content but I want to somehow show the current page above the main content as well, perhaps even duplicating the pager to the top part of the page. I can duplicate it with JavaScript in a cloneNode(true); but I doubt it's a good way to go.
    – webtech
    Nov 13 at 3:38
  • Go for JavaScript. Because whatever top of the page means and depending of how the view is built, it can become quite difficult to retrieve the pager states outside of the context of the view.
    – leymannx
    Nov 13 at 7:32
  • My comment was for This can cause an accessibility problem of a user mistakenly clicking to go to a next page without understanding what the current page is. The current page is shown near the link to go to the next page. If users do not notice that, they would not notice that if the page number is placed on the top of the page. I could understand if the problem is that users do not understand what those numbers are, but in that case it should be sufficient to change how the pager is rendered to add more explicit labels, for example Current page: near the current page number.
    – apaderno
    Nov 13 at 8:25
  • @leymannx I guess the main problem is not where the pager is shown, but for the users to understand that the numbers shown on the pager are page numbers, and the highlighted one is the current page.
    – apaderno
    Nov 13 at 8:27

2 Answers 2

1

You can use template_preprocess_views_view() hook to get current page number

function Mymodule_preprocess_views_view(&$vars){
  $view = $vars['view'];
  $id = $view->storage->id();
  if($id == 'view_name'){
   $pager_number = $view->pager->current_page;
   // current page key value start from 0 so add by 1
   // set current_page variable that can access in view twig
   $vars['current_page'] = $pager_number + 1;
  }
}

Note: Now you can use the "current_page" in views-view.html.twig by rendering like this: {{ current_page }} Make sure override view twig by view name displayed by theme suggestion

To override view twig can follow naming convention like this: views-view--machine_name.html.twig

0

In the View add a Header: Global: Text area and fill in the text:

[view:current-page]

There is a list with more pager variables in the section Available global token replacements.

Your Answer

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

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