我正在尝试在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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句