‪TYPO3CMS  ‪main
HtmlViewHelper.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 
20 use Psr\Http\Message\ServerRequestInterface;
26 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
27 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
28 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
29 
119 final class ‪HtmlViewHelper extends AbstractViewHelper
120 {
121  use CompileWithRenderStatic;
122 
128  protected ‪$escapeChildren = false;
129 
135  protected ‪$escapeOutput = false;
136 
137  public function ‪initializeArguments(): void
138  {
139  $this->registerArgument('parseFuncTSPath', 'string', 'Path to the TypoScript parseFunc setup.', false, 'lib.parseFunc_RTE');
140  $this->registerArgument('data', 'mixed', 'Initialize the content object with this set of data. Either an array or object.');
141  $this->registerArgument('current', 'string', 'Initialize the content object with this value for current property.');
142  $this->registerArgument('currentValueKey', 'string', 'Define the value key, used to locate the current value for the content object');
143  $this->registerArgument('table', 'string', 'The table name associated with the "data" argument.', false, '');
144  }
145 
146  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
147  {
148  $parseFuncTSPath = $arguments['parseFuncTSPath'];
149  $data = $arguments['data'];
150  $current = $arguments['current'];
151  $currentValueKey = $arguments['currentValueKey'];
152  $table = $arguments['table'];
153 
155  $request = $renderingContext->getRequest();
156  $isBackendRequest = $request instanceof ServerRequestInterface && ‪ApplicationType::fromRequest($request)->isBackend();
157  if ($isBackendRequest) {
158  throw new \RuntimeException(
159  'Using f:format.html in backend context is not allowed. Use f:sanitize.html or f:transform.html instead.',
160  1686813703
161  );
162  }
163 
164  $value = $renderChildrenClosure() ?? '';
165 
166  // Prepare data array
167  if (is_object($data)) {
168  $data = ObjectAccess::getGettableProperties($data);
169  } elseif (!is_array($data)) {
170  $data = (array)$data;
171  }
172 
173  $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
174  $contentObject->setRequest($request);
175  $contentObject->start($data, $table);
176 
177  if ($current !== null) {
178  $contentObject->setCurrentVal($current);
179  } elseif ($currentValueKey !== null && isset($data[$currentValueKey])) {
180  $contentObject->setCurrentVal($data[$currentValueKey]);
181  }
182 
183  $content = $contentObject->parseFunc($value, null, '< ' . $parseFuncTSPath);
184  return $content;
185  }
186 }
‪TYPO3\CMS\Fluid\ViewHelpers\Format\HtmlViewHelper\initializeArguments
‪initializeArguments()
Definition: HtmlViewHelper.php:134
‪TYPO3\CMS\Fluid\ViewHelpers\Format\HtmlViewHelper
Definition: HtmlViewHelper.php:120
‪TYPO3\CMS\Fluid\ViewHelpers\Format\HtmlViewHelper\$escapeOutput
‪bool $escapeOutput
Definition: HtmlViewHelper.php:132
‪TYPO3\CMS\Fluid\ViewHelpers\Format\HtmlViewHelper\renderStatic
‪static renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: HtmlViewHelper.php:143
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess
Definition: ObjectAccess.php:39
‪TYPO3\CMS\Fluid\ViewHelpers\Format
Definition: AbstractEncodingViewHelper.php:18
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:102
‪TYPO3\CMS\Core\Http\fromRequest
‪@ fromRequest
Definition: ApplicationType.php:66
‪TYPO3\CMS\Fluid\ViewHelpers\Format\HtmlViewHelper\$escapeChildren
‪bool $escapeChildren
Definition: HtmlViewHelper.php:126
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Fluid\Core\Rendering\RenderingContext
Definition: RenderingContext.php:35
‪TYPO3\CMS\Core\Http\ApplicationType
‪ApplicationType
Definition: ApplicationType.php:55