‪TYPO3CMS  ‪main
ArrayFormFactory.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 /*
19  * Inspired by and partially taken from the Neos.Form package (www.neos.io)
20  */
21 
23 
32 
39 {
46  public function ‪build(array $configuration, string $prototypeName = null): ‪FormDefinition
47  {
48  if (empty($prototypeName)) {
49  $prototypeName = $configuration['prototypeName'] ?? 'standard';
50  }
51  $persistenceIdentifier = $configuration['persistenceIdentifier'] ?? null;
52 
53  if ($configuration['invalid'] ?? false) {
54  throw new ‪RenderingException($configuration['label'], 1529710560);
55  }
56 
57  $prototypeConfiguration = GeneralUtility::makeInstance(ConfigurationService::class)
58  ->getPrototypeConfiguration($prototypeName);
59 
60  $form = GeneralUtility::makeInstance(
61  FormDefinition::class,
62  $configuration['identifier'],
63  $prototypeConfiguration,
64  'Form',
65  $persistenceIdentifier
66  );
67  if (isset($configuration['renderables'])) {
68  foreach ($configuration['renderables'] as $pageConfiguration) {
69  $this->‪addNestedRenderable($pageConfiguration, $form);
70  }
71  }
72 
73  unset($configuration['persistenceIdentifier']);
74  unset($configuration['prototypeName']);
75  unset($configuration['renderables']);
76  unset($configuration['type']);
77  unset($configuration['identifier']);
78  $form->setOptions($configuration);
79 
80  $this->‪triggerFormBuildingFinished($form);
81 
82  return $form;
83  }
84 
92  protected function ‪addNestedRenderable(array $nestedRenderableConfiguration, ‪CompositeRenderableInterface $parentRenderable)
93  {
94  if (!isset($nestedRenderableConfiguration['identifier'])) {
95  throw new ‪IdentifierNotValidException('Identifier not set.', 1329289436);
96  }
97  if ($parentRenderable instanceof ‪FormDefinition) {
98  $renderable = $parentRenderable->createPage($nestedRenderableConfiguration['identifier'], $nestedRenderableConfiguration['type']);
99  } elseif ($parentRenderable instanceof ‪AbstractSection) {
100  $renderable = $parentRenderable->createElement($nestedRenderableConfiguration['identifier'], $nestedRenderableConfiguration['type']);
101  } else {
102  throw new ‪UnknownCompositRenderableException('Unknown composit renderable "' . get_class($parentRenderable) . '"', 1479593622);
103  }
104 
105  if (isset($nestedRenderableConfiguration['renderables']) && is_array($nestedRenderableConfiguration['renderables'])) {
106  $childRenderables = $nestedRenderableConfiguration['renderables'];
107  } else {
108  $childRenderables = [];
109  }
110 
111  unset($nestedRenderableConfiguration['type']);
112  unset($nestedRenderableConfiguration['identifier']);
113  unset($nestedRenderableConfiguration['renderables']);
114 
115  $renderable->setOptions($nestedRenderableConfiguration);
116 
117  if ($renderable instanceof ‪CompositeRenderableInterface) {
118  foreach ($childRenderables as $elementConfiguration) {
119  $this->‪addNestedRenderable($elementConfiguration, $renderable);
120  }
121  }
122 
123  return $renderable;
124  }
125 }
‪TYPO3\CMS\Form\Domain\Factory\ArrayFormFactory
Definition: ArrayFormFactory.php:39
‪TYPO3\CMS\Form\Domain\Factory\ArrayFormFactory\addNestedRenderable
‪mixed addNestedRenderable(array $nestedRenderableConfiguration, CompositeRenderableInterface $parentRenderable)
Definition: ArrayFormFactory.php:92
‪TYPO3\CMS\Form\Domain\Exception\IdentifierNotValidException
Definition: IdentifierNotValidException.php:30
‪TYPO3\CMS\Form\Domain\Factory\AbstractFormFactory
Definition: AbstractFormFactory.php:54
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractSection
Definition: AbstractSection.php:43
‪TYPO3\CMS\Form\Domain\Factory
Definition: AbstractFormFactory.php:22
‪TYPO3\CMS\Form\Domain\Configuration\ConfigurationService
Definition: ConfigurationService.php:50
‪TYPO3\CMS\Form\Domain\Model\FormDefinition
Definition: FormDefinition.php:224
‪TYPO3\CMS\Form\Domain\Factory\ArrayFormFactory\build
‪build(array $configuration, string $prototypeName=null)
Definition: ArrayFormFactory.php:46
‪TYPO3\CMS\Form\Domain\Exception\UnknownCompositRenderableException
Definition: UnknownCompositRenderableException.php:30
‪TYPO3\CMS\Form\Domain\Exception\RenderingException
Definition: RenderingException.php:29
‪TYPO3\CMS\Form\Domain\Model\Renderable\CompositeRenderableInterface
Definition: CompositeRenderableInterface.php:32
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Form\Domain\Factory\AbstractFormFactory\triggerFormBuildingFinished
‪triggerFormBuildingFinished(FormDefinition $form)
Definition: AbstractFormFactory.php:59