diff --git a/Classes/Domain/Model/News.php b/Classes/Domain/Model/News.php index a3e51bdc5f4e550951673003aa4902ccfc6e7b24..fd90345e4a839e2c057ab9aa3225b00a2da235f3 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 60d516693a8dcfe1079eff82a1f0b675b9091eec..25da9fb7a147ccf182ba84dfc550157f596ac1be 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 af0e2c0f7e045f2ea8b3715ebcc834f5dadebea7..6da22e82464b101d0e2f617c1952e00e7b94881a 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 c85638a53a154f330b4268b94250998efa65ba0b..1d41c089b2b11274a1deef16126d3237281b2daf 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 {