‪TYPO3CMS  ‪main
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 
30 
43 {
51  public function ‪__construct(string ‪$identifier, string ‪$type)
52  {
53  if (!is_string(‪$identifier) || strlen(‪$identifier) === 0) {
54  throw new ‪IdentifierNotValidException('The given identifier was not a string or the string was empty.', 1477082501);
55  }
56 
57  $this->identifier = ‪$identifier;
58  $this->type = ‪$type;
59  }
60 
66  public function ‪getElements(): array
67  {
68  return ‪$this->renderables;
69  }
70 
76  public function ‪getElementsRecursively(): array
77  {
78  return $this->‪getRenderablesRecursively();
79  }
80 
87  public function ‪addElement(‪FormElementInterface $formElement)
88  {
89  $this->‪addRenderable($formElement);
90  }
91 
107  public function ‪createElement(string ‪$identifier, string $typeName): ‪FormElementInterface
108  {
109  $formDefinition = $this->‪getRootForm();
110 
111  $typeDefinitions = $formDefinition->getTypeDefinitions();
112  if (isset($typeDefinitions[$typeName])) {
113  $typeDefinition = $typeDefinitions[$typeName];
114  } else {
115  ‪$renderingOptions = $formDefinition->getRenderingOptions();
116  $skipUnknownElements = isset(‪$renderingOptions['skipUnknownElements']) && ‪$renderingOptions['skipUnknownElements'] === true;
117  if (!$skipUnknownElements) {
118  throw new ‪TypeDefinitionNotFoundException(sprintf('Type "%s" not found. Probably some configuration is missing.', $typeName), 1382364019);
119  }
120 
121  $element = GeneralUtility::makeInstance(UnknownFormElement::class, ‪$identifier, $typeName);
122  $this->‪addElement($element);
123  return $element;
124  }
125 
126  if (!isset($typeDefinition['implementationClassName'])) {
127  throw new ‪TypeDefinitionNotFoundException(sprintf('The "implementationClassName" was not set in type definition "%s".', $typeName), 1325689855);
128  }
129 
130  $implementationClassName = $typeDefinition['implementationClassName'];
131  $element = GeneralUtility::makeInstance($implementationClassName, ‪$identifier, $typeName);
132  if (!$element instanceof ‪FormElementInterface) {
133  throw new ‪TypeDefinitionNotValidException(sprintf('The "implementationClassName" for element "%s" ("%s") does not implement the FormElementInterface.', ‪$identifier, $implementationClassName), 1327318156);
134  }
135  unset($typeDefinition['implementationClassName']);
136 
137  $this->‪addElement($element);
138  $element->setOptions($typeDefinition);
139 
140  $element->initializeFormElement();
141  return $element;
142  }
143 
152  public function ‪moveElementBefore(‪FormElementInterface $elementToMove, ‪FormElementInterface $referenceElement)
153  {
154  $this->‪moveRenderableBefore($elementToMove, $referenceElement);
155  }
156 
165  public function ‪moveElementAfter(‪FormElementInterface $elementToMove, ‪FormElementInterface $referenceElement)
166  {
167  $this->‪moveRenderableAfter($elementToMove, $referenceElement);
168  }
169 
173  public function ‪removeElement(‪FormElementInterface $elementToRemove)
174  {
175  $this->‪removeRenderable($elementToRemove);
176  }
177 }
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractSection\getElementsRecursively
‪FormElementInterface[] getElementsRecursively()
Definition: AbstractSection.php:76
‪TYPO3\CMS\Form\Domain\Model\Exception\FormDefinitionConsistencyException
Definition: FormDefinitionConsistencyException.php:26
‪TYPO3\CMS\Form\Domain\Exception\TypeDefinitionNotFoundException
Definition: TypeDefinitionNotFoundException.php:30
‪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:108
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractSection\addElement
‪addElement(FormElementInterface $formElement)
Definition: AbstractSection.php:87
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable\removeRenderable
‪removeRenderable(RenderableInterface $renderableToRemove)
Definition: AbstractCompositeRenderable.php:162
‪TYPO3\CMS\Form\Domain\Exception\IdentifierNotValidException
Definition: IdentifierNotValidException.php:30
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable\moveRenderableBefore
‪moveRenderableBefore(RenderableInterface $renderableToMove, RenderableInterface $referenceRenderable)
Definition: AbstractCompositeRenderable.php:72
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable\getRenderablesRecursively
‪RenderableInterface[] getRenderablesRecursively()
Definition: AbstractCompositeRenderable.php:140
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractSection\moveElementBefore
‪moveElementBefore(FormElementInterface $elementToMove, FormElementInterface $referenceElement)
Definition: AbstractSection.php:152
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$type
‪string $type
Definition: AbstractRenderable.php:49
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$renderingOptions
‪array $renderingOptions
Definition: AbstractRenderable.php:73
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractSection
Definition: AbstractSection.php:43
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\getRootForm
‪getRootForm()
Definition: AbstractRenderable.php:286
‪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:39
‪TYPO3\CMS\Form\Domain\Exception\TypeDefinitionNotValidException
Definition: TypeDefinitionNotValidException.php:30
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractSection\createElement
‪FormElementInterface createElement(string $identifier, string $typeName)
Definition: AbstractSection.php:107
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractSection\removeElement
‪removeElement(FormElementInterface $elementToRemove)
Definition: AbstractSection.php:173
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$identifier
‪string $identifier
Definition: AbstractRenderable.php:55
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractSection\getElements
‪FormElementInterface[] getElements()
Definition: AbstractSection.php:66
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable\addRenderable
‪addRenderable(RenderableInterface $renderable)
Definition: AbstractCompositeRenderable.php:51
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractSection\moveElementAfter
‪moveElementAfter(FormElementInterface $elementToMove, FormElementInterface $referenceElement)
Definition: AbstractSection.php:165
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractSection\__construct
‪__construct(string $identifier, string $type)
Definition: AbstractSection.php:51
‪TYPO3\CMS\Form\Domain\Model\FormElements
Definition: AbstractFormElement.php:22