‪TYPO3CMS  10.4
TranslateElementPropertyViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 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 
19 
23 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
24 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
25 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
26 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
27 
33 class ‪TranslateElementPropertyViewHelper extends AbstractViewHelper
34 {
35  use CompileWithRenderStatic;
36 
42  public function ‪initializeArguments()
43  {
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 
57  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
58  {
59  static::assertArgumentTypes($arguments);
60 
61  $element = $arguments['element'];
62 
63  $property = null;
64  if (!empty($arguments['property'])) {
65  $property = $arguments['property'];
66  } elseif (!empty($arguments['renderingOptionProperty'])) {
67  $property = $arguments['renderingOptionProperty'];
68  }
69 
70  if (empty($property)) {
71  $propertyParts = [];
72  } elseif (is_array($property)) {
73  $propertyParts = $property;
74  } else {
75  $propertyParts = [$property];
76  }
77 
79  $formRuntime = $renderingContext
80  ->getViewHelperVariableContainer()
81  ->get(RenderRenderableViewHelper::class, 'formRuntime');
82 
83  return ‪TranslationService::getInstance()->‪translateFormElementValue($element, $propertyParts, $formRuntime);
84  }
85 
89  protected static function ‪assertArgumentTypes(array $arguments)
90  {
91  foreach (['property', 'renderingOptionProperty'] as $argumentName) {
92  if (
93  !isset($arguments[$argumentName])
94  || is_string($arguments[$argumentName])
95  || is_array($arguments[$argumentName])
96  ) {
97  continue;
98  }
99  throw new Exception(
100  sprintf(
101  'Arguments "%s" either must be string or array',
102  $argumentName
103  ),
104  1504871830
105  );
106  }
107  }
108 }
‪TYPO3\CMS\Form\Service\TranslationService\translateFormElementValue
‪string array translateFormElementValue(RootRenderableInterface $element, array $propertyParts, FormRuntime $formRuntime)
Definition: TranslationService.php:313
‪TYPO3\CMS\Form\Service\TranslationService\getInstance
‪static TranslationService getInstance()
Definition: TranslationService.php:83
‪TYPO3\CMS\Form\Service\TranslationService
Definition: TranslationService.php:44
‪TYPO3\CMS\Form\ViewHelpers\TranslateElementPropertyViewHelper
Definition: TranslateElementPropertyViewHelper.php:34
‪TYPO3\CMS\Form\ViewHelpers\TranslateElementPropertyViewHelper\renderStatic
‪static string array renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: TranslateElementPropertyViewHelper.php:56
‪TYPO3\CMS\Form\ViewHelpers\TranslateElementPropertyViewHelper\initializeArguments
‪initializeArguments()
Definition: TranslateElementPropertyViewHelper.php:41
‪TYPO3\CMS\Form\Domain\Model\Renderable\RootRenderableInterface
Definition: RootRenderableInterface.php:31
‪TYPO3\CMS\Form\ViewHelpers\TranslateElementPropertyViewHelper\assertArgumentTypes
‪static assertArgumentTypes(array $arguments)
Definition: TranslateElementPropertyViewHelper.php:88
‪TYPO3\CMS\Form\ViewHelpers
‪TYPO3\CMS\Form\Domain\Runtime\FormRuntime
Definition: FormSession.php:18