[英]How can I show results count on search results page in Drupal 8?
我正在嘗試在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中使用{{count_items}} - search-results.html.twig :
<div> {{ count_items }} results for search_word </div>
item-list文件工作的默認方式是遍歷和調用名為items的數組,這樣你就可以執行類似<div> {{ items|length }} results printed </div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.