3 namespace Drupal\pager_test\Controller;
5 use Drupal\Core\Controller\ControllerBase;
8 * Controller routine for testing the pager.
10 class PagerTestController extends ControllerBase {
13 * Builds a render array for a pageable test table.
16 * The pager element to be used for paging.
18 * The limit of rows per page for the specified element.
23 protected function buildTestTable($element, $limit) {
27 ['data' => 'timestamp'],
29 $query = db_select('watchdog', 'd')->extend('Drupal\Core\Database\Query\PagerSelectExtender')->element($element);
31 ->fields('d', ['wid', 'type', 'timestamp'])
36 foreach ($result as $row) {
37 $rows[] = ['data' => (array) $row];
43 '#empty' => $this->t("There are no watchdog records found in the db"),
48 * Returns a pager with 'parameters' variable.
50 * The 'pager_calls' parameter counts the calls to the pager, subsequent
51 * to the initial call.
53 public function queryParameters() {
56 $build['pager_table_0'] = $this->buildTestTable(0, 5);
58 // Counter of calls to the current pager.
59 $query_params = pager_get_query_parameters();
60 $pager_calls = isset($query_params['pager_calls']) ? ($query_params['pager_calls'] ? $query_params['pager_calls'] : 0) : 0;
61 $build['l_pager_pager_0'] = ['#markup' => $this->t('Pager calls: @pager_calls', ['@pager_calls' => $pager_calls])];
64 $build['pager_pager_0'] = [
68 'pager_calls' => ++$pager_calls,
71 'Drupal\pager_test\Controller\PagerTestController::showPagerCacheContext',
79 * Returns a page with multiple pagers.
81 public function multiplePagers() {
83 // Build three tables with same query and different pagers.
84 $build['pager_table_0'] = $this->buildTestTable(0, 20);
85 $build['pager_pager_0'] = [
86 '#type' => 'container',
87 '#attributes' => ['class' => ['test-pager-0']],
94 $build['pager_table_1'] = $this->buildTestTable(1, 20);
95 $build['pager_pager_1'] = [
96 '#type' => 'container',
97 '#attributes' => ['class' => ['test-pager-1']],
104 $build['pager_table_4'] = $this->buildTestTable(4, 20);
105 $build['pager_pager_4'] = [
106 '#type' => 'container',
107 '#attributes' => ['class' => ['test-pager-4']],
118 * #pre_render callback for #type => pager that shows the pager cache context.
120 public static function showPagerCacheContext(array $pager) {
121 drupal_set_message(\Drupal::service('cache_contexts_manager')->convertTokensToKeys(['url.query_args.pagers:' . $pager['#element']])->getKeys()[0]);