[FOLLOWUP][TASK] EXT:form - change signal slots to hooks
[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 * @return void
50 * @internal
51 */
52 public function initializeArguments()
53 {
54 parent::initializeArguments();
55 $this->registerArgument('renderable', RootRenderableInterface::class, 'A RenderableInterface instance', true);
56 }
57
58 /**
59 * @param array $arguments
60 * @param \Closure $renderChildrenClosure
61 * @param RenderingContextInterface $renderingContext
62 * @return string
63 * @public
64 */
65 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
66 {
67
68 /** @var FormRuntime $formRuntime */
69 $formRuntime = $renderingContext
70 ->getViewHelperVariableContainer()
71 ->get(self::class, 'formRuntime');
72
73 GeneralUtility::deprecationLog('EXT:form - calls for "beforeRendering" are deprecated since TYPO3 v8 and will be removed in TYPO3 v9');
74 $arguments['renderable']->beforeRendering($formRuntime);
75
76 if (
77 isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['beforeRendering'])
78 && is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['beforeRendering'])
79 ) {
80 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['beforeRendering'] as $className) {
81 $hookObj = GeneralUtility::makeInstance($className);
82 if (method_exists($hookObj, 'beforeRendering')) {
83 $hookObj->beforeRendering(
84 $formRuntime,
85 $arguments['renderable']
86 );
87 }
88 }
89 }
90
91 $content = $renderChildrenClosure();
92
93 // Wrap every renderable with a span with a identifier path data attribute if previewMode is active
94 if (!empty($content)) {
95 $renderingOptions = $formRuntime->getRenderingOptions();
96 if (isset($renderingOptions['previewMode']) && $renderingOptions['previewMode'] === true) {
97 $renderable = $arguments['renderable'];
98 $path = $renderable->getIdentifier();
99 if ($renderable instanceof RenderableInterface) {
100 while ($renderable = $renderable->getParentRenderable()) {
101 $path = $renderable->getIdentifier() . '/' . $path;
102 }
103 }
104 $content = '<span data-element-identifier-path="' . $path . '">' . $content . '</span>';
105 }
106 }
107 return $content;
108 }
109 }