-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathContentAreaProcessor.php
More file actions
121 lines (103 loc) · 4.42 KB
/
ContentAreaProcessor.php
File metadata and controls
121 lines (103 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
declare(strict_types=1);
namespace B13\Container\DataProcessing;
/*
* This file is part of TYPO3 CMS-based extension "container" by b13.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*/
use B13\Container\Domain\Factory\FrontendContainerFactory;
use B13\Container\Tca\Registry;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Domain\RecordFactory;
use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Core\Page\ContentArea;
use TYPO3\CMS\Core\Page\ContentAreaClosure;
use TYPO3\CMS\Core\Page\ContentAreaCollection;
use TYPO3\CMS\Core\Page\ContentSlideMode;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentDataProcessor;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;
use function array_map;
/**
* Automatically detects if content element has container columns
* adds them lazily to the content variable.
* The ContentArea can be used in f:render.contentArea ViewHelper
*
* Only use this DataProcessor for TYPO3 v14 or higher:
*
* typoscript:
* lib.contentElement.dataProcessing.1773665522 = B13\Container\DataProcessing\ContentAreaProcessor
* #or
* tt_content.b13-2cols < lib.contentElement
* tt_content.b13-2cols {
* templateName = 2Cols
* templateRootPaths.10 = EXT:base/Resources/Private/Templates
* dataProcessing.100 = B13\Container\DataProcessing\ContentAreaProcessor
* }
*
* html:
* <f:render.contentArea contentArea="{content.200}" />
*
*/
#[Autoconfigure(public: true)]
readonly class ContentAreaProcessor implements DataProcessorInterface
{
public function __construct(
protected ContentDataProcessor $contentDataProcessor,
protected Context $context,
protected FrontendContainerFactory $frontendContainerFactory,
protected Registry $tcaRegistry,
protected RecordFactory $recordFactory,
protected Typo3Version $typo3Version,
protected LoggerInterface $logger,
) {}
public function process(
ContentObjectRenderer $cObj,
array $contentObjectConfiguration,
array $processorConfiguration,
array $processedData,
): array {
if (((float)$this->typo3Version->getBranch()) <= 14.1) {
$this->logger->error(ContentAreaProcessor::class . ' requires TYPO3 v14.2 or higher. Please check your configuration.');
return $processedData;
}
$record = $cObj->data;
$CType = $record['CType'] ?? '';
if (!$this->tcaRegistry->isContainerElement($CType)) {
return $processedData;
}
$columnsColPos = $this->tcaRegistry->getAllAvailableColumnsColPos($CType);
$container = null;
$areas = [];
foreach ($columnsColPos as $colPos) {
$areas[$colPos] = new ContentAreaClosure(
function () use (&$container, $CType, $cObj, $record, $colPos): ContentArea {
$container ??= $this->frontendContainerFactory->buildContainer($cObj, $this->context, (int)$record['uid']);
$contentDefenderConfiguration = $this->tcaRegistry->getContentDefenderConfiguration($CType, $colPos);
$rows = $container->getChildrenByColPos($colPos);
$records = array_map(fn($row) => $this->recordFactory->createFromDatabaseRow('tt_content', $row), $rows);
return new ContentArea(
(string)$colPos,
$this->tcaRegistry->getColPosName($record['CType'], $colPos),
$colPos,
ContentSlideMode::None,
GeneralUtility::trimExplode(',', $contentDefenderConfiguration['allowedContentTypes'] ?? '', true),
GeneralUtility::trimExplode(',', $contentDefenderConfiguration['disallowedContentTypes'] ?? '', true),
[
'container' => $container,
],
$records,
);
},
);
}
$processedData[$processedConfiguration['as'] ?? 'content'] = new ContentAreaCollection($areas);
return $processedData;
}
}