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