05237f3f6681ad30d9df0992820b39faf4c8367f
[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\Fluid\Core\ViewHelper\AbstractViewHelper;
25 use TYPO3\CMS\Form\Domain\Factory\ArrayFormFactory;
26 use TYPO3\CMS\Form\Mvc\Persistence\FormPersistenceManagerInterface;
27 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
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 parent::initializeArguments();
63 $this->registerArgument('persistenceIdentifier', 'string', 'The persistence identifier for the form.', false, null);
64 $this->registerArgument('factoryClass', 'string', 'The fully qualified class name of the factory', false, ArrayFormFactory::class);
65 $this->registerArgument('prototypeName', 'string', 'Name of the prototype to use', false, null);
66 $this->registerArgument('overrideConfiguration', 'array', 'factory specific configuration', false, []);
67 }
68
69 /**
70 * @param array $arguments
71 * @param \Closure $renderChildrenClosure
72 * @param RenderingContextInterface $renderingContext
73 * @return string
74 * @public
75 */
76 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
77 {
78 $persistenceIdentifier = $arguments['persistenceIdentifier'];
79 $factoryClass = $arguments['factoryClass'];
80 $prototypeName = $arguments['prototypeName'];
81 $overrideConfiguration = $arguments['overrideConfiguration'];
82
83 $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
84 if (!empty($persistenceIdentifier)) {
85 $formPersistenceManager = $objectManager->get(FormPersistenceManagerInterface::class);
86 $formConfiguration = $formPersistenceManager->load($persistenceIdentifier);
87 ArrayUtility::mergeRecursiveWithOverrule(
88 $formConfiguration,
89 $overrideConfiguration
90 );
91 $overrideConfiguration = $formConfiguration;
92 $overrideConfiguration['persistenceIdentifier'] = $persistenceIdentifier;
93 }
94
95 if (empty($prototypeName)) {
96 $prototypeName = isset($overrideConfiguration['prototypeName']) ? $overrideConfiguration['prototypeName'] : 'standard';
97 }
98
99 $factory = $objectManager->get($factoryClass);
100 $formDefinition = $factory->build($overrideConfiguration, $prototypeName);
101 $response = $objectManager->get(Response::class, $renderingContext->getControllerContext()->getResponse());
102 $form = $formDefinition->bind($renderingContext->getControllerContext()->getRequest(), $response);
103 return $form->render();
104 }
105 }