I am trying to implement the pagination in drupal 8 custom page.

I have a custom array which I am rendering using theme, I need to add pagination for it.

    $internal_result = array_merge($internal_result, $basic_data);
    $response = new AjaxResponse();
    $terms['internal'] = $internal_result;
    $renderable = array(
        '#theme' => 'search_result_template',
        '#data' => $terms
    $rendered = \Drupal::service('renderer')->render($renderable);

I need to add pagination for above result. Can anyone help me?


I tried with the search_result template and it worked, hope this helps you :

$output['prefix']['#markup'] = '<h2>Test page</h2>';
//This sets up necessary global variables for the type => #pager
$page = pager_default_initialize(5, 2);
$render = [];
  $output[] = [
    '#theme' => 'search_result',
    '#result' => 's',
$pager = [
  '#type' => 'pager',
$output['suffix']['#markup'] = '</ol>' . \Drupal::service('renderer')->render($pager);
return $output;

We just need to set up the global variables which help to process rendered array to output pagers items. And add a pager to the suffix.

  • Unused variable $page. Global means it will affect all existing views as well? – leymannx Dec 5 '18 at 6:34
  • I believe the pager_default_initialize method is set per page by taking two parameters ie total as well as the limit as per the current page requirement. Please let me know if my understanding is not correct – Shreya Shetty Dec 5 '18 at 8:30
  • I have achieved getting pager, but page is not working. $chunks = array_chunk($internal_result, $perPage, TRUE); // Split all records in to chunked. $terms['internal'] = $chunks[0]; $renderable = []; $renderable[] = array( '#theme' => 'search_result_template', '#data' => $terms, ); $renderable[] = ['#type' => 'pager']; $rendered = \Drupal::service('renderer')->render($renderable); – sneha.kamble Dec 5 '18 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.