1

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

  • Category_one (38)
  • 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

{namespace s=HIT\huskytheme\ViewHelpers} 
{s:news.countCategories(category: 2)}
Oleg V Karun
  • 726
  • 1
  • 6
  • 29

3 Answers3

3

Actualy i found way to get number of news in Category. Thx Georg Ringer and undko

My 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

<!-- 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>
...
Oleg V Karun
  • 726
  • 1
  • 6
  • 29
1

There is no method countByCategories which implements something like a demand object. Please just use a direct call to the DB.

Georg Ringer
  • 7,779
  • 1
  • 16
  • 34
  • It‘s important to mention that Oleg‘s code doesn‘t break (but return 0) because it‘s a valid magic method call, albeit useless. – undko May 23 '17 at 20:50
  • About my question: why function "findDemanded" have "$query->getQuerySettings()->setRespectStoragePage(false);" but "countDemanded" haven't and get StoragePid as "0". So i can get list my "findDemanded" corectly but can't do the same directly by "countDemanded" – Oleg V Karun May 24 '17 at 09:14
1

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. Just connect a slot of yours to signal GeorgRinger\News\Controller\CategoryController : listAction. You‘ll get all categories there (in argument categories) and can launch a

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.

undko
  • 927
  • 8
  • 15