【问题标题】:How can I show results count on search results page in Drupal 8?如何在 Drupal 8 的搜索结果页面上显示结果计数?
【发布时间】:2016-03-29 23:08:44
【问题描述】:

我正在尝试在 Drupal 8 的结果页面中显示搜索结果计数,我想显示如下内容:23 results for search_word

我使用默认的 Drupal 搜索,并使用item-list--search-results.html.twig 作为模板文件来显示结果,但是我在可用变量中找不到搜索结果计数,知道如何找到这个值吗?

【问题讨论】:

    标签: php drupal twig drupal-8


    【解决方案1】:

    Drupal 8 中的结果计数没有变量。

    1) 使用以下代码添加此变量(将此代码添加到 MYTHEME.theme):

    function MYTHEME_preprocess_item_list(&$variables) {
    
        $total = null;
      // get the total number of results from the $GLOBALS
      if(isset($GLOBALS['pager_total_items'])){
        $total = $GLOBALS['pager_total_items'][0];
      }
    
      $variables['count_items'] = $total;
    }
    

    2) 然后你可以在item-list--search-results.html.twig中使用{{ count_items }}

    <div> {{ count_items }} results for search_word </div>
    

    【讨论】:

      【解决方案2】:

      item-list 文件的默认工作方式是循环遍历并调用数组,这样您就可以执行类似的操作 &lt;div&gt; {{ items|length }} results printed &lt;/div&gt;

      Source

      【讨论】:

      • 但是如果我们在页面中有分页器,这将不起作用,因为在这种情况下,items 数组只包含单页项目!
      • 然后我会查看Devel 模块,看看您是否可以找到保存整个搜索结果的数组。在适应新的 Twig 恶作剧时,那个在 Drupal 8 中还没有让我失望。
      猜你喜欢
      • 2016-04-07
      • 1970-01-01
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      • 1970-01-01
      • 2011-08-25
      • 1970-01-01
      • 2018-06-22
      相关资源
      最近更新 更多