TYPO3 & tx_news 需要 ViewHelper 来显示类别中实体的数量
TYPO3 & tx_news need ViewHelper for show count of Entities in category
任务:在类别菜单中显示每个类别中的项目数,例如
- Category_one (38)
- Category_two (14)
- 等等...
我按 $demand 进行了尝试计数,但没有成功
<?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
可以实现需求对象之类的东西。请直接调用数据库。
根据类别的数量和在未缓存页面上显示此菜单的需要,我建议不要使用视图助手方式,而是直接在控制器中查询数据库(如 Georg 所建议的)。只需连接您的插槽即可发出信号 GeorgRinger\News\Controller\CategoryController
: listAction
。您将在那里获得所有类别(在参数 categories
中)并且可以启动
SELECT COUNT(*) FROM sys_category_record_mm … GROUP BY uid_local
一次获取所有计数。然后只需向数组中添加一个新键 return 并以该名称在模板中使用它。
实际上我找到了获取类别中新闻数量的方法。谢谢Georg Ringer and undko
我的视图助手
<?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 Templates/Category/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>
...
任务:在类别菜单中显示每个类别中的项目数,例如
- Category_one (38)
- Category_two (14)
- 等等...
我按 $demand 进行了尝试计数,但没有成功
<?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
可以实现需求对象之类的东西。请直接调用数据库。
根据类别的数量和在未缓存页面上显示此菜单的需要,我建议不要使用视图助手方式,而是直接在控制器中查询数据库(如 Georg 所建议的)。只需连接您的插槽即可发出信号 GeorgRinger\News\Controller\CategoryController
: listAction
。您将在那里获得所有类别(在参数 categories
中)并且可以启动
SELECT COUNT(*) FROM sys_category_record_mm … GROUP BY uid_local
一次获取所有计数。然后只需向数组中添加一个新键 return 并以该名称在模板中使用它。
实际上我找到了获取类别中新闻数量的方法。谢谢Georg Ringer and undko
我的视图助手
<?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 Templates/Category/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>
...