Passed
Push — master ( 862eff...ee24ba )
by
unknown
29:12
created

TcaColumnsProcessFieldDescriptions::addData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
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\FormDataProvider;
19
20
use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
21
use TYPO3\CMS\Core\Localization\LanguageService;
22
23
/**
24
 * Works on processedTca to determine the final value of field descriptions.
25
 *
26
 * processedTca['columns']['aField']['description']
27
 */
28
class TcaColumnsProcessFieldDescriptions implements FormDataProviderInterface
29
{
30
    /**
31
     * Iterate over all processedTca columns fields
32
     *
33
     * @param array $result Result array
34
     * @return array Modified result array
35
     */
36
    public function addData(array $result): array
37
    {
38
        $result = $this->setDescriptionFromPageTsConfig($result);
39
        $result = $this->translateDescriptions($result);
40
        return $result;
41
    }
42
43
    /**
44
     * page TSconfig can override description:
45
     *
46
     * TCEFORM.aTable.aField.description = override
47
     * TCEFORM.aTable.aField.description.en = override
48
     *
49
     * @param array $result Result array
50
     * @return array Modified result array
51
     */
52
    protected function setDescriptionFromPageTsConfig(array $result): array
53
    {
54
        $languageService = $this->getLanguageService();
55
        $tableName = $result['tableName'];
56
        foreach ($result['processedTca']['columns'] ?? [] as $fieldName => $fieldConfiguration) {
57
            $fieldTSconfig = $result['pageTsConfig']['TCEFORM.'][$tableName . '.'][$fieldName . '.'] ?? null;
58
            if (!is_array($fieldTSconfig)) {
59
                continue;
60
            }
61
            if (!empty($fieldTSconfig['description'])) {
62
                $result['processedTca']['columns'][$fieldName]['description'] = $fieldTSconfig['description'];
63
            }
64
            if (!empty($fieldTSconfig['description.'][$languageService->lang])) {
65
                $result['processedTca']['columns'][$fieldName]['description'] = $fieldTSconfig['description.'][$languageService->lang];
66
            }
67
        }
68
        return $result;
69
    }
70
71
    /**
72
     * Translate all descriptions if needed.
73
     *
74
     * @param array $result Result array
75
     * @return array Modified result array
76
     */
77
    protected function translateDescriptions(array $result): array
78
    {
79
        $languageService = $this->getLanguageService();
80
        foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfiguration) {
81
            if (!isset($fieldConfiguration['description'])) {
82
                continue;
83
            }
84
            $result['processedTca']['columns'][$fieldName]['description'] = $languageService->sL($fieldConfiguration['description']);
85
        }
86
        return $result;
87
    }
88
89
    /**
90
     * @return LanguageService
91
     */
92
    protected function getLanguageService(): LanguageService
93
    {
94
        return $GLOBALS['LANG'];
95
    }
96
}
97