[BUGFIX] Validation error arguments are not applied
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / ViewHelpers / TranslateElementErrorViewHelper.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Form\ViewHelpers;
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\Extbase\Validation\Error;
19 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
20 use TYPO3\CMS\Form\Domain\Model\Renderable\RootRenderableInterface;
21 use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
22 use TYPO3\CMS\Form\Service\TranslationService;
23 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
24 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
25
26 /**
27 * Translate form element properites.
28 *
29 * Scope: frontend / backend
30 * @api
31 */
32 class TranslateElementErrorViewHelper extends AbstractViewHelper
33 {
34 use CompileWithRenderStatic;
35
36 /**
37 * Initialize arguments.
38 *
39 * @internal
40 */
41 public function initializeArguments()
42 {
43 parent::initializeArguments();
44 $this->registerArgument('element', RootRenderableInterface::class, 'Form Element to translate', true);
45 $this->registerArgument('error', Error::class, '', false, '');
46 $this->registerArgument('code', 'integer', 'Error code - deprecated', false);
47 $this->registerArgument('arguments', 'array', 'Error arguments - deprecated', false, null);
48 $this->registerArgument('defaultValue', 'string', 'The default value - deprecated', false, '');
49 }
50
51 /**
52 * Return array element by key.
53 *
54 * @param array $arguments
55 * @param \Closure $renderChildrenClosure
56 * @param RenderingContextInterface $renderingContext
57 * @return string
58 * @api
59 */
60 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
61 {
62 $element = $arguments['element'];
63 $error = $arguments['error'];
64
65 $code = $arguments['code'];
66 $errorArguments = $arguments['arguments'];
67 $defaultValue = $arguments['defaultValue'];
68
69 if ($error instanceof Error) {
70 $code = $error->getCode();
71 $errorArguments = $error->getArguments();
72 $defaultValue = $error->__toString();
73 }
74
75 /** @var FormRuntime $formRuntime */
76 $formRuntime = $renderingContext
77 ->getViewHelperVariableContainer()
78 ->get(RenderRenderableViewHelper::class, 'formRuntime');
79
80 return TranslationService::getInstance()->translateFormElementError(
81 $element,
82 $code,
83 $errorArguments,
84 $defaultValue,
85 $formRuntime
86 );
87 }
88 }