-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathContentPreview.php
More file actions
104 lines (91 loc) · 3.99 KB
/
ContentPreview.php
File metadata and controls
104 lines (91 loc) · 3.99 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
<?php
declare(strict_types=1);
namespace B13\Backendpreviews\Backend\Preview;
/*
* This file is part of TYPO3 CMS-extension backendpreviews 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\Backendpreviews\Service\DatabaseRowService;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Backend\View\PageLayoutContext;
use TYPO3\CMS\Core\Domain\RecordInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\View\ViewFactoryData;
use TYPO3\CMS\Core\View\ViewFactoryInterface;
use TYPO3\CMS\Fluid\View\StandaloneView;
use TYPO3Fluid\Fluid\View\Exception\InvalidTemplateResourceException;
class ContentPreview
{
public function render(RecordInterface $record, PageLayoutContext $context): ?string
{
$previewConfiguration = BackendUtility::getPagesTSconfig($record->getPid())['mod.']['web_layout.']['tt_content.']['preview.'] ?? [];
if (!$previewConfiguration) {
// Early return in case no preview configuration can be found
return null;
}
$fluidConfiguration = $previewConfiguration['view.'] ?? [];
if (!$fluidConfiguration) {
// Early return in case no fluid template configuration can be found
return null;
}
$templateConfiguration = $previewConfiguration['template.'] ?? [];
$cType = $record->getRecordType();
if (!empty($templateConfiguration[$cType])) {
$fluidTemplateName = $templateConfiguration[$cType];
} else {
$fluidTemplateName = $cType;
}
$viewFactory = GeneralUtility::makeInstance(ViewFactoryInterface::class);
$view = $viewFactory->create(
new ViewFactoryData(
$fluidConfiguration['templateRootPaths.'] ?? null,
$fluidConfiguration['partialRootPaths.'] ?? null,
$fluidConfiguration['layoutRootPaths.'] ?? null,
null,
$context->getCurrentRequest()
)
);
$data = GeneralUtility::makeInstance(DatabaseRowService::class)->getAdditionalDataForView($record, $context);
$view->assignMultiple($data);
$view->assign('record', $record);
try {
return $view->render($fluidTemplateName);
} catch (InvalidTemplateResourceException) {
}
return null;
}
public function renderLegacy(array $row): ?string
{
$previewConfiguration = BackendUtility::getPagesTSconfig($row['pid'])['mod.']['web_layout.']['tt_content.']['preview.'] ?? [];
if (!$previewConfiguration) {
// Early return in case no preview configuration can be found
return null;
}
$fluidConfiguration = $previewConfiguration['view.'] ?? [];
if (!$fluidConfiguration) {
// Early return in case no fluid template configuration can be found
return null;
}
$templateConfiguration = $previewConfiguration['template.'] ?? [];
if ($row['CType'] === 'list' && !empty($row['list_type']) && !empty($templateConfiguration['list.'][$row['list_type']])) {
$fluidTemplateName = $templateConfiguration['list.'][$row['list_type']];
} elseif (!empty($templateConfiguration[$row['CType']])) {
$fluidTemplateName = $templateConfiguration[$row['CType']];
} else {
$fluidTemplateName = $row['CType'];
}
$view = GeneralUtility::makeInstance(StandaloneView::class);
$view->setLayoutRootPaths($fluidConfiguration['layoutRootPaths.'] ?? []);
$view->setPartialRootPaths($fluidConfiguration['partialRootPaths.'] ?? []);
$view->setTemplateRootPaths($fluidConfiguration['templateRootPaths.'] ?? []);
$view->setTemplate($fluidTemplateName);
$view->assignMultiple($row);
if ($view->hasTemplate()) {
return $view->render();
}
return null;
}
}