‪TYPO3CMS  ‪main
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 
24 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
25 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
26 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
27 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
28 
34 final class ‪TranslateElementPropertyViewHelper extends AbstractViewHelper
35 {
36  use CompileWithRenderStatic;
37 
38  public function ‪initializeArguments(): void
39  {
40  $this->registerArgument('element', RootRenderableInterface::class, 'Form Element to translate', true);
41  $this->registerArgument('property', 'mixed', 'Property to translate', false);
42  $this->registerArgument('renderingOptionProperty', 'mixed', 'Property to translate', false);
43  }
44 
50  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
51  {
52  ‪self::assertArgumentTypes($arguments);
53 
54  $element = $arguments['element'];
55 
56  $property = null;
57  if (!empty($arguments['property'])) {
58  $property = $arguments['property'];
59  } elseif (!empty($arguments['renderingOptionProperty'])) {
60  $property = $arguments['renderingOptionProperty'];
61  }
62 
63  if (empty($property)) {
64  $propertyParts = [];
65  } elseif (is_array($property)) {
66  $propertyParts = $property;
67  } else {
68  $propertyParts = [$property];
69  }
70 
72  $formRuntime = $renderingContext
73  ->getViewHelperVariableContainer()
74  ->get(RenderRenderableViewHelper::class, 'formRuntime');
75 
76  return GeneralUtility::makeInstance(TranslationService::class)->translateFormElementValue($element, $propertyParts, $formRuntime);
77  }
78 
79  protected static function ‪assertArgumentTypes(array $arguments)
80  {
81  foreach (['property', 'renderingOptionProperty'] as $argumentName) {
82  if (
83  !isset($arguments[$argumentName])
84  || is_string($arguments[$argumentName])
85  || is_array($arguments[$argumentName])
86  ) {
87  continue;
88  }
89  throw new Exception(
90  sprintf(
91  'Arguments "%s" either must be string or array',
92  $argumentName
93  ),
94  1504871830
95  );
96  }
97  }
98 }
‪TYPO3\CMS\Form\Service\TranslationService
Definition: TranslationService.php:45
‪TYPO3\CMS\Form\ViewHelpers\TranslateElementPropertyViewHelper
Definition: TranslateElementPropertyViewHelper.php:35
‪TYPO3\CMS\Form\ViewHelpers\TranslateElementPropertyViewHelper\renderStatic
‪static string array renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: TranslateElementPropertyViewHelper.php:49
‪TYPO3\CMS\Form\ViewHelpers\TranslateElementPropertyViewHelper\initializeArguments
‪initializeArguments()
Definition: TranslateElementPropertyViewHelper.php:37
‪TYPO3\CMS\Form\Domain\Model\Renderable\RootRenderableInterface
Definition: RootRenderableInterface.php:31
‪TYPO3\CMS\Form\ViewHelpers\TranslateElementPropertyViewHelper\assertArgumentTypes
‪static assertArgumentTypes(array $arguments)
Definition: TranslateElementPropertyViewHelper.php:78
‪TYPO3\CMS\Form\ViewHelpers
‪TYPO3\CMS\Form\Domain\Runtime\FormRuntime
Definition: FormSession.php:18
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52