‪TYPO3CMS  10.4
AbstractCompositeRenderable.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 
25 
34 {
35 
41  protected ‪$renderables = [];
42 
53  protected function ‪addRenderable(‪RenderableInterface $renderable)
54  {
55  if ($renderable->‪getParentRenderable() !== null) {
56  throw new ‪FormDefinitionConsistencyException(sprintf('The renderable with identifier "%s" is already added to another element (element identifier: "%s").', $renderable->‪getIdentifier(), $renderable->‪getParentRenderable()->‪getIdentifier()), 1325665144);
57  }
58  $renderable->‪setIndex(count($this->renderables));
59  $renderable->‪setParentRenderable($this);
60  $this->renderables[] = $renderable;
61  }
62 
74  protected function ‪moveRenderableBefore(‪RenderableInterface $renderableToMove, ‪RenderableInterface $referenceRenderable)
75  {
76  if ($renderableToMove->‪getParentRenderable() !== $referenceRenderable->‪getParentRenderable() || $renderableToMove->‪getParentRenderable() !== $this) {
77  throw new ‪FormDefinitionConsistencyException('Moved renderables need to be part of the same parent element.', 1326089744);
78  }
79 
80  $reorderedRenderables = [];
81  $i = 0;
82  foreach ($this->renderables as $renderable) {
83  if ($renderable === $renderableToMove) {
84  continue;
85  }
86 
87  if ($renderable === $referenceRenderable) {
88  $reorderedRenderables[] = $renderableToMove;
89  $renderableToMove->‪setIndex($i);
90  $i++;
91  }
92  $reorderedRenderables[] = $renderable;
93  $renderable->setIndex($i);
94  $i++;
95  }
96  $this->renderables = $reorderedRenderables;
97  }
98 
110  protected function ‪moveRenderableAfter(‪RenderableInterface $renderableToMove, ‪RenderableInterface $referenceRenderable)
111  {
112  if ($renderableToMove->‪getParentRenderable() !== $referenceRenderable->‪getParentRenderable() || $renderableToMove->‪getParentRenderable() !== $this) {
113  throw new ‪FormDefinitionConsistencyException('Moved renderables need to be part of the same parent element.', 1477083145);
114  }
115 
116  $reorderedRenderables = [];
117  $i = 0;
118  foreach ($this->renderables as $renderable) {
119  if ($renderable === $renderableToMove) {
120  continue;
121  }
122 
123  $reorderedRenderables[] = $renderable;
124  $renderable->setIndex($i);
125  $i++;
126 
127  if ($renderable === $referenceRenderable) {
128  $reorderedRenderables[] = $renderableToMove;
129  $renderableToMove->‪setIndex($i);
130  $i++;
131  }
132  }
133  $this->renderables = $reorderedRenderables;
134  }
135 
142  public function ‪getRenderablesRecursively(): array
143  {
144  ‪$renderables = [];
145  foreach ($this->renderables as $renderable) {
146  ‪$renderables[] = $renderable;
147  if ($renderable instanceof ‪CompositeRenderableInterface) {
148  ‪$renderables = array_merge(‪$renderables, $renderable->getRenderablesRecursively());
149  }
150  }
151  return ‪$renderables;
152  }
153 
164  protected function ‪removeRenderable(‪RenderableInterface $renderableToRemove)
165  {
166  if ($renderableToRemove->‪getParentRenderable() !== $this) {
167  throw new ‪FormDefinitionConsistencyException('The renderable to be removed must be part of the calling parent renderable.', 1326090127);
168  }
169 
170  $updatedRenderables = [];
171  foreach ($this->renderables as $renderable) {
172  if ($renderable === $renderableToRemove) {
173  continue;
174  }
175 
176  $updatedRenderables[] = $renderable;
177  }
178  $this->renderables = $updatedRenderables;
179 
180  $renderableToRemove->‪onRemoveFromParentRenderable();
181  }
182 
188  public function ‪registerInFormIfPossible()
189  {
190  parent::registerInFormIfPossible();
191  foreach ($this->renderables as $renderable) {
192  $renderable->registerInFormIfPossible();
193  }
194  }
195 
203  public function ‪onRemoveFromParentRenderable()
204  {
205  foreach ($this->renderables as $renderable) {
206  $renderable->onRemoveFromParentRenderable();
207  }
208  parent::onRemoveFromParentRenderable();
209  }
210 }
‪TYPO3\CMS\Form\Domain\Model\Exception\FormDefinitionConsistencyException
Definition: FormDefinitionConsistencyException.php:27
‪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\Renderable\RenderableInterface\setParentRenderable
‪setParentRenderable(CompositeRenderableInterface $renderable)
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable\removeRenderable
‪removeRenderable(RenderableInterface $renderableToRemove)
Definition: AbstractCompositeRenderable.php:163
‪TYPO3\CMS\Form\Domain\Model\Renderable
Definition: AbstractCompositeRenderable.php:22
‪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\Renderable\AbstractCompositeRenderable\registerInFormIfPossible
‪registerInFormIfPossible()
Definition: AbstractCompositeRenderable.php:187
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable
Definition: AbstractRenderable.php:42
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable\onRemoveFromParentRenderable
‪onRemoveFromParentRenderable()
Definition: AbstractCompositeRenderable.php:202
‪TYPO3\CMS\Form\Domain\Model\Renderable\RootRenderableInterface\getIdentifier
‪string getIdentifier()
‪TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface
Definition: RenderableInterface.php:32
‪TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface\onRemoveFromParentRenderable
‪onRemoveFromParentRenderable()
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable\$renderables
‪TYPO3 CMS Form Domain Model Renderable RenderableInterface[] $renderables
Definition: AbstractCompositeRenderable.php:40
‪TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface\getParentRenderable
‪CompositeRenderableInterface null getParentRenderable()
‪TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface\setIndex
‪setIndex(int $index)
‪TYPO3\CMS\Form\Domain\Model\Renderable\CompositeRenderableInterface
Definition: CompositeRenderableInterface.php:32
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable\addRenderable
‪addRenderable(RenderableInterface $renderable)
Definition: AbstractCompositeRenderable.php:52