• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TYPO3-Headless / headless / 12065205662

28 Nov 2024 08:32AM UTC coverage: 72.782%. Remained the same
12065205662

push

github

web-flow
[BUGFIX] Don't render double slashes in file URLs (#796)

Without a frontendApiProxy in the site config, URLs to local files contain two slashes like www.mysite.org//fileadmin/my-image.jpg

1083 of 1488 relevant lines covered (72.78%)

8.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/Classes/Form/Service/FormTranslationService.php
1
<?php
2

3
/*
4
 * This file is part of the "headless" Extension for TYPO3 CMS.
5
 *
6
 * For the full copyright and license information, please read the
7
 * LICENSE.md file that was distributed with this source code.
8
 */
9

10
declare(strict_types=1);
11

12
namespace FriendsOfTYPO3\Headless\Form\Service;
13

14
use InvalidArgumentException;
15
use TYPO3\CMS\Core\Utility\ArrayUtility;
16
use TYPO3\CMS\Core\Utility\Exception\MissingArrayPathException;
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
use TYPO3\CMS\Form\Service\TranslationService;
19

20
class FormTranslationService extends TranslationService
21
{
22
    /**
23
     * @return FormTranslationService
24
     */
25
    public static function getInstance(): self
26
    {
27
        return GeneralUtility::makeInstance(FormTranslationService::class);
×
28
    }
29

30
    /**
31
     * @param array<string,mixed> $element
32
     * @param array<int,mixed> $propertyParts
33
     * @param array<string,mixed> $formRuntime
34
     * @return string|array<int,string>
35
     * @throws InvalidArgumentException
36
     * @internal
37
     */
38
    public function translateElementValue(
39
        array $element,
40
        array $propertyParts,
41
        array $formRuntime
42
    ) {
43
        if (empty($propertyParts)) {
×
44
            throw new InvalidArgumentException('The argument "propertyParts" is empty', 1476216007);
×
45
        }
46

47
        $propertyType = 'properties';
×
48
        $property = implode('.', $propertyParts);
×
49
        $renderingOptions = $element['renderingOptions'] ?? [];
×
50
        $element['properties'] = $element['properties'] ?? [];
×
51

52
        if ($property === 'label') {
×
53
            $defaultValue = $element['label'] ?? '';
×
54
        } elseif (($element['type'] ?? '') !== 'Page') {
×
55
            try {
56
                $defaultValue = ArrayUtility::getValueByPath($element['properties'], $propertyParts, '.');
×
57
            } catch (MissingArrayPathException $exception) {
×
58
                $defaultValue = null;
×
59
            }
60
        } else {
61
            $propertyType = 'renderingOptions';
×
62
            try {
63
                $defaultValue = ArrayUtility::getValueByPath($renderingOptions, $propertyParts, '.');
×
64
            } catch (MissingArrayPathException $exception) {
×
65
                $defaultValue = null;
×
66
            }
67
        }
68

69
        $translatePropertyValueIfEmpty = $renderingOptions['translation']['translatePropertyValueIfEmpty'] ?? true;
×
70

71
        if (empty($defaultValue) && !$translatePropertyValueIfEmpty) {
×
72
            return $defaultValue;
×
73
        }
74

75
        $defaultValue = empty($defaultValue) ? '' : $defaultValue;
×
76
        $translationFiles = $renderingOptions['translation']['translationFiles'] ?? [];
×
77
        if (empty($translationFiles)) {
×
78
            $formRuntime['renderingOptions'] = $formRuntime['renderingOptions'] ?? [];
×
79
            $translationFiles = $formRuntime['renderingOptions']['translation']['translationFiles'];
×
80
        }
81

82
        $translationFiles = $this->sortArrayWithIntegerKeysDescending($translationFiles);
×
83
        $language = $renderingOptions['translation']['language'] ?? null;
×
84

85
        try {
86
            $arguments = ArrayUtility::getValueByPath(
×
87
                $renderingOptions['translation']['arguments'] ?? [],
×
88
                $propertyParts,
×
89
                '.'
×
90
            );
×
91
        } catch (MissingArrayPathException $e) {
×
92
            $arguments = [];
×
93
        }
94

95
        $originalFormIdentifier = null;
×
96
        if (isset($formRuntime['renderingOptions']['_originalIdentifier'])) {
×
97
            $originalFormIdentifier = $formRuntime['renderingOptions']['_originalIdentifier'];
×
98
        }
99

100
        if ($property === 'options' && is_array($defaultValue)) {
×
101
            foreach ($defaultValue as $optionValue => &$optionLabel) {
×
102
                $translationKeyChain = [];
×
103
                foreach ($translationFiles as $translationFile) {
×
104
                    if (!empty($originalFormIdentifier)) {
×
105
                        $translationKeyChain[] = sprintf(
×
106
                            '%s:%s.element.%s.%s.%s.%s',
×
107
                            $translationFile,
×
108
                            $originalFormIdentifier,
×
109
                            $element['identifier'],
×
110
                            $propertyType,
×
111
                            $property,
×
112
                            $optionValue
×
113
                        );
×
114
                    }
115
                    $translationKeyChain[] = sprintf(
×
116
                        '%s:%s.element.%s.%s.%s.%s',
×
117
                        $translationFile,
×
118
                        $formRuntime['identifier'],
×
119
                        $element['identifier'],
×
120
                        $propertyType,
×
121
                        $property,
×
122
                        $optionValue
×
123
                    );
×
124
                    $translationKeyChain[] = sprintf(
×
125
                        '%s:element.%s.%s.%s.%s',
×
126
                        $translationFile,
×
127
                        $element['identifier'],
×
128
                        $propertyType,
×
129
                        $property,
×
130
                        $optionValue
×
131
                    );
×
132
                    $translationKeyChain[] = sprintf(
×
133
                        '%s:element.%s.%s.%s.%s',
×
134
                        $translationFile,
×
135
                        $element['type'],
×
136
                        $propertyType,
×
137
                        $property,
×
138
                        $optionValue
×
139
                    );
×
140
                }
141

142
                $translatedValue = $this->processTranslationChain($translationKeyChain, $language, $arguments);
×
143
                $optionLabel = empty($translatedValue) ? $optionLabel : $translatedValue;
×
144
            }
145
            $translatedValue = $defaultValue;
×
146
        } elseif ($property === 'fluidAdditionalAttributes' && is_array($defaultValue)) {
×
147
            foreach ($defaultValue as $propertyName => &$propertyValue) {
×
148
                $translationKeyChain = [];
×
149
                foreach ($translationFiles as $translationFile) {
×
150
                    if (!empty($originalFormIdentifier)) {
×
151
                        $translationKeyChain[] = sprintf(
×
152
                            '%s:%s.element.%s.%s.%s',
×
153
                            $translationFile,
×
154
                            $originalFormIdentifier,
×
155
                            $element['identifier'],
×
156
                            $propertyType,
×
157
                            $propertyName
×
158
                        );
×
159
                    }
160
                    $translationKeyChain[] = sprintf(
×
161
                        '%s:%s.element.%s.%s.%s',
×
162
                        $translationFile,
×
163
                        $formRuntime['identifier'],
×
164
                        $element['identifier'],
×
165
                        $propertyType,
×
166
                        $propertyName
×
167
                    );
×
168
                    $translationKeyChain[] = sprintf(
×
169
                        '%s:element.%s.%s.%s',
×
170
                        $translationFile,
×
171
                        $element['identifier'],
×
172
                        $propertyType,
×
173
                        $propertyName
×
174
                    );
×
175
                    $translationKeyChain[] = sprintf(
×
176
                        '%s:element.%s.%s.%s',
×
177
                        $translationFile,
×
178
                        $element['type'],
×
179
                        $propertyType,
×
180
                        $propertyName
×
181
                    );
×
182
                }
183

184
                $translatedValue = $this->processTranslationChain($translationKeyChain, $language, $arguments);
×
185
                $propertyValue = empty($translatedValue) ? $propertyValue : $translatedValue;
×
186
            }
187
            $translatedValue = $defaultValue;
×
188
        } else {
189
            $translationKeyChain = [];
×
190
            foreach ($translationFiles as $translationFile) {
×
191
                if (!empty($originalFormIdentifier)) {
×
192
                    $translationKeyChain[] = sprintf(
×
193
                        '%s:%s.element.%s.%s.%s',
×
194
                        $translationFile,
×
195
                        $originalFormIdentifier,
×
196
                        $element['identifier'],
×
197
                        $propertyType,
×
198
                        $property
×
199
                    );
×
200
                }
201
                $translationKeyChain[] = sprintf(
×
202
                    '%s:%s.element.%s.%s.%s',
×
203
                    $translationFile,
×
204
                    'identifier',
×
205
                    $element['identifier'],
×
206
                    $propertyType,
×
207
                    $property
×
208
                );
×
209
                $translationKeyChain[] = sprintf(
×
210
                    '%s:element.%s.%s.%s',
×
211
                    $translationFile,
×
212
                    $element['identifier'],
×
213
                    $propertyType,
×
214
                    $property
×
215
                );
×
216
                $translationKeyChain[] = sprintf(
×
217
                    '%s:element.%s.%s.%s',
×
218
                    $translationFile,
×
219
                    $element['type'] ?? '',
×
220
                    $propertyType,
×
221
                    $property
×
222
                );
×
223
            }
224

225
            $translatedValue = $this->processTranslationChain($translationKeyChain, $language, $arguments);
×
226
            $translatedValue = empty($translatedValue) ? $defaultValue : $translatedValue;
×
227
        }
228

229
        return $translatedValue;
×
230
    }
231

232
    /**
233
     * @param array<string,mixed> $element
234
     * @param int $code
235
     * @param array<string,mixed> $formRuntime
236
     * @param array<string,mixed> $arguments
237
     * @param string $defaultValue
238
     * @return string
239
     */
240
    public function translateElementError(
241
        array $element,
242
        int $code,
243
        array $formRuntime,
244
        array $arguments = [],
245
        string $defaultValue = ''
246
    ): string {
247
        if (empty($code)) {
×
248
            throw new InvalidArgumentException('The argument "code" is empty', 1489272978);
×
249
        }
250

251
        $renderingOptions = $element['renderingOptions'] ?? [];
×
252
        $translationFiles = $renderingOptions['translation']['translationFiles'] ?? [];
×
253
        if (empty($translationFiles)) {
×
254
            $translationFiles = $formRuntime['renderingOptions']['translation']['translationFiles'];
×
255
        }
256

257
        $translationFiles = $this->sortArrayWithIntegerKeysDescending($translationFiles);
×
258
        $language = $renderingOptions['language'] ?? null;
×
259
        $originalFormIdentifier = $formRuntime['renderingOptions']['_originalIdentifier'] ?? null;
×
260

261
        $translationKeyChain = [];
×
262
        foreach ($translationFiles as $translationFile) {
×
263
            if (!empty($originalFormIdentifier)) {
×
264
                $translationKeyChain[] = sprintf(
×
265
                    '%s:%s.validation.error.%s.%s',
×
266
                    $translationFile,
×
267
                    $originalFormIdentifier,
×
268
                    $element['identifier'],
×
269
                    $code
×
270
                );
×
271

272
                $translationKeyChain[] = sprintf(
×
273
                    '%s:%s.validation.error.%s',
×
274
                    $translationFile,
×
275
                    $originalFormIdentifier,
×
276
                    $code
×
277
                );
×
278
            }
279
            $translationKeyChain[] = sprintf(
×
280
                '%s:%s.validation.error.%s.%s',
×
281
                $translationFile,
×
282
                $formRuntime['identifier'],
×
283
                $element['identifier'],
×
284
                $code
×
285
            );
×
286
            $translationKeyChain[] = sprintf(
×
287
                '%s:%s.validation.error.%s',
×
288
                $translationFile,
×
289
                $formRuntime['identifier'],
×
290
                $code
×
291
            );
×
292
            $translationKeyChain[] = sprintf(
×
293
                '%s:validation.error.%s.%s',
×
294
                $translationFile,
×
295
                $element['identifier'],
×
296
                $code
×
297
            );
×
298
            $translationKeyChain[] = sprintf('%s:validation.error.%s', $translationFile, $code);
×
299
        }
300

301
        $translatedValue = $this->processTranslationChain($translationKeyChain, $language, $arguments);
×
302

303
        return empty($translatedValue) ? $defaultValue : $translatedValue;
×
304
    }
305
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc