这是完整的解决方案:
- 编写DataProcessor获取相关新闻记录
- 添加 NewsDataProcessor
- 将 n:excludeDisplayedNews 添加到流体模板
第 1 步:在您的 Page Typoscript 中,添加一个 DataProcessor 以获取相关的新闻记录。将此添加到页面 FLUIDTEMPLATE Typoscript 的 dataProcession 块中:
10 = TYPO3\CMS\Frontend\DataProcessing\DatabaseQueryProcessor
10 {
table = tx_news_domain_model_news
#pid for news folder
pidInList = 123
where.data = page : uid
where.wrap = internalurl=|
as = displayedNews
dataProcessing {
10 = MyCompany\MyExtension\DataProcessor\NewsDataProcessor
10 {
field = uid
}
}
}
注意:internalUrl 是一个字符串。对我来说,它是这样工作的,但可能需要拼写链接语法。欢迎任何查询改进!
第 2 步:在 MyExtension/Classes/DataProcessor/NewsDataProcessor.php 中添加 NewsDataProcessor
<?php
namespace MyCompany\MyExtension\DataProcessor;
use GeorgRinger\News\Domain\Repository\NewsRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;
class NewsDataProcessor implements DataProcessorInterface
{
/**
* @param \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $cObj
* @param array $contentObjectConfiguration
* @param array $processorConfiguration
* @param array $processedData
* @return array
*/
public function process(
ContentObjectRenderer $cObj,
array $contentObjectConfiguration,
array $processorConfiguration,
array $processedData
) {
/** @var \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager */
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
/** @var \GeorgRinger\News\Domain\Repository\NewsRepository $newsRepository */
$newsRepository = $objectManager->get(NewsRepository::class);
$field = 'uid';
if (array_key_exists('field',$processorConfiguration)) {
$field = $processorConfiguration['field'];
}
$newsArray = $processedData['data'];
$news = $newsRepository->findByUid((int)$newsArray[$field], false);
$processedData['news'] = $news;
return $processedData;
}
}
在以后的 EXT:news 版本(当前为 7.0.7)中,可能会有一个 DataProcessor 可用,那么您可以跳过这一步并使用现有的一个
第 3 步:将 n:excludeDisplayedNews Viewhelper 添加到页面的流体模板中。不要忘记将 Viewhelper 命名空间添加到模板中。
<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
xmlns:n="http://typo3.org/ns/GeorgRinger/News/ViewHelpers"
data-namespace-typo3-fluid="true">
<f:layout name="MyLayout"/>
<f:section name="main">
<f:for each="{displayedNews}" as="newsItem">
<n:excludeDisplayedNews newsItem="{newsItem.news}" />
</f:for>
<div class="main-content">
...
</div>
</f:section>
</html>