-
Notifications
You must be signed in to change notification settings - Fork 14
/
PreviewRenderer.php
146 lines (136 loc) · 6.47 KB
/
PreviewRenderer.php
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* 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.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\ContentBlocks\Backend\Preview;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Component\VarExporter\Exception\NotInstantiableTypeException;
use Symfony\Component\VarExporter\VarExporter;
use TYPO3\CMS\Backend\Preview\StandardContentPreviewRenderer;
use TYPO3\CMS\Backend\View\BackendLayout\Grid\GridColumnItem;
use TYPO3\CMS\ContentBlocks\DataProcessing\ContentBlockDataDecorator;
use TYPO3\CMS\ContentBlocks\DataProcessing\RelationResolver;
use TYPO3\CMS\ContentBlocks\Definition\ContentType\ContentType;
use TYPO3\CMS\ContentBlocks\Definition\TableDefinitionCollection;
use TYPO3\CMS\ContentBlocks\Registry\ContentBlockRegistry;
use TYPO3\CMS\ContentBlocks\Utility\ContentBlockPathUtility;
use TYPO3\CMS\Core\Cache\Frontend\PhpFrontend;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;
/**
* Sets up Fluid and applies the same DataProcessor as the frontend to the data record.
*
* @internal Not part of TYPO3's public API.
*/
class PreviewRenderer extends StandardContentPreviewRenderer
{
public function __construct(
protected TableDefinitionCollection $tableDefinitionCollection,
protected RelationResolver $relationResolver,
protected ContentBlockRegistry $contentBlockRegistry,
protected ContentBlockDataDecorator $contentBlockDataDecorator,
protected PhpFrontend $cache,
protected RootPathsSettings $rootPathsSettings,
) {}
public function renderPageModulePreviewContent(GridColumnItem $item): string
{
/** @var ServerRequestInterface $request */
$request = $GLOBALS['TYPO3_REQUEST'];
$record = $item->getRecord();
$typeField = ContentType::CONTENT_ELEMENT->getTypeField();
$contentElementTable = ContentType::CONTENT_ELEMENT->getTable();
$cacheIdentifier = $contentElementTable . '-' . $record['uid'] . '-' . md5(json_encode($record));
$typeName = $record[$typeField];
$contentElementDefinition = $this->tableDefinitionCollection->getContentElementDefinition($typeName);
$contentBlockExtPath = $this->contentBlockRegistry->getContentBlockExtPath($contentElementDefinition->getName());
$contentBlockPrivatePath = $contentBlockExtPath . '/' . ContentBlockPathUtility::getPrivateFolder();
// Fall back to standard preview rendering if EditorPreview.html does not exist.
$editorPreviewExtPath = $contentBlockExtPath . '/' . ContentBlockPathUtility::getBackendPreviewPath();
$editorPreviewAbsPath = GeneralUtility::getFileAbsFileName($editorPreviewExtPath);
if (!file_exists($editorPreviewAbsPath)) {
$result = parent::renderPageModulePreviewContent($item);
return $result;
}
$contentElementTableDefinition = $this->tableDefinitionCollection->getTable($contentElementTable);
if ($this->cache->has($cacheIdentifier)) {
$resolvedData = $this->cache->require($cacheIdentifier);
} else {
$this->relationResolver->setRequest($request);
$resolvedData = $this->relationResolver->resolve(
$contentElementDefinition,
$contentElementTableDefinition,
$record,
$contentElementTable,
);
// Avoid flooding cache with redundant data.
if ($resolvedData->resolved !== $record) {
try {
$exported = 'return ' . VarExporter::export($resolvedData) . ';';
$this->cache->set($cacheIdentifier, $exported, ['content_blocks_preview'], 0);
} catch (NotInstantiableTypeException) {
// @todo objects of class TYPO3\CMS\Core\Resource\File can't be exported
// @todo due to attached storage, which itself has EventDispatcher attached
// @todo which eventually leads to illegal Closures for EventListeners.
// @todo Right now, this happens for relations of type "folder".
}
}
}
$data = $this->contentBlockDataDecorator->decorate(
$contentElementDefinition,
$contentElementTableDefinition,
$resolvedData,
$item->getContext(),
);
$view = $this->createView($contentBlockPrivatePath, $request, $item);
$view->assign('data', $data);
$result = (string)$view->render();
return $result;
}
protected function createView(
string $contentBlockPrivatePath,
ServerRequestInterface $request,
GridColumnItem $item
): StandaloneView {
$pageUid = $item->getContext()->getPageId();
$view = GeneralUtility::makeInstance(StandaloneView::class);
$view->setLayoutRootPaths($this->getContentBlocksLayoutRootPaths($contentBlockPrivatePath, $pageUid));
$view->setPartialRootPaths($this->getContentBlocksPartialRootPaths($contentBlockPrivatePath, $pageUid));
$view->setTemplateRootPaths([$contentBlockPrivatePath]);
$view->setTemplate(ContentBlockPathUtility::getBackendPreviewFileNameWithoutExtension());
$view->setRequest($request);
return $view;
}
/**
* @return array<int, string>
*/
protected function getContentBlocksPartialRootPaths(string $contentBlockPrivatePath, int $pageUid): array
{
$contentBlockPartialRootPaths = $this->rootPathsSettings->getContentBlocksPartialRootPaths($pageUid);
$partialRootPaths = [
'EXT:backend/Resources/Private/Partials/',
'EXT:content_blocks/Resources/Private/Partials/',
...$contentBlockPartialRootPaths,
$contentBlockPrivatePath . '/Partials/',
];
return $partialRootPaths;
}
/**
* @return array<int, string>
*/
protected function getContentBlocksLayoutRootPaths(string $contentBlockPrivatePath, int $pageUid): array
{
$layoutRootPaths = $this->rootPathsSettings->getContentBlocksLayoutRootPaths($pageUid);
$layoutRootPaths[] = $contentBlockPrivatePath . '/Layouts/';
return $layoutRootPaths;
}
}