‪TYPO3CMS  10.4
AbstractSection.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 
31 
44 {
45 
53  public function ‪__construct(string ‪$identifier, string ‪$type)
54  {
55  if (!is_string(‪$identifier) || strlen(‪$identifier) === 0) {
56  throw new ‪IdentifierNotValidException('The given identifier was not a string or the string was empty.', 1477082501);
57  }
58 
59  $this->identifier = ‪$identifier;
60  $this->type = ‪$type;
61  }
62 
68  public function ‪getElements(): array
69  {
70  return ‪$this->renderables;
71  }
72 
78  public function ‪getElementsRecursively(): array
79  {
80  return $this->‪getRenderablesRecursively();
81  }
82 
89  public function ‪addElement(‪FormElementInterface $formElement)
90  {
91  $this->‪addRenderable($formElement);
92  }
93 
109  public function ‪createElement(string ‪$identifier, string $typeName): ‪FormElementInterface
110  {
111  $formDefinition = $this->‪getRootForm();
112 
113  $typeDefinitions = $formDefinition->getTypeDefinitions();
114  if (isset($typeDefinitions[$typeName])) {
115  $typeDefinition = $typeDefinitions[$typeName];
116  } else {
117  ‪$renderingOptions = $formDefinition->getRenderingOptions();
118  $skipUnknownElements = isset(‪$renderingOptions['skipUnknownElements']) && ‪$renderingOptions['skipUnknownElements'] === true;
119  if (!$skipUnknownElements) {
120  throw new ‪TypeDefinitionNotFoundException(sprintf('Type "%s" not found. Probably some configuration is missing.', $typeName), 1382364019);
121  }
122 
123  $element = GeneralUtility::makeInstance(ObjectManager::class)
124  ->get(UnknownFormElement::class, ‪$identifier, $typeName);
125  $this->‪addElement($element);
126  return $element;
127  }
128 
129  if (!isset($typeDefinition['implementationClassName'])) {
130  throw new ‪TypeDefinitionNotFoundException(sprintf('The "implementationClassName" was not set in type definition "%s".', $typeName), 1325689855);
131  }
132 
133  $implementationClassName = $typeDefinition['implementationClassName'];
134  $element = GeneralUtility::makeInstance(ObjectManager::class)
135  ->get($implementationClassName, ‪$identifier, $typeName);
136  if (!$element instanceof ‪FormElementInterface) {
137  throw new ‪TypeDefinitionNotValidException(sprintf('The "implementationClassName" for element "%s" ("%s") does not implement the FormElementInterface.', ‪$identifier, $implementationClassName), 1327318156);
138  }
139  unset($typeDefinition['implementationClassName']);
140 
141  $this->‪addElement($element);
142  $element->setOptions($typeDefinition);
143 
144  $element->initializeFormElement();
145  return $element;
146  }
147 
156  public function ‪moveElementBefore(‪FormElementInterface $elementToMove, ‪FormElementInterface $referenceElement)
157  {
158  $this->‪moveRenderableBefore($elementToMove, $referenceElement);
159  }
160 
169  public function ‪moveElementAfter(‪FormElementInterface $elementToMove, ‪FormElementInterface $referenceElement)
170  {
171  $this->‪moveRenderableAfter($elementToMove, $referenceElement);
172  }
173 
179  public function ‪removeElement(‪FormElementInterface $elementToRemove)
180  {
181  $this->‪removeRenderable($elementToRemove);
182  }
183 }
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractSection\getElementsRecursively
‪FormElementInterface[] getElementsRecursively()
Definition: AbstractSection.php:78
‪TYPO3\CMS\Form\Domain\Model\Exception\FormDefinitionConsistencyException
Definition: FormDefinitionConsistencyException.php:27
‪TYPO3\CMS\Form\Domain\Exception\TypeDefinitionNotFoundException
Definition: TypeDefinitionNotFoundException.php:31
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable
Definition: AbstractCompositeRenderable.php:34
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable\moveRenderableAfter
‪moveRenderableAfter(RenderableInterface $renderableToMove, RenderableInterface $referenceRenderable)
Definition: AbstractCompositeRenderable.php:109
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractSection\addElement
‪addElement(FormElementInterface $formElement)
Definition: AbstractSection.php:89
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable\removeRenderable
‪removeRenderable(RenderableInterface $renderableToRemove)
Definition: AbstractCompositeRenderable.php:163
‪TYPO3\CMS\Form\Domain\Exception\IdentifierNotValidException
Definition: IdentifierNotValidException.php:31
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable\moveRenderableBefore
‪moveRenderableBefore(RenderableInterface $renderableToMove, RenderableInterface $referenceRenderable)
Definition: AbstractCompositeRenderable.php:73
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable\getRenderablesRecursively
‪RenderableInterface[] getRenderablesRecursively()
Definition: AbstractCompositeRenderable.php:141
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractSection\moveElementBefore
‪moveElementBefore(FormElementInterface $elementToMove, FormElementInterface $referenceElement)
Definition: AbstractSection.php:156
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$type
‪string $type
Definition: AbstractRenderable.php:50
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$renderingOptions
‪array $renderingOptions
Definition: AbstractRenderable.php:74
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\getRootForm
‪FormDefinition getRootForm()
Definition: AbstractRenderable.php:318
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractSection
Definition: AbstractSection.php:44
‪TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface
Definition: FormElementInterface.php:40
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable\$renderables
‪TYPO3 CMS Form Domain Model Renderable RenderableInterface[] $renderables
Definition: AbstractCompositeRenderable.php:40
‪TYPO3\CMS\Form\Domain\Exception\TypeDefinitionNotValidException
Definition: TypeDefinitionNotValidException.php:31
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractSection\createElement
‪FormElementInterface createElement(string $identifier, string $typeName)
Definition: AbstractSection.php:109
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractSection\removeElement
‪removeElement(FormElementInterface $elementToRemove)
Definition: AbstractSection.php:179
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$identifier
‪string $identifier
Definition: AbstractRenderable.php:56
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractSection\getElements
‪FormElementInterface[] getElements()
Definition: AbstractSection.php:68
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable\addRenderable
‪addRenderable(RenderableInterface $renderable)
Definition: AbstractCompositeRenderable.php:52
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractSection\moveElementAfter
‪moveElementAfter(FormElementInterface $elementToMove, FormElementInterface $referenceElement)
Definition: AbstractSection.php:169
‪TYPO3\CMS\Extbase\Object\ObjectManager
Definition: ObjectManager.php:28
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractSection\__construct
‪__construct(string $identifier, string $type)
Definition: AbstractSection.php:53
‪TYPO3\CMS\Form\Domain\Model\FormElements
Definition: AbstractFormElement.php:22