2

We've a Drupal 9 installation and are trying to add a pager using the pagerer module for articles entityQuery, the aim is to list tagged articles in a tag page, but it’s not working. It returns null.

When we dump the data without the pager, using default drupal query, it returns the data of all tagged articles properly. The code is added in the theme file themeName_preprocess_page hook and being called in page--page.html.twig template file.

Here’s our code:

$query = \Drupal::entityQuery('node')
            ->condition('status', 1)
            ->condition('type', 'article');
            ->pager(2);
        $nids = $query->sort('created', 'DESC')
                ->execute();
        if($nids):
            $nodesNews = \Drupal\node\Entity\Node::loadMultiple($nids);
            $pathNews = base_path();
            $pager =  [
                'articles_data' => $nodesNews,
                'results' => [
                  '#theme' => 'news_pagination',
                  '#items' => $nodesNews,
                  '#path'  => $pathNews,
                  '#tag' => $tag
                ],
                'pager' => [
                  '#type' => 'pager',
                  '#quantity' => 5
                ],
              ]; 
            
            return $pager;
        endif; 

And here is the code that calls the query:

<div>
{{ articles_data }}
    {{ pager }}
</div>

The above code returns only one page in the navigation and we’ve 10 articles, so given that we set 2 articles per page, the output should be 5 pages instead of 1.

Also articles_data attribute returns null. Could you please help me to find what’s wrong with the code? Happy to share more information as needed, thank you.

  • I am not familiar with this module, but as far as i can see, the code seems ok. I guess you save `pager` in a variable that is used in the twig template – Gurgolo Aug 09 '22 at 06:52

1 Answers1

0

Just reading the docs for this module here, it would seem that you are missing at least the #theme and #style keys in your render array for the pager.

A more likely to succeed version of the above render array would be


              $pager =  [
                'articles_data' => $nodesNews,
                'results' => [
                  '#theme' => 'news_pagination',
                  '#items' => $nodesNews,
                  '#path'  => $pathNews,
                  '#tag' => $tag
                ],
                'pager' => [
                  '#type' => 'pager',
                  '#theme' => 'pagerer_base',
                  '#style' => 'standard',
                  '#config' => [
                    'display_restriction' => 0,
                  ],
                  '#quantity' => 5
                ],
              ]; 
Nick Andriopoulos
  • 10,313
  • 6
  • 32
  • 56