1 pager_example.module pager_example_page()

Build the pager query.

Uses the menu_links table since it is installed with several rows in it and we don't have to create fake data in order to show this example.

Return value

array: A render array completely set up with a pager.

Related topics

File

modules/examples/pager_example/pager_example.module, line 44
This is an example describing how a module can implement a pager in order to reduce the number of output rows to the screen and allow a user to scroll through multiple screens of output.

Code

function pager_example_page() {
  // We are going to output the results in a table with a nice header.
  $header = array(
    array('data' => t('MLID')),
    array('data' => t('Link title')),
    array('data' => t('Menu name')),
  );

  // We are extending the PagerDefault class here.
  // It has a default of 10 rows per page.
  // The extend('PagerDefault') part here does all the magic.
  $query = db_select('menu_links', 'm')->extend('PagerDefault');
  $query->fields('m', array('mlid', 'link_title', 'menu_name'));

  // Change the number of rows with the limit() call. Skip rows with no link
  // title to prevent empty table cells.
  $result = $query
  ->condition('m.link_title', '', '<>')
    ->limit(10)
    ->orderBy('m.mlid')
    ->execute();

  $rows = array();
  foreach ($result as $row) {
    // Normally we would add some nice formatting to our rows
    // but for our purpose we are simply going to add our row
    // to the array.
    $rows[] = array('data' => (array) $row);
  }

  // Create a render array ($build) which will be themed as a table with a
  // pager.
  $build['pager_table'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => t('There are no date formats found in the db'),
  );

  // Attach the pager theme.
  $build['pager_pager'] = array('#theme' => 'pager');

  return $build;
}