简体   繁体   English

Drupal 9 如何使用 entityQuery 添加寻呼机

[英]Drupal 9 How to add pager with entityQuery

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.我们安装了 Drupal 9 并尝试使用分页器模块为文章entityQuery添加分页器,目的是在标签页面中列出已标记的文章,但它不起作用。 It returns null .它返回null

When we dump the data without the pager, using default drupal query, it returns the data of all tagged articles properly.当我们在没有分页器的情况下转储数据时,使用默认的 drupal 查询,它会正确返回所有标记文章的数据。 The code is added in the theme file themeName_preprocess_page hook and being called in page--page.html.twig template file.代码添加在主题文件themeName_preprocess_page hook中,并在page--page.html.twig模板文件中调用。

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.上面的代码在导航中只返回一页,我们有 10 篇文章,所以假设我们设置每页 2 篇文章,output 应该是 5 页而不是 1 页。

Also articles_data attribute returns null.还有articles_data属性返回null。 Could you please help me to find what's wrong with the code?你能帮我找出代码有什么问题吗? Happy to share more information as needed, thank you.很高兴根据需要分享更多信息,谢谢。

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.只是在此处阅读此模块的文档,您似乎在寻呼机的渲染数组中至少缺少#theme#style键。

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
                ],
              ]; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM