‪TYPO3CMS  9.5
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 
21 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
23 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
24 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
25 
31 class ‪TranslateElementPropertyViewHelper extends AbstractViewHelper
32 {
33  use CompileWithRenderStatic;
34 
40  public function ‪initializeArguments()
41  {
42  $this->registerArgument('element', RootRenderableInterface::class, 'Form Element to translate', true);
43  $this->registerArgument('property', 'mixed', 'Property to translate', false);
44  $this->registerArgument('renderingOptionProperty', 'mixed', 'Property to translate', false);
45  }
46 
55  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
56  {
57  static::assertArgumentTypes($arguments);
58 
59  $element = $arguments['element'];
60 
61  $property = null;
62  if (!empty($arguments['property'])) {
63  $property = $arguments['property'];
64  } elseif (!empty($arguments['renderingOptionProperty'])) {
65  $property = $arguments['renderingOptionProperty'];
66  }
67 
68  if (empty($property)) {
69  $propertyParts = [];
70  } elseif (is_array($property)) {
71  $propertyParts = $property;
72  } else {
73  $propertyParts = [$property];
74  }
75 
77  $formRuntime = $renderingContext
78  ->getViewHelperVariableContainer()
79  ->get(RenderRenderableViewHelper::class, 'formRuntime');
80 
81  return ‪TranslationService::getInstance()->‪translateFormElementValue($element, $propertyParts, $formRuntime);
82  }
83 
87  protected static function ‪assertArgumentTypes(array $arguments)
88  {
89  foreach (['property', 'renderingOptionProperty'] as $argumentName) {
90  if (
91  !isset($arguments[$argumentName])
92  || is_string($arguments[$argumentName])
93  || is_array($arguments[$argumentName])
94  ) {
95  continue;
96  }
97  throw new Exception(
98  sprintf(
99  'Arguments "%s" either must be string or array',
100  $argumentName
101  ),
102  1504871830
103  );
104  }
105  }
106 }
‪TYPO3\CMS\Form\Service\TranslationService\translateFormElementValue
‪string array translateFormElementValue(RootRenderableInterface $element, array $propertyParts, FormRuntime $formRuntime)
Definition: TranslationService.php:321
‪TYPO3\CMS\Form\Service\TranslationService\getInstance
‪static TranslationService getInstance()
Definition: TranslationService.php:82
‪TYPO3\CMS\Form\Service\TranslationService
Definition: TranslationService.php:43
‪TYPO3\CMS\Form\ViewHelpers\TranslateElementPropertyViewHelper
Definition: TranslateElementPropertyViewHelper.php:32
‪TYPO3\CMS\Form\ViewHelpers\TranslateElementPropertyViewHelper\renderStatic
‪static string array renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: TranslateElementPropertyViewHelper.php:54
‪TYPO3\CMS\Form\ViewHelpers\TranslateElementPropertyViewHelper\initializeArguments
‪initializeArguments()
Definition: TranslateElementPropertyViewHelper.php:39
‪TYPO3\CMS\Form\Domain\Model\Renderable\RootRenderableInterface
Definition: RootRenderableInterface.php:27
‪TYPO3\CMS\Form\ViewHelpers\TranslateElementPropertyViewHelper\assertArgumentTypes
‪static assertArgumentTypes(array $arguments)
Definition: TranslateElementPropertyViewHelper.php:86
‪TYPO3\CMS\Form\ViewHelpers
‪TYPO3\CMS\Form\Domain\Runtime\FormRuntime
Definition: FormSession.php:3