Skip to content
Snippets Groups Projects
Verified Commit 2ac0611e authored by Kevin Ditscheid's avatar Kevin Ditscheid
Browse files

[TASK] Add related news by tag or category

parent 3b6dc410
No related branches found
No related tags found
1 merge request!41[TASK] Add related news by tag or category
......@@ -26,8 +26,12 @@ namespace SGalinski\SgNews\Domain\Model;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use SGalinski\SgNews\Domain\Repository\NewsRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy;
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
/**
* News
......@@ -97,14 +101,28 @@ class News extends CategoryAndNews {
*/
protected $contentFromAnotherPage = 0;
/**
* @var ConfigurationManager
*/
protected $configurationManager;
/**
* Inject the configuration manager
*
* @param ConfigurationManager $configurationManager
*/
public function injectConfigurationManager(ConfigurationManager $configurationManager) {
$this->configurationManager = $configurationManager;
}
/**
* Constructor
*/
public function __construct() {
parent::__construct();
$this->relatedNews = new ObjectStorage();
$this->tags = new ObjectStorage();
$this->newsAuthor = new ObjectStorage();
$this->relatedNews = new ObjectStorage();
}
/**
......@@ -182,6 +200,12 @@ class News extends CategoryAndNews {
$this->relatedNews->_loadRealInstance();
}
$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);
}
return $this->relatedNews;
}
......
......@@ -505,4 +505,29 @@ class NewsRepository extends AbstractRepository {
$query->matching($query->equals('uid', $uid));
return $query->execute()->getFirst();
}
/**
* This method finds news related by Tag or Category to the given news record
*
* @param News $news The news to find related news to
* @return QueryResultInterface
*/
public function findRelated(News $news) {
$query = $this->createQuery();
$query->getQuerySettings()->setRespectStoragePage(FALSE);
$contains = [];
$tags = $news->getTags();
if ($tags->count() > 0) {
foreach ($tags as $tag) {
$contains[] = $query->contains('tags', $tag);
}
} else {
$contains[] = $query->equals('pid', $news->getPid());
}
$query->matching(
$contains
);
return $query->execute();
}
}
......@@ -19,5 +19,8 @@ plugin.tx_sgnews {
# sort direction (DESC / ASC)
sortDirection = DESC
# Enable fetching of related news by tag or category, if related news are not set manually
automaticRelatedNews = 1
}
}
......@@ -109,6 +109,9 @@ plugin.tx_sgnews {
# The logo value for the structured data implementation (see single view template)
publisherLogo =
# Enable fetching of related news by tag or category, if related news are not set manually
automaticRelatedNews = {$plugin.tx_sgnews.settings.automaticRelatedNews}
}
features {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment