Search code examples
typo3typo3-7.6.xtx-news

TYPO3 tx_news & news_comments display count of comments in news list


I looking for normal (fluid) way add in tx_news (Template->News->List) count of comments from table EXT: news_comments for each entity. For Example:

News 1 (Comments:5)  
Date  
Description  
-----
News 2 (Commnets: 0)  
Date  
Description  

Solution

  • Ok i create simple ViewHelper for this. I guess not allowed if you have few thousand posts but if few hundred - all will be ok. First we nned add ViewHelper in our project EXT

    <?php
    
    namespace HIT\huskytheme\ViewHelpers\News;
    
    class CountCommnetsViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
    
        /**
         * @var \DRCSystems\NewsComment\Domain\Repository\CommentRepository
         * @inject
         */
        protected $commentRepository = null;
    
        /**
         * 
         * @param int $news
         * @return string
         */
        public function render($news) {
            return count($this->commentRepository->getCommentsByNews($news));
        }
    
    }
    

    Please change 'namespace' for your Vendor name and ViewHelper location.

    Then in copy tx_news fluid Templates somewhere and add them by TS. In Templates->Partitials->List->Item.html (or you path) you can call ViewHelper and use like that:

    {namespace s=HIT\huskytheme\ViewHelpers} 
    ...
    Comments: ({s:news.countCommnets(news: newsItem.uid)})
    

    Or other template - just add corect newsItem.uid inside and againe you own namesapce mast be called.