From 4cb2a099e99166bfa106fd134c4bc017d7d6039c Mon Sep 17 00:00:00 2001 From: Kevin Ditscheid <kevin.ditscheid@sgalinski.de> Date: Tue, 15 Mar 2022 10:00:36 +0100 Subject: [PATCH] [TASK] Add a limit and ordering to automatic related news --- Classes/Domain/Model/News.php | 2 +- Classes/Domain/Repository/NewsRepository.php | 10 +++++++++- Configuration/TypoScript/Frontend/constants.typoscript | 3 +++ Configuration/TypoScript/Frontend/setup.typoscript | 3 +++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Classes/Domain/Model/News.php b/Classes/Domain/Model/News.php index a3e51bd..fd90345 100644 --- a/Classes/Domain/Model/News.php +++ b/Classes/Domain/Model/News.php @@ -203,7 +203,7 @@ class News extends CategoryAndNews { $settings = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS); if ($settings['automaticRelatedNews'] && !$this->_isNew() && $this->relatedNews->count() === 0) { $newsRepository = GeneralUtility::makeInstance(NewsRepository::class); - $this->relatedNews = $newsRepository->findRelated($this); + $this->relatedNews = $newsRepository->findRelated($this, $settings['limitRelatedNews']); } return $this->relatedNews; diff --git a/Classes/Domain/Repository/NewsRepository.php b/Classes/Domain/Repository/NewsRepository.php index 60d5166..25da9fb 100644 --- a/Classes/Domain/Repository/NewsRepository.php +++ b/Classes/Domain/Repository/NewsRepository.php @@ -510,11 +510,15 @@ class NewsRepository extends AbstractRepository { * This method finds news related by Tag or Category to the given news record * * @param News $news The news to find related news to + * @param int $limit Limit the amount of related news * @return QueryResultInterface */ - public function findRelated(News $news) { + public function findRelated(News $news, int $limit = 0) { $query = $this->createQuery(); $query->getQuerySettings()->setRespectStoragePage(FALSE); + $query->setOrderings([ + 'crdate' => QueryInterface::ORDER_DESCENDING + ]); $contains = []; $tags = $news->getTags(); if ($tags->count() > 0) { @@ -528,6 +532,10 @@ class NewsRepository extends AbstractRepository { $query->matching( $contains ); + if ($limit > 0) { + $query->setLimit($limit); + } + return $query->execute(); } } diff --git a/Configuration/TypoScript/Frontend/constants.typoscript b/Configuration/TypoScript/Frontend/constants.typoscript index af0e2c0..6da22e8 100644 --- a/Configuration/TypoScript/Frontend/constants.typoscript +++ b/Configuration/TypoScript/Frontend/constants.typoscript @@ -22,5 +22,8 @@ plugin.tx_sgnews { # Enable fetching of related news by tag or category, if related news are not set manually automaticRelatedNews = 1 + + # Set a limit for fetched related news (for no limit, set to 0) + limitRelatedNews = 5 } } diff --git a/Configuration/TypoScript/Frontend/setup.typoscript b/Configuration/TypoScript/Frontend/setup.typoscript index c85638a..1d41c08 100644 --- a/Configuration/TypoScript/Frontend/setup.typoscript +++ b/Configuration/TypoScript/Frontend/setup.typoscript @@ -112,6 +112,9 @@ plugin.tx_sgnews { # Enable fetching of related news by tag or category, if related news are not set manually automaticRelatedNews = {$plugin.tx_sgnews.settings.automaticRelatedNews} + + # Set a limit for fetched related news (for no limit, set to 0) + limitRelatedNews = {$plugin.tx_sgnews.settings.limitRelatedNews} } features { -- GitLab