如何在Drupal 8中的搜索结果页面上显示结果计数?

Agh*_*iad 3 php drupal twig drupal-8

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

我正在使用默认的Drupal搜索,并item-list--search-results.html.twig作为模板文件显示结果,但我找不到可用变量中的搜索结果计数,任何想法怎么能找到这个值?

Nik*_*tov 5

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;
}
Run Code Online (Sandbox Code Playgroud)

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

<div> {{ count_items }} results for search_word </div>
Run Code Online (Sandbox Code Playgroud)