[CLEANUP] Remove/Replace non-standard phpdoc annotations
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / ViewHelpers / RenderRenderableViewHelper.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Form\ViewHelpers;
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
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
22 use TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface;
23 use TYPO3\CMS\Form\Domain\Model\Renderable\RootRenderableInterface;
24 use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
25 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
26 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
27
28 /**
29 * Render a renderable.
30 *
31 * Set the renderable into the \TYPO3\CMS\Form\Mvc\View\FormView
32 * and return the rendered content.
33 *
34 * Scope: frontend
35 * @api
36 */
37 class RenderRenderableViewHelper extends AbstractViewHelper
38 {
39 use CompileWithRenderStatic;
40
41 /**
42 * @var bool
43 */
44 protected $escapeOutput = false;
45
46 /**
47 * Initialize the arguments.
48 *
49 * @internal
50 */
51 public function initializeArguments()
52 {
53 parent::initializeArguments();
54 $this->registerArgument('renderable', RootRenderableInterface::class, 'A RenderableInterface instance', true);
55 }
56
57 /**
58 * @param array $arguments
59 * @param \Closure $renderChildrenClosure
60 * @param RenderingContextInterface $renderingContext
61 * @return string
62 */
63 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
64 {
65
66 /** @var FormRuntime $formRuntime */
67 $formRuntime = $renderingContext
68 ->getViewHelperVariableContainer()
69 ->get(self::class, 'formRuntime');
70
71 if (
72 isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['beforeRendering'])
73 && is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['beforeRendering'])
74 ) {
75 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['beforeRendering'] as $className) {
76 $hookObj = GeneralUtility::makeInstance($className);
77 if (method_exists($hookObj, 'beforeRendering')) {
78 $hookObj->beforeRendering(
79 $formRuntime,
80 $arguments['renderable']
81 );
82 }
83 }
84 }
85
86 $content = $renderChildrenClosure();
87
88 // Wrap every renderable with a span with a identifier path data attribute if previewMode is active
89 if (!empty($content)) {
90 $renderingOptions = $formRuntime->getRenderingOptions();
91 if (isset($renderingOptions['previewMode']) && $renderingOptions['previewMode'] === true) {
92 $renderable = $arguments['renderable'];
93 $path = $renderable->getIdentifier();
94 if ($renderable instanceof RenderableInterface) {
95 while ($renderable = $renderable->getParentRenderable()) {
96 $path = $renderable->getIdentifier() . '/' . $path;
97 }
98 }
99 $content = '<span data-element-identifier-path="' . $path . '">' . $content . '</span>';
100 }
101 }
102 return $content;
103 }
104 }