9d839f88aea17c411de6ba3abe6aa85abe90604c
[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\Fluid\Core\ViewHelper\AbstractViewHelper;
19 use TYPO3\CMS\Form\Domain\Model\Renderable\RootRenderableInterface;
20 use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
21 use TYPO3\CMS\Form\Service\TranslationService;
22 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
23 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
24
25 /**
26 * Translate form element properites.
27 *
28 * Scope: frontend / backend
29 * @api
30 */
31 class TranslateElementErrorViewHelper extends AbstractViewHelper
32 {
33 use CompileWithRenderStatic;
34
35 /**
36 * Initialize arguments.
37 *
38 * @internal
39 */
40 public function initializeArguments()
41 {
42 parent::initializeArguments();
43 $this->registerArgument('element', RootRenderableInterface::class, 'Form Element to translate', true);
44 $this->registerArgument('code', 'integer', 'Error code', true);
45 $this->registerArgument('arguments', 'array', 'Error arguments', false, null);
46 $this->registerArgument('defaultValue', 'string', 'The default value', false, '');
47 }
48
49 /**
50 * Return array element by key.
51 *
52 * @param array $arguments
53 * @param \Closure $renderChildrenClosure
54 * @param RenderingContextInterface $renderingContext
55 * @return string
56 * @api
57 */
58 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
59 {
60 $element = $arguments['element'];
61
62 /** @var FormRuntime $formRuntime */
63 $formRuntime = $renderingContext
64 ->getViewHelperVariableContainer()
65 ->get(RenderRenderableViewHelper::class, 'formRuntime');
66
67 return TranslationService::getInstance()->translateFormElementError(
68 $element,
69 $arguments['code'],
70 $arguments['arguments'],
71 $arguments['defaultValue'],
72 $formRuntime
73 );
74 }
75 }