‪TYPO3CMS  10.4
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 
33 
40 {
41 
51  public function ‪build(array $configuration, string $prototypeName = null): ‪FormDefinition
52  {
53  if (empty($prototypeName)) {
54  $prototypeName = $configuration['prototypeName'] ?? 'standard';
55  }
56  $persistenceIdentifier = $configuration['persistenceIdentifier'] ?? null;
57 
58  if ($configuration['invalid'] === true) {
59  throw new ‪RenderingException($configuration['label'], 1529710560);
60  }
61 
62  $prototypeConfiguration = GeneralUtility::makeInstance(ObjectManager::class)
63  ->get(ConfigurationService::class)
64  ->getPrototypeConfiguration($prototypeName);
65 
66  $form = GeneralUtility::makeInstance(ObjectManager::class)->get(
67  FormDefinition::class,
68  $configuration['identifier'],
69  $prototypeConfiguration,
70  'Form',
71  $persistenceIdentifier
72  );
73  if (isset($configuration['renderables'])) {
74  foreach ($configuration['renderables'] as $pageConfiguration) {
75  $this->‪addNestedRenderable($pageConfiguration, $form);
76  }
77  }
78 
79  unset($configuration['persistenceIdentifier']);
80  unset($configuration['prototypeName']);
81  unset($configuration['renderables']);
82  unset($configuration['type']);
83  unset($configuration['identifier']);
84  $form->setOptions($configuration);
85 
86  $this->‪triggerFormBuildingFinished($form);
87 
88  return $form;
89  }
90 
100  protected function ‪addNestedRenderable(array $nestedRenderableConfiguration, ‪CompositeRenderableInterface $parentRenderable)
101  {
102  if (!isset($nestedRenderableConfiguration['identifier'])) {
103  throw new ‪IdentifierNotValidException('Identifier not set.', 1329289436);
104  }
105  if ($parentRenderable instanceof ‪FormDefinition) {
106  $renderable = $parentRenderable->createPage($nestedRenderableConfiguration['identifier'], $nestedRenderableConfiguration['type']);
107  } elseif ($parentRenderable instanceof ‪AbstractSection) {
108  $renderable = $parentRenderable->createElement($nestedRenderableConfiguration['identifier'], $nestedRenderableConfiguration['type']);
109  } else {
110  throw new ‪UnknownCompositRenderableException('Unknown composit renderable "' . get_class($parentRenderable) . '"', 1479593622);
111  }
112 
113  if (isset($nestedRenderableConfiguration['renderables']) && is_array($nestedRenderableConfiguration['renderables'])) {
114  $childRenderables = $nestedRenderableConfiguration['renderables'];
115  } else {
116  $childRenderables = [];
117  }
118 
119  unset($nestedRenderableConfiguration['type']);
120  unset($nestedRenderableConfiguration['identifier']);
121  unset($nestedRenderableConfiguration['renderables']);
122 
123  $renderable->setOptions($nestedRenderableConfiguration);
124 
125  if ($renderable instanceof ‪CompositeRenderableInterface) {
126  foreach ($childRenderables as $elementConfiguration) {
127  $this->‪addNestedRenderable($elementConfiguration, $renderable);
128  }
129  }
130 
131  return $renderable;
132  }
133 }
‪TYPO3\CMS\Form\Domain\Factory\ArrayFormFactory
Definition: ArrayFormFactory.php:40
‪TYPO3\CMS\Form\Domain\Factory\ArrayFormFactory\addNestedRenderable
‪mixed addNestedRenderable(array $nestedRenderableConfiguration, CompositeRenderableInterface $parentRenderable)
Definition: ArrayFormFactory.php:100
‪TYPO3\CMS\Form\Domain\Exception\IdentifierNotValidException
Definition: IdentifierNotValidException.php:31
‪TYPO3\CMS\Form\Domain\Factory\AbstractFormFactory
Definition: AbstractFormFactory.php:56
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractSection
Definition: AbstractSection.php:44
‪TYPO3\CMS\Form\Domain\Factory
Definition: AbstractFormFactory.php:22
‪TYPO3\CMS\Form\Domain\Factory\ArrayFormFactory\build
‪FormDefinition build(array $configuration, string $prototypeName=null)
Definition: ArrayFormFactory.php:51
‪TYPO3\CMS\Form\Domain\Configuration\ConfigurationService
Definition: ConfigurationService.php:51
‪TYPO3\CMS\Form\Domain\Model\FormDefinition
Definition: FormDefinition.php:223
‪TYPO3\CMS\Form\Domain\Exception\UnknownCompositRenderableException
Definition: UnknownCompositRenderableException.php:31
‪TYPO3\CMS\Form\Domain\Exception\RenderingException
Definition: RenderingException.php:30
‪TYPO3\CMS\Form\Domain\Model\Renderable\CompositeRenderableInterface
Definition: CompositeRenderableInterface.php:32
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Extbase\Object\ObjectManager
Definition: ObjectManager.php:28
‪TYPO3\CMS\Form\Domain\Factory\AbstractFormFactory\triggerFormBuildingFinished
‪triggerFormBuildingFinished(FormDefinition $form)
Definition: AbstractFormFactory.php:63