68a5a7233a3c55aee3d77072725abca72c731c85
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FieldWizard / OtherLanguageContent.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Backend\Form\FieldWizard;
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\Backend\Form\AbstractNode;
19 use TYPO3\CMS\Backend\Utility\BackendUtility;
20 use TYPO3\CMS\Core\Imaging\Icon;
21 use TYPO3\CMS\Core\Imaging\IconFactory;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24 /**
25 * Render values of "other" languages. If editing a localized row, this is typically
26 * the content value of the according default record, but it may render field values
27 * of other languages too, depending on configuration.
28 */
29 class OtherLanguageContent extends AbstractNode
30 {
31 /**
32 * Render other language content if enabled.
33 *
34 * @return array
35 */
36 public function render(): array
37 {
38 $result = $this->initializeResultArray();
39
40 $fieldName = $this->data['fieldName'];
41 $fieldConfig = $this->data['processedTca']['columns'][$fieldName];
42 $l10nDisplay = $this->data['parameterArray']['fieldConf']['l10n_display'] ?? '';
43 $defaultLanguageRow = $this->data['defaultLanguageRow'] ?? null;
44 if (!is_array($defaultLanguageRow)
45 || GeneralUtility::inList($l10nDisplay, 'hideDiff')
46 || GeneralUtility::inList($l10nDisplay, 'defaultAsReadonly')
47 || $fieldConfig['config']['type'] === 'inline'
48 || $fieldConfig['config']['type'] === 'flex'
49 || ($fieldConfig['config']['type'] === 'group' && isset($fieldConfig['config']['MM']))
50 || ($fieldConfig['config']['type'] === 'select' && isset($fieldConfig['config']['MM']))
51 ) {
52 // Early return if there is no default language row or the display is disabled
53 return $result;
54 }
55
56 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
57 $table = $this->data['tableName'];
58 $html = [];
59 $defaultLanguageValue = BackendUtility::getProcessedValue(
60 $table,
61 $fieldName,
62 $defaultLanguageRow[$fieldName],
63 0,
64 true,
65 false,
66 $defaultLanguageRow['uid'],
67 true,
68 $defaultLanguageRow['pid']
69 );
70 if ($defaultLanguageValue !== '') {
71 $html[] = '<div class="t3-form-original-language">';
72 $html[] = $iconFactory->getIcon($this->data['systemLanguageRows'][0]['flagIconIdentifier'], Icon::SIZE_SMALL)->render();
73 $html[] = $this->previewFieldValue($defaultLanguageValue, $fieldConfig, $fieldName);
74 $html[] = '</div>';
75 }
76 $additionalPreviewLanguages = $this->data['additionalLanguageRows'];
77 foreach ($additionalPreviewLanguages as $previewLanguage) {
78 $defaultLanguageValue = BackendUtility::getProcessedValue(
79 $table,
80 $fieldName,
81 $previewLanguage[$fieldName],
82 0,
83 true
84 );
85 if ($defaultLanguageValue !== '') {
86 $html[] = '<div class="t3-form-original-language">';
87 $html[] = $iconFactory->getIcon($this->data['systemLanguageRows'][$previewLanguage['sys_language_uid']]['flagIconIdentifier'], Icon::SIZE_SMALL)->render();
88 $html[] = $this->previewFieldValue($defaultLanguageValue, $fieldConfig, $fieldName);
89 $html[] = '</div>';
90 }
91 }
92 $result['html'] = implode(LF, $html);
93 return $result;
94 }
95
96 /**
97 * Rendering preview output of a field value which is not shown as a form field but just outputted.
98 *
99 * @param string $value The value to output
100 * @param array $config Configuration for field.
101 * @param string $field Name of field.
102 * @return string HTML formatted output
103 */
104 protected function previewFieldValue($value, $config, $field = '')
105 {
106 $value = (string)$value;
107 if ($config['config']['type'] === 'group'
108 && ($config['config']['internal_type'] === 'file' || $config['config']['internal_type'] === 'file_reference')
109 ) {
110 // Ignore upload folder if internal_type is file_reference
111 if ($config['config']['internal_type'] === 'file_reference') {
112 $config['config']['uploadfolder'] = '';
113 }
114 $table = 'tt_content';
115 // Making the array of file items:
116 $itemArray = GeneralUtility::trimExplode(',', $value, true);
117 // Showing thumbnails:
118 $imgs = [];
119 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
120 foreach ($itemArray as $imgRead) {
121 $imgParts = explode('|', $imgRead);
122 $imgPath = rawurldecode($imgParts[0]);
123 $rowCopy = [];
124 $rowCopy[$field] = $imgPath;
125 // Icon + click menu:
126 $absFilePath = GeneralUtility::getFileAbsFileName($config['config']['uploadfolder'] ? $config['config']['uploadfolder'] . '/' . $imgPath : $imgPath);
127 $fileInformation = pathinfo($imgPath);
128 $title = $fileInformation['basename'] . ($absFilePath && @is_file($absFilePath))
129 ? ' (' . GeneralUtility::formatSize(filesize($absFilePath)) . ')'
130 : ' - FILE NOT FOUND!';
131 $fileIcon = '<span title="' . htmlspecialchars($title) . '">' . $iconFactory->getIconForFileExtension($fileInformation['extension'], Icon::SIZE_SMALL)->render() . '</span>';
132 $imgs[] =
133 '<span class="text-nowrap">' .
134 BackendUtility::thumbCode(
135 $rowCopy,
136 $table,
137 $field,
138 '',
139 '',
140 $config['config']['uploadfolder'],
141 0,
142 ' align="middle"'
143 ) .
144 ($absFilePath ? BackendUtility::wrapClickMenuOnIcon($fileIcon, $absFilePath, 0) : $fileIcon) .
145 $imgPath .
146 '</span>';
147 }
148 return implode('<br />', $imgs);
149 } else {
150 return nl2br(htmlspecialchars($value));
151 }
152 }
153 }