bc046c5aaae1ca3db6d5075da88c4b100a30a9a4
[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 * @public
63 */
64 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
65 {
66
67 /** @var FormRuntime $formRuntime */
68 $formRuntime = $renderingContext
69 ->getViewHelperVariableContainer()
70 ->get(self::class, 'formRuntime');
71
72 if (
73 isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['beforeRendering'])
74 && is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['beforeRendering'])
75 ) {
76 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['beforeRendering'] as $className) {
77 $hookObj = GeneralUtility::makeInstance($className);
78 if (method_exists($hookObj, 'beforeRendering')) {
79 $hookObj->beforeRendering(
80 $formRuntime,
81 $arguments['renderable']
82 );
83 }
84 }
85 }
86
87 $content = $renderChildrenClosure();
88
89 // Wrap every renderable with a span with a identifier path data attribute if previewMode is active
90 if (!empty($content)) {
91 $renderingOptions = $formRuntime->getRenderingOptions();
92 if (isset($renderingOptions['previewMode']) && $renderingOptions['previewMode'] === true) {
93 $renderable = $arguments['renderable'];
94 $path = $renderable->getIdentifier();
95 if ($renderable instanceof RenderableInterface) {
96 while ($renderable = $renderable->getParentRenderable()) {
97 $path = $renderable->getIdentifier() . '/' . $path;
98 }
99 }
100 $content = '<span data-element-identifier-path="' . $path . '">' . $content . '</span>';
101 }
102 }
103 return $content;
104 }
105 }