I've created a custom module to get X items using entityQuery, and then display them as a list. I need to limit these and add a pager to click through to the next page, but I cannot find how best to do this. Examples I've found either use external search sources, or are depreciated functions of older versions of Drupal.

I've added the pager option in the query, initialized the pager, and added the #type = pager to the content block, but it's not showing up. I know I am missing something but I cant figure out what it is.

class MyPageController extends ControllerBase {

public function myPage1() {

    $query = \Drupal::entityQuery('node')
    ->condition('status', 1)
    ->condition('type', 'page')
    ->range(0, 10)
    ->sort('created', 'DESC');

    $nids = $query->execute();
    $nodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($nids);

    pager_default_initialize($query->total, '7');

    $list = array();

    foreach ($nodes AS $node){
        $list[] = $node->get('title')->value;

    $content = [
        '#theme' => 'item_list',
        '#list_type' => 'ul',
        '#title' => 'Page list',
        '#items' => $list,
        '#attributes' => ['class' => 'my_page1_list'],
        '#wrapper_attributes' => ['class' => 'container'],
        '#type' => ['pager'],

    return $content;

1 Answer 1


The format of the render array is wrong, you have to place the pager in its own render element (see this question, which was almost at the same time Pager with EntityQuery) or if you want to have it in the theme in a variable, but then a name other than #type.

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.