Skip to main content

Drupal 7 notes - getting a Views block with a pager to remember what page it's on

Posted on: Friday, 27 June 2014 @ 9:20pm
Blatting about
More specifically

While building this gallery for this client, I ran into an issue where if you clicked on the next page in the navigation block and selected a project on the second page, when the new project loaded the block would reload back on page 1.  The Designer and I thought that pagers really needed to remember what page they were on but as of four years ago it was a feature Views and the core pager didn't support, so at the bottom of an issue someone else opened up four years ago asking for this to happen some kind soul provided this code:

function lastviewedpage_views_pre_view(&$view) { 
  $view_name = $view->name . '_' . $view->current_display; 
  $page=(isset($_SESSION['views_pages'][$view_name]))?$_SESSION['views_pages'][$view_name]:null; 
  
  if(!empty($_REQUEST)) $page=(isset($_POST['page']))?end(explode(',',$_POST['page'])):null;
  $_SESSION['views_pages'][$view_name] = $page; $view->set_current_page($page); 
}

 

Hope it's in core in 8.

No comments yet

Add new comment

The content of this field is kept private and will not be shown publicly.