‪TYPO3CMS  10.4
RenderFormValueViewHelper.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 
18 /*
19  * Inspired by and partially taken from the Neos.Form package (www.neos.io)
20  */
21 
23 
29 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
30 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
31 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
32 
38 class ‪RenderFormValueViewHelper extends AbstractViewHelper
39 {
40  use CompileWithRenderStatic;
41 
45  protected ‪$escapeOutput = false;
46 
52  public function ‪initializeArguments()
53  {
54  $this->registerArgument('renderable', RenderableInterface::class, 'A renderable element', true);
55  $this->registerArgument('as', 'string', 'The name within the template', false, 'formValue');
56  }
57 
66  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
67  {
68  $element = $arguments['renderable'];
69 
70  if (!$element instanceof ‪FormElementInterface || !self::isEnabled($element)) {
71  return '';
72  }
73 
74  $renderingOptions = $element->getRenderingOptions();
75 
76  if ($renderingOptions['_isSection'] ?? false) {
77  $data = [
78  'element' => $element,
79  'isSection' => true,
80  ];
81  } elseif ($renderingOptions['_isCompositeFormElement'] ?? false) {
82  return '';
83  } else {
84  $formRuntime = $renderingContext
85  ->getViewHelperVariableContainer()
86  ->get(RenderRenderableViewHelper::class, 'formRuntime');
87  $value = $formRuntime[$element->getIdentifier()];
88  $data = [
89  'element' => $element,
90  'value' => $value,
91  'processedValue' => ‪self::processElementValue($element, $value, $renderChildrenClosure, $renderingContext),
92  'isMultiValue' => is_iterable($value),
93  ];
94  }
95 
96  $as = $arguments['as'];
97  $renderingContext->getVariableProvider()->add($as, $data);
98  ‪$output = $renderChildrenClosure();
99  $renderingContext->getVariableProvider()->remove($as);
100 
101  return ‪$output;
102  }
103 
114  public static function ‪processElementValue(
115  ‪FormElementInterface $element,
116  $value,
117  \Closure $renderChildrenClosure,
118  RenderingContextInterface $renderingContext
119  ) {
120  $properties = $element->‪getProperties();
121  if (isset($properties['options']) && is_array($properties['options'])) {
123  ['element' => $element, 'property' => 'options'],
124  $renderChildrenClosure,
125  $renderingContext
126  );
127  if (is_array($value)) {
128  return ‪self::mapValuesToOptions($value, $properties['options']);
129  }
130  return ‪self::mapValueToOption($value, $properties['options']);
131  }
132  if (is_object($value)) {
133  return ‪self::processObject($element, $value);
134  }
135  return $value;
136  }
137 
147  public static function ‪mapValuesToOptions(array $value, array $options): array
148  {
149  $result = [];
150  foreach ($value as $key) {
151  $result[] = ‪self::mapValueToOption($key, $options);
152  }
153  return $result;
154  }
155 
165  public static function ‪mapValueToOption($value, array $options)
166  {
167  return $options[$value] ?? $value;
168  }
169 
178  public static function ‪processObject(‪FormElementInterface $element, $object): string
179  {
180  $properties = $element->‪getProperties();
181 
182  if ($element instanceof ‪StringableFormElementInterface) {
183  return $element->valueToString($object);
184  }
185 
186  if ($object instanceof \DateTime) {
187  return $object->format(\DateTime::W3C);
188  }
189 
190  if ($object instanceof ‪File || $object instanceof ‪FileReference) {
191  if ($object instanceof ‪FileReference) {
192  $object = $object->getOriginalResource();
193  }
194 
195  return $object->getName();
196  }
197 
198  if (method_exists($object, '__toString')) {
199  return (string)$object;
200  }
201 
202  return 'Object [' . get_class($object) . ']';
203  }
204 
210  public static function ‪isEnabled(‪RenderableInterface $renderable): bool
211  {
212  if (!$renderable->‪isEnabled()) {
213  return false;
214  }
215 
216  while ($renderable = $renderable->‪getParentRenderable()) {
217  if ($renderable instanceof ‪RenderableInterface && !$renderable->‪isEnabled()) {
218  return false;
219  }
220  }
221 
222  return true;
223  }
224 }
‪TYPO3\CMS\Form\ViewHelpers\RenderFormValueViewHelper\initializeArguments
‪initializeArguments()
Definition: RenderFormValueViewHelper.php:50
‪TYPO3\CMS\Form\ViewHelpers\RenderFormValueViewHelper\isEnabled
‪static bool isEnabled(RenderableInterface $renderable)
Definition: RenderFormValueViewHelper.php:208
‪TYPO3\CMS\Form\ViewHelpers\RenderFormValueViewHelper\processElementValue
‪static mixed processElementValue(FormElementInterface $element, $value, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: RenderFormValueViewHelper.php:112
‪TYPO3\CMS\Form\ViewHelpers\RenderFormValueViewHelper
Definition: RenderFormValueViewHelper.php:39
‪TYPO3\CMS\Form\ViewHelpers\RenderFormValueViewHelper\$escapeOutput
‪bool $escapeOutput
Definition: RenderFormValueViewHelper.php:43
‪TYPO3\CMS\Form\ViewHelpers\TranslateElementPropertyViewHelper\renderStatic
‪static string array renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: TranslateElementPropertyViewHelper.php:56
‪TYPO3\CMS\Form\ViewHelpers\RenderFormValueViewHelper\mapValueToOption
‪static mixed mapValueToOption($value, array $options)
Definition: RenderFormValueViewHelper.php:163
‪TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface
Definition: RenderableInterface.php:32
‪TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface
Definition: FormElementInterface.php:40
‪TYPO3\CMS\Form\ViewHelpers\RenderFormValueViewHelper\renderStatic
‪static string renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: RenderFormValueViewHelper.php:64
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Form\ViewHelpers\RenderFormValueViewHelper\processObject
‪static string processObject(FormElementInterface $element, $object)
Definition: RenderFormValueViewHelper.php:176
‪TYPO3\CMS\Form\Domain\Model\FormElements\StringableFormElementInterface
Definition: StringableFormElementInterface.php:30
‪TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface\getParentRenderable
‪CompositeRenderableInterface null getParentRenderable()
‪$output
‪$output
Definition: annotationChecker.php:119
‪TYPO3\CMS\Extbase\Domain\Model\FileReference
Definition: FileReference.php:28
‪TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface\getProperties
‪array getProperties()
‪TYPO3\CMS\Form\ViewHelpers
‪TYPO3\CMS\Form\ViewHelpers\RenderFormValueViewHelper\mapValuesToOptions
‪static array mapValuesToOptions(array $value, array $options)
Definition: RenderFormValueViewHelper.php:145
‪TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface\isEnabled
‪bool isEnabled()