[BUGFIX] Fix multi-checkbox/radiobox labels for values containing dots
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / ViewHelpers / TranslateElementPropertyViewHelper.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\Exception;
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 TranslateElementPropertyViewHelper 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('property', 'mixed', 'Property to translate', false);
46 $this->registerArgument('renderingOptionProperty', 'mixed', 'Property to translate', 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|array
56 * @api
57 */
58 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
59 {
60 static::assertArgumentTypes($arguments);
61
62 $element = $arguments['element'];
63
64 $property = null;
65 if (!empty($arguments['property'])) {
66 $property = $arguments['property'];
67 } elseif (!empty($arguments['renderingOptionProperty'])) {
68 $property = $arguments['renderingOptionProperty'];
69 }
70
71 if (empty($property)) {
72 $propertyParts = [];
73 } elseif (is_array($property)) {
74 $propertyParts = $property;
75 } else {
76 $propertyParts = [$property];
77 }
78
79 /** @var FormRuntime $formRuntime */
80 $formRuntime = $renderingContext
81 ->getViewHelperVariableContainer()
82 ->get(RenderRenderableViewHelper::class, 'formRuntime');
83
84 return TranslationService::getInstance()->translateFormElementValue($element, $propertyParts, $formRuntime);
85 }
86
87 /**
88 * @param array $arguments
89 */
90 protected static function assertArgumentTypes(array $arguments)
91 {
92 foreach (['property', 'renderingOptionProperty'] as $argumentName) {
93 if (
94 !isset($arguments[$argumentName])
95 || is_string($arguments[$argumentName])
96 || is_array($arguments[$argumentName])
97 ) {
98 continue;
99 }
100 throw new Exception(
101 sprintf(
102 'Arguments "%s" either must be string or array',
103 $argumentName
104 ),
105 1504871830
106 );
107 }
108 }
109 }