[英]TYPO3 & tx_news need ViewHelper for show count of Entities in category
任務:在類別菜單中顯示每個類別中項目的計數,如
我嘗試按需求計算,但沒有工作
<?php
namespace HIT\huskytheme\ViewHelpers\News;
class CountCategoriesViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
/**
* @var \GeorgRinger\News\Domain\Repository\NewsRepository
* @inject
*/
protected $newsRepository = null;
/**
*
* @param string $category
* @return string
*/
public function render($category) {
$demand = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('GeorgRinger\\News\\Domain\\Model\\Dto\\NewsDemand');
//$demand->setDateField('datetime');
$demand->setStoragePage(10, true);
// for example by id = 2
$demand->setCategories(2);
$demand->setCategoryConjunction('and');
$demand->setIncludeSubCategories('1');
//$demand->setArchiveRestriction($settings['archiveRestriction']);
$statistics = $this->newsRepository->countByCategories($demand);
\TYPO3\CMS\Core\Utility\DebugUtility::debug($statistics);
return $this->newsRepository->countByCategories($demand);
}
}
如果打電話,只需0
{namespace s=HIT\huskytheme\ViewHelpers}
{s:news.countCategories(category: 2)}
沒有方法countByCategories
實現像需求對象之類的東西。 請直接使用DB。
根據類別的數量以及在非緩存頁面上顯示此菜單的需要,我建議不要直接在控制器中查看助手方式,而是查詢DB(如Georg建議的那樣)。 只需連接你的插槽就可以發信號GeorgRinger\\News\\Controller\\CategoryController
: listAction
。 您將獲得所有類別(在參數categories
)並可以啟動
SELECT COUNT(*) FROM sys_category_record_mm … GROUP BY uid_local
一次性獲取所有計數。 然后只需將新密鑰添加到您返回的數組中,並在該名稱下的模板中使用它。
Actualy我找到了獲取類別新聞數量的方法。 格奧爾格林 格和undko
我的ViewHelper
<?php
namespace HIT\huskytheme\ViewHelpers\News;
class CountCategoriesViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
/**
* @var \GeorgRinger\News\Domain\Repository\NewsRepository
* @inject
*/
protected $newsRepository = null;
/**
*
* @param \GeorgRinger\News\Domain\Model\Category $category
* @return string
*/
public function render($category) {
/* @var $demand \GeorgRinger\News\Domain\Model\Dto\NewsDemand */
$demand = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\GeorgRinger\News\Domain\Model\Dto\NewsDemand::class);
$demand->setCategories(array($category));
$demand->setCategoryConjunction('and');
$demand->setIncludeSubCategories(false);
return count($this->newsRepository->findDemanded($demand));
}
}
在我的tx_news模板/類別/ List.html中
<!-- load my ViewHelper -->
{namespace s=HIT\huskytheme\ViewHelpers}
這里加上計數
...
<f:link.page title="{category.item.title}" class="current-item" pageUid="{settings.listPid}"
additionalParams="{tx_news_pi1:{overwriteDemand:{categories: category.item.uid}}}">{category.item.title}
<span class="postnum">({s:news.countCategories(category: category.item)})</span>
</f:link.page>
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.