pager_example.module 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * @file
  4. * This is an example describing how a module can implement a pager in order
  5. * to reduce the number of output rows to the screen and allow a user
  6. * to scroll through multiple screens of output.
  7. *
  8. * See:
  9. * @link http://drupal.org/developing/api/database Database API @endlink
  10. * @link http://drupal.org/node/508796 Extenders @endlink
  11. */
  12. /**
  13. * @defgroup pager_example Example: Pager
  14. * @ingroup examples
  15. * @{
  16. * Example of a results pager.
  17. */
  18. /**
  19. * Implements hook_help().
  20. */
  21. function pager_example_help($path, $arg) {
  22. switch ($path) {
  23. case 'examples/pager_example':
  24. return '<p>' . t('The layout here is a themed as a table with a default limit of 10 rows per page. The limit can be changed in the code by changing the limit to some other value. This can be extended to add a filter form as well so the user can choose how many they would like to see on each screen.') . '</p>';
  25. }
  26. }
  27. /**
  28. * Implements hook_menu().
  29. */
  30. function pager_example_menu() {
  31. $items['examples/pager_example'] = array(
  32. 'title' => 'Pager example',
  33. 'description' => 'Show a page with a long list across multiple pages',
  34. 'page callback' => 'pager_example_page',
  35. 'access callback' => TRUE,
  36. );
  37. return $items;
  38. }
  39. /**
  40. * Build the pager query.
  41. *
  42. * Uses the date_formats table since it is installed with ~35 rows
  43. * in it and we don't have to create fake data in order to show
  44. * this example.
  45. *
  46. * @return array
  47. * A render array completely set up with a pager.
  48. */
  49. function pager_example_page() {
  50. // We are going to output the results in a table with a nice header.
  51. $header = array(
  52. array('data' => t('DFID')),
  53. array('data' => t('Format')),
  54. array('data' => t('Type')),
  55. );
  56. // We are extending the PagerDefault class here.
  57. // It has a default of 10 rows per page.
  58. // The extend('PagerDefault') part here does all the magic.
  59. $query = db_select('date_formats', 'd')->extend('PagerDefault');
  60. $query->fields('d', array('dfid', 'format', 'type'));
  61. // Change the number of rows with the limit() call.
  62. $result = $query
  63. ->limit(10)
  64. ->orderBy('d.dfid')
  65. ->execute();
  66. $rows = array();
  67. foreach ($result as $row) {
  68. // Normally we would add some nice formatting to our rows
  69. // but for our purpose we are simply going to add our row
  70. // to the array.
  71. $rows[] = array('data' => (array) $row);
  72. }
  73. // Create a render array ($build) which will be themed as a table with a
  74. // pager.
  75. $build['pager_table'] = array(
  76. '#theme' => 'table',
  77. '#header' => $header,
  78. '#rows' => $rows,
  79. '#empty' => t('There are no date formats found in the db'),
  80. );
  81. // Attach the pager theme.
  82. $build['pager_pager'] = array('#theme' => 'pager');
  83. return $build;
  84. }
  85. /**
  86. * @} End of "defgroup pager_example".
  87. */