【发布时间】:2016-03-29 23:08:44
【问题描述】:
我正在尝试在 Drupal 8 的结果页面中显示搜索结果计数,我想显示如下内容:23 results for search_word。
我使用默认的 Drupal 搜索,并使用item-list--search-results.html.twig 作为模板文件来显示结果,但是我在可用变量中找不到搜索结果计数,知道如何找到这个值吗?
【问题讨论】:
我正在尝试在 Drupal 8 的结果页面中显示搜索结果计数,我想显示如下内容:23 results for search_word。
我使用默认的 Drupal 搜索,并使用item-list--search-results.html.twig 作为模板文件来显示结果,但是我在可用变量中找不到搜索结果计数,知道如何找到这个值吗?
【问题讨论】:
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>
【讨论】:
item-list 文件的默认工作方式是循环遍历并调用数组,这样您就可以执行类似的操作
<div> {{ items|length }} results printed </div>
【讨论】: