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