‪TYPO3CMS  9.5
RenderAllFormValuesViewHelper.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 originated from the Neos.Form package (www.neos.io)
9  *
10  * It is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU General Public License, either version 2
12  * of the License, or any later version.
13  *
14  * For the full copyright and license information, please read the
15  * LICENSE.txt file that was distributed with this source code.
16  *
17  * The TYPO3 project - inspiring people to share!
18  */
19 
26 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
27 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
28 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
29 
35 class ‪RenderAllFormValuesViewHelper extends AbstractViewHelper
36 {
37  use CompileWithRenderStatic;
38 
42  protected ‪$escapeOutput = false;
43 
49  public function ‪initializeArguments()
50  {
51  $this->registerArgument('renderable', RootRenderableInterface::class, 'A RootRenderableInterface instance', true);
52  $this->registerArgument('as', 'string', 'The name within the template', false, 'formValue');
53  }
54 
63  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
64  {
65  $renderable = $arguments['renderable'];
66  $as = $arguments['as'];
67 
68  if (!$renderable->isEnabled()) {
69  return '';
70  }
71 
72  if ($renderable instanceof ‪CompositeRenderableInterface) {
73  $elements = $renderable->getRenderablesRecursively();
74  } else {
75  $elements = [$renderable];
76  }
77 
78  $formRuntime = $renderingContext
79  ->getViewHelperVariableContainer()
80  ->get(RenderRenderableViewHelper::class, 'formRuntime');
81 
82  ‪$output = '';
83  foreach ($elements as $element) {
84  $renderingOptions = $element->getRenderingOptions();
85 
86  if (
87  !$element instanceof ‪FormElementInterface
88  || (isset($renderingOptions['_isCompositeFormElement']) && (bool)$renderingOptions['_isCompositeFormElement'] === true)
89  || !$element->‪isEnabled()
90  || self::hasDisabledParent($element)
91  ) {
92  continue;
93  }
94 
95  if (
96  (isset($renderingOptions['_isHiddenFormElement']) && (bool)$renderingOptions['_isHiddenFormElement'] === true)
97  || (isset($renderingOptions['_isReadOnlyFormElement']) && (bool)$renderingOptions['_isReadOnlyFormElement'] === true)
98  ) {
99  trigger_error(
100  'Using the properties "renderingOptions._isHiddenFormElement" and "renderingOptions._isReadOnlyFormElement" will be removed in TYPO3 v10.0. Use variants instead.',
101  E_USER_DEPRECATED
102  );
103  continue;
104  }
105 
106  $value = $formRuntime[$element->getIdentifier()];
107 
108  $formValue = [
109  'element' => $element,
110  'value' => $value,
111  'processedValue' => ‪self::processElementValue($element, $value, $renderChildrenClosure, $renderingContext),
112  'isMultiValue' => is_array($value) || $value instanceof \Iterator
113  ];
114  $renderingContext->getVariableProvider()->add($as, $formValue);
115  ‪$output .= $renderChildrenClosure();
116  $renderingContext->getVariableProvider()->remove($as);
117  }
118  return ‪$output;
119  }
120 
131  public static function ‪processElementValue(
132  ‪FormElementInterface $element,
133  $value,
134  \Closure $renderChildrenClosure,
135  RenderingContextInterface $renderingContext
136  ) {
137  $properties = $element->‪getProperties();
138  if (isset($properties['options']) && is_array($properties['options'])) {
140  ['element' => $element, 'property' => 'options'],
141  $renderChildrenClosure,
142  $renderingContext
143  );
144  if (is_array($value)) {
145  return ‪self::mapValuesToOptions($value, $properties['options']);
146  }
147  return ‪self::mapValueToOption($value, $properties['options']);
148  }
149  if (is_object($value)) {
150  return ‪self::processObject($element, $value);
151  }
152  return $value;
153  }
154 
164  public static function ‪mapValuesToOptions(array $value, array $options): array
165  {
166  $result = [];
167  foreach ($value as $key) {
168  $result[] = ‪self::mapValueToOption($key, $options);
169  }
170  return $result;
171  }
172 
182  public static function ‪mapValueToOption($value, array $options)
183  {
184  return $options[$value] ?? $value;
185  }
186 
195  public static function ‪processObject(‪FormElementInterface $element, $object): string
196  {
197  $properties = $element->‪getProperties();
198  if ($object instanceof \DateTime) {
199  if (
200  $element->‪getType() === 'DatePicker'
201  && isset($properties['dateFormat'])
202  ) {
203  $dateFormat = $properties['dateFormat'];
204  if (isset($properties['displayTimeSelector']) && $properties['displayTimeSelector'] === true) {
205  $dateFormat .= ' H:i';
206  }
207  } elseif ($element->‪getType() === 'Date') {
208  if (isset($properties['displayFormat'])) {
209  $dateFormat = $properties['displayFormat'];
210  } else {
211  $dateFormat = 'Y-m-d';
212  }
213  } else {
214  $dateFormat = \DateTime::W3C;
215  }
216 
217  return $object->format($dateFormat);
218  }
219 
220  if ($object instanceof ‪File || $object instanceof ‪FileReference) {
221  if ($object instanceof ‪FileReference) {
222  $object = $object->getOriginalResource();
223  }
224  return $object->getName();
225  }
226 
227  if (method_exists($object, '__toString')) {
228  return (string)$object;
229  }
230  return 'Object [' . get_class($object) . ']';
231  }
232 
238  public static function ‪hasDisabledParent(‪RenderableInterface $renderable): bool
239  {
240  while ($renderable = $renderable->‪getParentRenderable()) {
241  if ($renderable instanceof ‪RenderableInterface && !$renderable->‪isEnabled()) {
242  return true;
243  }
244  }
245 
246  return false;
247  }
248 }
‪TYPO3\CMS\Form\ViewHelpers\RenderAllFormValuesViewHelper
Definition: RenderAllFormValuesViewHelper.php:36
‪TYPO3\CMS\Form\ViewHelpers\RenderAllFormValuesViewHelper\hasDisabledParent
‪static bool hasDisabledParent(RenderableInterface $renderable)
Definition: RenderAllFormValuesViewHelper.php:236
‪TYPO3\CMS\Form\ViewHelpers\TranslateElementPropertyViewHelper\renderStatic
‪static string array renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: TranslateElementPropertyViewHelper.php:54
‪TYPO3\CMS\Form\ViewHelpers\RenderAllFormValuesViewHelper\renderStatic
‪static string renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: RenderAllFormValuesViewHelper.php:61
‪TYPO3\CMS\Form\ViewHelpers\RenderAllFormValuesViewHelper\mapValueToOption
‪static mixed mapValueToOption($value, array $options)
Definition: RenderAllFormValuesViewHelper.php:180
‪TYPO3\CMS\Form\ViewHelpers\RenderAllFormValuesViewHelper\$escapeOutput
‪bool $escapeOutput
Definition: RenderAllFormValuesViewHelper.php:40
‪TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface
Definition: RenderableInterface.php:28
‪TYPO3\CMS\Form\ViewHelpers\RenderAllFormValuesViewHelper\initializeArguments
‪initializeArguments()
Definition: RenderAllFormValuesViewHelper.php:47
‪TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface
Definition: FormElementInterface.php:36
‪TYPO3\CMS\Form\Domain\Model\Renderable\RootRenderableInterface
Definition: RootRenderableInterface.php:27
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:23
‪TYPO3\CMS\Form\ViewHelpers\RenderAllFormValuesViewHelper\mapValuesToOptions
‪static array mapValuesToOptions(array $value, array $options)
Definition: RenderAllFormValuesViewHelper.php:162
‪TYPO3\CMS\Form\ViewHelpers\RenderAllFormValuesViewHelper\processObject
‪static string processObject(FormElementInterface $element, $object)
Definition: RenderAllFormValuesViewHelper.php:193
‪TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface\getParentRenderable
‪CompositeRenderableInterface null getParentRenderable()
‪$output
‪$output
Definition: annotationChecker.php:113
‪TYPO3\CMS\Extbase\Domain\Model\FileReference
Definition: FileReference.php:25
‪TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface\getProperties
‪array getProperties()
‪TYPO3\CMS\Form\ViewHelpers
‪TYPO3\CMS\Form\ViewHelpers\RenderAllFormValuesViewHelper\processElementValue
‪static mixed processElementValue(FormElementInterface $element, $value, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: RenderAllFormValuesViewHelper.php:129
‪TYPO3\CMS\Form\Domain\Model\Renderable\RootRenderableInterface\getType
‪string getType()
‪TYPO3\CMS\Form\Domain\Model\Renderable\CompositeRenderableInterface
Definition: CompositeRenderableInterface.php:28
‪TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface\isEnabled
‪bool isEnabled()