Passed
Push — master ( 768705...6c08ab )
by
unknown
23:45 queued 10:02
created

BackendLayoutFromParentPage::render()   B

Complexity

Conditions 10
Paths 12

Size

Total Lines 53
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 53
rs 7.6666
cc 10
nc 12
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
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
namespace TYPO3\CMS\Backend\Form\FieldInformation;
19
20
use TYPO3\CMS\Backend\Form\AbstractNode;
21
use TYPO3\CMS\Backend\View\BackendLayoutView;
22
use TYPO3\CMS\Core\Localization\LanguageService;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25
/**
26
 * This field information node is used for the pages backend_layout
27
 * field to inform about a possible backend layout, inherited form
28
 * a parent page.
29
 *
30
 * @internal
31
 */
32
class BackendLayoutFromParentPage extends AbstractNode
33
{
34
    /**
35
     * Handler for single nodes
36
     *
37
     * @return array As defined in initializeResultArray() of AbstractNode
38
     */
39
    public function render(): array
40
    {
41
        if ($this->data['tableName'] !== 'pages' || $this->data['fieldName'] !== 'backend_layout') {
42
            throw new \RuntimeException(
43
                'The backendLayoutFromParentPage field information can only be used for the backend_layout field of the pages table.',
44
                1622109821
45
            );
46
        }
47
48
        $resultArray = $this->initializeResultArray();
49
        $parameterArray = $this->data['parameterArray'];
50
51
        // In case the backend_layout field of the current page is not empty, no backend layout will be inherited.
52
        if (!empty($parameterArray['itemFormElValue'])) {
53
            return $resultArray;
54
        }
55
56
        $backendLayoutInformation = '';
57
        $languageService = $this->getLanguageService();
58
59
        if ($this->data['command'] === 'new') {
60
            // In case we deal with a new record, we try to find a possible inherited backend layout in
61
            // the rootline. Since there might be further actions, e.g. DataHandler hooks, the actually
62
            // resolved backend layout can only be determined, once the record is saved. For now we just
63
            // inform about the backend layout, which will most likely be used.
64
            foreach ($this->data['rootline'] as $page) {
65
                if (!empty($page['backend_layout_next_level']) && ($page['uid'] ?? false)) {
66
                    $backendLayoutInformation = sprintf(
67
                        $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:formEngine.pages.backendLayout.information.inheritFromParentPage'),
68
                        $this->getFieldValueLabel($parameterArray['fieldConf'], $page['backend_layout_next_level'])
69
                    );
70
                    break;
71
                }
72
            }
73
        } else {
74
            // Get the resolved backend layout for the current page.
75
            $backendLayoutView = GeneralUtility::makeInstance(BackendLayoutView::class);
76
            $backendLayout = $backendLayoutView->getBackendLayoutForPage(
77
                $this->data['databaseRow']['uid'] ?? $this->data['effectivePid'] ?? 0
78
            );
79
            if ($backendLayout !== null) {
80
                $backendLayoutInformation = sprintf(
81
                    $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:formEngine.pages.backendLayout.information.inheritedFromParentPage'),
82
                    $languageService->sL($backendLayout->getTitle())
83
                );
84
            }
85
        }
86
87
        if ($backendLayoutInformation !== '') {
88
            $resultArray['html'] = '<p class="text-muted">' . htmlspecialchars($backendLayoutInformation) . '</p>';
89
        }
90
91
        return $resultArray;
92
    }
93
94
    protected function getFieldValueLabel(array $fieldConfiguration, string $fieldValue): string
95
    {
96
        foreach ($fieldConfiguration['config']['items'] as $item) {
97
            if (($item[1] ?? '') === $fieldValue && !empty($item[0])) {
98
                return $item[0];
99
            }
100
        }
101
102
        $invalidValue = sprintf(
103
            $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.noMatchingValue'),
104
            $fieldValue
105
        );
106
107
        return '[ ' . $invalidValue . ' ]';
108
    }
109
110
    protected function getLanguageService(): LanguageService
111
    {
112
        return $GLOBALS['LANG'];
113
    }
114
}
115