简体   繁体   English

TYPO3和tx_news需要ViewHelper来显示类别中实体的显示数量

[英]TYPO3 & tx_news need ViewHelper for show count of Entities in category

Task: in category menu show count of item in each category, like 任务:在类别菜单中显示每个类别中项目的计数,如

  • Category_one (38) Category_one(38)
  • Category_two (14) Category_two(14)
  • Etc ... 等......

I have try count by $demand but did'nt work 我尝试按需求计算,但没有工作

<?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); 

        }

    }

But get just 0, if call 如果打电话,只需0

{namespace s=HIT\huskytheme\ViewHelpers} 
{s:news.countCategories(category: 2)}

There is no method countByCategories which implements something like a demand object. 没有方法countByCategories实现像需求对象之类的东西。 Please just use a direct call to the DB. 请直接使用DB。

Depending on the number of categories and the need to show this menu on uncached pages I'd suggest to not go the view helper way but query DB (as Georg suggested) directly in the controller. 根据类别的数量以及在非缓存页面上显示此菜单的需要,我建议不要直接在控制器中查看助手方式,而是查询DB(如Georg建议的那样)。 Just connect a slot of yours to signal GeorgRinger\\News\\Controller\\CategoryController : listAction . 只需连接你的插槽就可以发信号GeorgRinger\\News\\Controller\\CategoryControllerlistAction You'll get all categories there (in argument categories ) and can launch a 您将获得所有类别(在参数categories )并可以启动

SELECT COUNT(*) FROM sys_category_record_mm … GROUP BY uid_local

to fetch all counts in one go. 一次性获取所有计数。 Then simply add a new key to the array you return and use it in your template under that name. 然后只需将新密钥添加到您返回的数组中,并在该名称下的模板中使用它。

Actualy i found way to get number of news in Category. Actualy我找到了获取类别新闻数量的方法。 Thx Georg Ringer and undko 格奥尔格林 undko

My ViewHelper 我的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));
    }
}

And in my tx_news Templates/Category/List.html 在我的tx_news模板/类别/ List.html中

<!-- load my ViewHelper -->
{namespace s=HIT\huskytheme\ViewHelpers} 

and here add count 这里加上计数

...
                            <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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM