本文介绍了TYPO3 &tx_news 需要 ViewHelper 来显示类别中实体的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任务:在类别菜单中显示每个类别中的项目数,如

Task: in category menu show count of item in each category, like

  • 类别一 (38)
  • 类别二 (14)
  • 等等……

我已尝试按 $demand 计数,但没有成功

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

        }

    }

但是如果调用只得到 0

But get just 0, if call

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

推荐答案

实际上我找到了获取类别新闻数量的方法.Thx Georg Ringer取消

Actualy i found way to get number of news in Category. Thx 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 中

And in my 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>
...

这篇关于TYPO3 &amp;tx_news 需要 ViewHelper 来显示类别中实体的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:22