Revert "[FEATURE] EXT:form - introduce YAML "imports""
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / ViewHelpers / RenderViewHelper.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\ArrayUtility;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Extbase\Mvc\Web\Response;
23 use TYPO3\CMS\Extbase\Object\ObjectManager;
24 use TYPO3\CMS\Form\Domain\Factory\ArrayFormFactory;
25 use TYPO3\CMS\Form\Mvc\Persistence\FormPersistenceManagerInterface;
26 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
27 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
28 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
29
30 /**
31 * Main Entry Point to render a Form into a Fluid Template
32 *
33 * Usage
34 * =====
35 *
36 * <pre>
37 * {namespace formvh=TYPO3\CMS\Form\ViewHelpers}
38 * <formvh:render factoryClass="NameOfYourCustomFactoryClass" />
39 * </pre>
40 *
41 * The factory class must implement {@link TYPO3\CMS\Form\Domain\Factory\FormFactoryInterface}.
42 *
43 * Scope: frontend
44 * @api
45 */
46 class RenderViewHelper extends AbstractViewHelper
47 {
48 use CompileWithRenderStatic;
49
50 /**
51 * @var bool
52 */
53 protected $escapeOutput = false;
54
55 /**
56 * Initialize the arguments.
57 *
58 * @internal
59 */
60 public function initializeArguments()
61 {
62 $this->registerArgument('persistenceIdentifier', 'string', 'The persistence identifier for the form.', false, null);
63 $this->registerArgument('factoryClass', 'string', 'The fully qualified class name of the factory', false, ArrayFormFactory::class);
64 $this->registerArgument('prototypeName', 'string', 'Name of the prototype to use', false, null);
65 $this->registerArgument('overrideConfiguration', 'array', 'factory specific configuration', false, []);
66 }
67
68 /**
69 * @param array $arguments
70 * @param \Closure $renderChildrenClosure
71 * @param RenderingContextInterface $renderingContext
72 * @return string
73 */
74 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
75 {
76 $persistenceIdentifier = $arguments['persistenceIdentifier'];
77 $factoryClass = $arguments['factoryClass'];
78 $prototypeName = $arguments['prototypeName'];
79 $overrideConfiguration = $arguments['overrideConfiguration'];
80
81 $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
82 if (!empty($persistenceIdentifier)) {
83 $formPersistenceManager = $objectManager->get(FormPersistenceManagerInterface::class);
84 $formConfiguration = $formPersistenceManager->load($persistenceIdentifier);
85 ArrayUtility::mergeRecursiveWithOverrule(
86 $formConfiguration,
87 $overrideConfiguration
88 );
89 $overrideConfiguration = $formConfiguration;
90 $overrideConfiguration['persistenceIdentifier'] = $persistenceIdentifier;
91 }
92
93 if (empty($prototypeName)) {
94 $prototypeName = $overrideConfiguration['prototypeName'] ?? 'standard';
95 }
96
97 $factory = $objectManager->get($factoryClass);
98 $formDefinition = $factory->build($overrideConfiguration, $prototypeName);
99 $response = $objectManager->get(Response::class, $renderingContext->getControllerContext()->getResponse());
100 $form = $formDefinition->bind($renderingContext->getControllerContext()->getRequest(), $response);
101 return $form->render();
102 }
103 }