TYPO3 CMS  TYPO3_8-7
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 
28 
36 {
38 
42  protected $escapeOutput = false;
43 
49  public function initializeArguments()
50  {
51  parent::initializeArguments();
52  $this->registerArgument('renderable', RootRenderableInterface::class, 'A RootRenderableInterface instance', true);
53  $this->registerArgument('as', 'string', 'The name within the template', false, 'formValue');
54  }
55 
65  public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
66  {
67  $renderable = $arguments['renderable'];
68  $as = $arguments['as'];
69 
70  if ($renderable instanceof CompositeRenderableInterface) {
71  $elements = $renderable->getRenderablesRecursively();
72  } else {
73  $elements = [$renderable];
74  }
75 
76  $formRuntime = $renderingContext
77  ->getViewHelperVariableContainer()
78  ->get(RenderRenderableViewHelper::class, 'formRuntime');
79 
80  $output = '';
81  foreach ($elements as $element) {
82  $renderingOptions = $element->getRenderingOptions();
83 
84  if (
85  !$element instanceof FormElementInterface
86  || (
87  isset($renderingOptions['_isCompositeFormElement'])
88  && $renderingOptions['_isCompositeFormElement'] === true
89  )
90  || (
91  isset($renderingOptions['_isHiddenFormElement'])
92  && $renderingOptions['_isHiddenFormElement'] === true
93  )
94  || (
95  isset($renderingOptions['_isReadOnlyFormElement'])
96  && $renderingOptions['_isReadOnlyFormElement'] === true
97  )
98  ) {
99  continue;
100  }
101  $value = $formRuntime[$element->getIdentifier()];
102 
103  $formValue = [
104  'element' => $element,
105  'value' => $value,
106  'processedValue' => self::processElementValue($element, $value, $renderChildrenClosure, $renderingContext),
107  'isMultiValue' => is_array($value) || $value instanceof \Iterator
108  ];
109  $renderingContext->getTemplateVariableContainer()->add($as, $formValue);
110  $output .= $renderChildrenClosure();
111  $renderingContext->getTemplateVariableContainer()->remove($as);
112  }
113  return $output;
114  }
115 
125  public static function processElementValue(
126  FormElementInterface $element,
127  $value,
128  \Closure $renderChildrenClosure,
129  RenderingContextInterface $renderingContext
130  ) {
131  $properties = $element->getProperties();
132  if (isset($properties['options']) && is_array($properties['options'])) {
133  $properties['options'] = TranslateElementPropertyViewHelper::renderStatic(
134  ['element' => $element, 'property' => 'options'],
135  $renderChildrenClosure,
136  $renderingContext
137  );
138  if (is_array($value)) {
139  return self::mapValuesToOptions($value, $properties['options']);
140  }
141  return self::mapValueToOption($value, $properties['options']);
142  }
143  if (is_object($value)) {
144  return self::processObject($element, $value);
145  }
146  return $value;
147  }
148 
157  public static function mapValuesToOptions(array $value, array $options): array
158  {
159  $result = [];
160  foreach ($value as $key) {
161  $result[] = self::mapValueToOption($key, $options);
162  }
163  return $result;
164  }
165 
174  public static function mapValueToOption($value, array $options)
175  {
176  return isset($options[$value]) ? $options[$value] : $value;
177  }
178 
186  public static function processObject(FormElementInterface $element, $object): string
187  {
188  $properties = $element->getProperties();
189  if ($object instanceof \DateTime) {
190  if (isset($properties['dateFormat'])) {
191  $dateFormat = $properties['dateFormat'];
192  if (isset($properties['displayTimeSelector']) && $properties['displayTimeSelector'] === true) {
193  $dateFormat .= ' H:i';
194  }
195  } else {
196  $dateFormat = \DateTime::W3C;
197  }
198  return $object->format($dateFormat);
199  }
200 
201  if ($object instanceof File || $object instanceof FileReference) {
202  if ($object instanceof FileReference) {
203  $object = $object->getOriginalResource();
204  }
205  return $object->getName();
206  }
207 
208  if (method_exists($object, '__toString')) {
209  return (string)$object;
210  }
211  return 'Object [' . get_class($object) . ']';
212  }
213 }
static processElementValue(FormElementInterface $element, $value, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
static processObject(FormElementInterface $element, $object)
static renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)