Completed
Push — master ( 7e342e...13c67c )
by
unknown
19:15
created

StandardPreviewRendererResolver   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 55
rs 10
wmc 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B resolveRendererFor() 0 45 10
1
<?php
2
declare(strict_types = 1);
3
namespace TYPO3\CMS\Backend\Preview;
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20
/**
21
 * Class StandardPreviewRendererResolver
22
 *
23
 * Default implementation of PreviewRendererResolverInterface.
24
 * Scans TCA configuration to detect:
25
 *
26
 * - TCA.$table.types.$typeFromTypeField.previewRenderer
27
 * - TCA.$table.ctrl.previewRenderer
28
 *
29
 * Depending on which one is defined and checking the first, type-specific
30
 * variant first.
31
 */
32
class StandardPreviewRendererResolver implements PreviewRendererResolverInterface
33
{
34
    /**
35
     * @param string $table The name of the table the returned PreviewRenderer must work with
36
     * @param array $row A record from $table which will be previewed - allows returning a different PreviewRenderer based on record attributes
37
     * @param int $pageUid The UID of the page on which the preview will be rendered - allows returning a different PreviewRenderer based on for example pageTSconfig
38
     * @return PreviewRendererInterface
39
     * @throws \UnexpectedValueException
40
     * @throws \RuntimeException
41
     */
42
    public function resolveRendererFor(string $table, array $row, int $pageUid): PreviewRendererInterface
43
    {
44
        $tca = $GLOBALS['TCA'][$table];
45
        $tcaTypeField = $tca['ctrl']['type'] ?? null;
46
        $previewRendererClassName = null;
47
        if ($tcaTypeField) {
48
            $tcaTypeOfRow = $row[$tcaTypeField];
49
            $typeConfiguration = $tca['types'][$tcaTypeOfRow] ?? [];
50
51
            $subTypeValueField = $typeConfiguration['subtype_value_field'] ?? null;
52
            if (!empty($subTypeValueField) && !empty($typeConfiguration['previewRenderer']) && is_array($typeConfiguration['previewRenderer'])) {
53
                // An array of subtype_value_field indexed preview renderers was defined, look up the right
54
                // class to use for the sub-type defined in this $row.
55
                $previewRendererClassName = $typeConfiguration['previewRenderer'][$row[$subTypeValueField]] ?? null;
56
            }
57
58
            // If no class was found in the subtype_value_field
59
            if (!$previewRendererClassName && !empty($typeConfiguration['previewRenderer'])) {
60
                // A type-specific preview renderer was configured for the TCA type (and one was not detected
61
                // based on the higher-priority lookups above).
62
                $previewRendererClassName = $typeConfiguration['previewRenderer'];
63
            }
64
        }
65
66
        if (!$previewRendererClassName) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
67
68
            // Table either has no type field or no custom preview renderer was defined for the type.
69
            // Use table's standard renderer if any is defined.
70
            $previewRendererClassName = $tca['ctrl']['previewRenderer'] ?? null;
71
        }
72
73
        if (!empty($previewRendererClassName)) {
74
            if (!is_a($previewRendererClassName, PreviewRendererInterface::class, true)) {
75
                throw new \UnexpectedValueException(
76
                    sprintf(
77
                        'Class %s must implement %s',
78
                        $previewRendererClassName,
79
                        PreviewRendererInterface::class
80
                    ),
81
                    1477512798
82
                );
83
            }
84
            return GeneralUtility::makeInstance($previewRendererClassName);
85
        }
86
        throw new \RuntimeException(sprintf('No Preview renderer registered for table %s', $table), 1477520356);
87
    }
88
}
89