TYPO3 CMS  TYPO3_8-7
TranslateElementPropertyViewHelper.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
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 
26 
34 {
36 
42  public function initializeArguments()
43  {
44  parent::initializeArguments();
45  $this->registerArgument('element', RootRenderableInterface::class, 'Form Element to translate', true);
46  $this->registerArgument('property', 'mixed', 'Property to translate', false);
47  $this->registerArgument('renderingOptionProperty', 'mixed', 'Property to translate', false);
48  }
49 
59  public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
60  {
61  static::assertArgumentTypes($arguments);
62 
63  $element = $arguments['element'];
64 
65  $property = null;
66  if (!empty($arguments['property'])) {
67  $property = $arguments['property'];
68  } elseif (!empty($arguments['renderingOptionProperty'])) {
69  $property = $arguments['renderingOptionProperty'];
70  }
71 
72  if (empty($property)) {
73  $propertyParts = [];
74  } elseif (is_array($property)) {
75  $propertyParts = $property;
76  } else {
77  $propertyParts = explode('.', $property);
78  if (count($propertyParts) > 1 && $propertyParts[0] === 'options') {
80  'EXT:form - translations for form element "options" with a string property path like ' .
81  '"{formvh:translateElementProperty(element: element, property: \'options.{value}\')}" ' .
82  'are deprecated since TYPO3 v8 and will be removed in TYPO3 v9. Use an array like ' .
83  '"{formvh:translateElementProperty(element: element, property: {0: \'options\', 1: \'value\'})}" instead.'
84  );
85  array_shift($propertyParts);
86  $propertyParts = [
87  'options',
88  htmlspecialchars_decode(implode('.', $propertyParts), ENT_QUOTES)
89  ];
90  } else {
91  $propertyParts = [$property];
92  }
93  }
94 
96  $formRuntime = $renderingContext
97  ->getViewHelperVariableContainer()
98  ->get(RenderRenderableViewHelper::class, 'formRuntime');
99 
100  return TranslationService::getInstance()->translateFormElementValue($element, $propertyParts, $formRuntime);
101  }
102 
106  protected static function assertArgumentTypes(array $arguments)
107  {
108  foreach (['property', 'renderingOptionProperty'] as $argumentName) {
109  if (
110  !isset($arguments[$argumentName])
111  || is_string($arguments[$argumentName])
112  || is_array($arguments[$argumentName])
113  ) {
114  continue;
115  }
116  throw new Exception(
117  sprintf(
118  'Arguments "%s" either must be string or array',
119  $argumentName
120  ),
121  1504871830
122  );
123  }
124  }
125 }