ElementBackendPreview::hasBackendPreview()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 3
cp 0
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * Custom Backend Preview for Elements like Content Objects.
5
 */
6
declare(strict_types = 1);
7
8
namespace HDNET\Autoloader\Hooks;
9
10
use HDNET\Autoloader\Annotation\Hook;
11
use HDNET\Autoloader\Utility\ExtendedUtility;
12
use HDNET\Autoloader\Utility\ModelUtility;
13
use TYPO3\CMS\Backend\View\PageLayoutView;
14
use TYPO3\CMS\Backend\View\PageLayoutViewDrawItemHookInterface;
15
use TYPO3\CMS\Core\Cache\CacheManager;
16
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19
/**
20
 * Class ElementBackendPreview.
21
 *
22
 * @see  \TYPO3\CMS\Backend\View\PageLayoutView::tt_content_drawItem
23
 * @Hook("TYPO3_CONF_VARS|SC_OPTIONS|cms/layout/class.tx_cms_layout.php|tt_content_drawItem")
24
 */
25
class ElementBackendPreview implements PageLayoutViewDrawItemHookInterface
26
{
27
    /**
28
     * Preprocesses the preview rendering of a content element.
29
     *
30
     * @param PageLayoutView $parentObject  Calling parent object
31
     * @param bool           $drawItem      Whether to draw the item using the default functionalities
32
     * @param string         $headerContent Header content
33
     * @param string         $itemContent   Item content
34
     * @param array          $row           Record row of tt_content
35
     */
36
    public function preProcess(PageLayoutView &$parentObject, &$drawItem, &$headerContent, &$itemContent, array &$row): void
37
    {
38
        if (!$this->isAutoloaderContenobject($row)) {
39
            return;
40
        }
41
42
        if (!$this->hasBackendPreview($row)) {
43
            return;
44
        }
45
46
        $itemContent = $this->getBackendPreview($row);
47
        $drawItem = false;
48
    }
49
50
    /**
51
     * Render the Backend Preview Template and return the HTML.
52
     *
53
     * @param array $row
54
     *
55
     * @return string
56
     */
57
    public function getBackendPreview($row)
58
    {
59
        if (!$this->hasBackendPreview($row)) {
60
            return '';
61
        }
62
63
        // Workarround for wrong FrontendUserGroup Restriction in TYPO3 8.x
64
        if (TYPO3_MODE === 'BE' && !\is_object($GLOBALS['TSFE'])) {
65
            $GLOBALS['TSFE'] = new \stdClass();
66
            $GLOBALS['TSFE']->gr_list = '';
67
        }
68
69
        $cacheIdentifier = 'tt-content-preview-' . $row['uid'] . '-' . $row['tstamp'];
70
71
        /** @var FrontendInterface $cache */
72
        $cache = GeneralUtility::makeInstance(CacheManager::class)
73
            ->getCache('pagesection')
74
        ;
75
        if ($cache->has($cacheIdentifier)) {
76
            return $cache->get($cacheIdentifier);
77
        }
78
79
        $ctype = $row['CType'];
80
        /** @var array $config */
81
        $config = $GLOBALS['TYPO3_CONF_VARS']['AUTOLOADER']['ContentObject'][$ctype];
82
83
        $model = ModelUtility::getModel($config['modelClass'], $row);
84
85
        $view = ExtendedUtility::createExtensionStandaloneView($config['extensionKey'], $config['backendTemplatePath']);
86
        $view->assignMultiple([
87
            'data' => $row,
88
            'object' => $model,
89
        ]);
90
        $output = $view->render();
91
        $cache->set($cacheIdentifier, $output);
92
93
        return $output;
94
    }
95
96
    /**
97
     * Check if the ContentObject has a Backend Preview Template.
98
     *
99
     * @param array $row
100
     *
101
     * @return bool
102
     */
103
    public function hasBackendPreview($row)
104
    {
105
        if (!$this->isAutoloaderContenobject($row)) {
106
            return false;
107
        }
108
        $ctype = $row['CType'];
109
        /** @var array $config */
110
        $config = $GLOBALS['TYPO3_CONF_VARS']['AUTOLOADER']['ContentObject'][$ctype];
111
112
        $beTemplatePath = GeneralUtility::getFileAbsFileName($config['backendTemplatePath']);
113
114
        return is_file($beTemplatePath);
115
    }
116
117
    /**
118
     * Check if the the Element is registered by the ContenObject-Autoloader.
119
     *
120
     * @return bool
121
     */
122
    public function isAutoloaderContenobject(array $row)
123
    {
124
        $ctype = $row['CType'];
125
126
        return (bool)$GLOBALS['TYPO3_CONF_VARS']['AUTOLOADER']['ContentObject'][$ctype];
127
    }
128
}
129