Enabling standard pagers in your custom views backend

Last updated on
1 December 2016

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

If you write a custom backend for Views 3 (eg you have an external data source that you wish to expose through a view) you'll need to ensure that you carry out the following steps in order that views' full- and mini-pagers work.

1. Add the following to the build() method of your query object:

  /*
  this creates a new pager object within the view
  */
  $view->init_pager();

  /*
  this takes the settings from the pager object (eg items per page - this could be innate or set via an exposed
  filter and makes them available to the view
  */
  $this->pager->query();

2. add the following to the execute() method of your query object:

/*
set the total number of items that exist in this query
eg select count(*) from foo;
*/
$this->pager->total_items = $total_items_in_view;
// Also tell the view the total, for the "Global: Result summary" output.
$view->total_rows = $this->pager->total_items;
/*
this does the final calculations to work out the current offset and
number of pages based on total items, exposed views etc
*/
$this->pager->update_page_info();

/*
$this->offset and $this->limit now contain the 0th indexed first
row to display and the number of rows to return so we can eg do
select foo from bar limit {$this->offset} {$this->limit};
*/

Help improve this page

Page status: No known problems

You can: