TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
AbstractCompositeRenderable.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Form\Domain\Model\Renderable;
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 
19 
28 {
29 
35  protected $renderables = [];
36 
48  protected function addRenderable(RenderableInterface $renderable)
49  {
50  if ($renderable->getParentRenderable() !== null) {
51  throw new FormDefinitionConsistencyException(sprintf('The renderable with identifier "%s" is already added to another element (element identifier: "%s").', $renderable->getIdentifier(), $renderable->getParentRenderable()->getIdentifier()), 1325665144);
52  }
53  $renderable->setIndex(count($this->renderables));
54  $renderable->setParentRenderable($this);
55  $this->renderables[] = $renderable;
56  }
57 
70  protected function moveRenderableBefore(RenderableInterface $renderableToMove, RenderableInterface $referenceRenderable)
71  {
72  if ($renderableToMove->getParentRenderable() !== $referenceRenderable->getParentRenderable() || $renderableToMove->getParentRenderable() !== $this) {
73  throw new FormDefinitionConsistencyException('Moved renderables need to be part of the same parent element.', 1326089744);
74  }
75 
76  $reorderedRenderables = [];
77  $i = 0;
78  foreach ($this->renderables as $renderable) {
79  if ($renderable === $renderableToMove) {
80  continue;
81  }
82 
83  if ($renderable === $referenceRenderable) {
84  $reorderedRenderables[] = $renderableToMove;
85  $renderableToMove->setIndex($i);
86  $i++;
87  }
88  $reorderedRenderables[] = $renderable;
89  $renderable->setIndex($i);
90  $i++;
91  }
92  $this->renderables = $reorderedRenderables;
93  }
94 
107  protected function moveRenderableAfter(RenderableInterface $renderableToMove, RenderableInterface $referenceRenderable)
108  {
109  if ($renderableToMove->getParentRenderable() !== $referenceRenderable->getParentRenderable() || $renderableToMove->getParentRenderable() !== $this) {
110  throw new FormDefinitionConsistencyException('Moved renderables need to be part of the same parent element.', 1477083145);
111  }
112 
113  $reorderedRenderables = [];
114  $i = 0;
115  foreach ($this->renderables as $renderable) {
116  if ($renderable === $renderableToMove) {
117  continue;
118  }
119 
120  $reorderedRenderables[] = $renderable;
121  $renderable->setIndex($i);
122  $i++;
123 
124  if ($renderable === $referenceRenderable) {
125  $reorderedRenderables[] = $renderableToMove;
126  $renderableToMove->setIndex($i);
127  $i++;
128  }
129  }
130  $this->renderables = $reorderedRenderables;
131  }
132 
139  public function getRenderablesRecursively(): array
140  {
141  $renderables = [];
142  foreach ($this->renderables as $renderable) {
143  $renderables[] = $renderable;
144  if ($renderable instanceof CompositeRenderableInterface) {
145  $renderables = array_merge($renderables, $renderable->getRenderablesRecursively());
146  }
147  }
148  return $renderables;
149  }
150 
162  protected function removeRenderable(RenderableInterface $renderableToRemove)
163  {
164  if ($renderableToRemove->getParentRenderable() !== $this) {
165  throw new FormDefinitionConsistencyException('The renderable to be removed must be part of the calling parent renderable.', 1326090127);
166  }
167 
168  $updatedRenderables = [];
169  foreach ($this->renderables as $renderable) {
170  if ($renderable === $renderableToRemove) {
171  continue;
172  }
173 
174  $updatedRenderables[] = $renderable;
175  }
176  $this->renderables = $updatedRenderables;
177 
178  $renderableToRemove->onRemoveFromParentRenderable();
179  }
180 
187  public function registerInFormIfPossible()
188  {
189  parent::registerInFormIfPossible();
190  foreach ($this->renderables as $renderable) {
191  $renderable->registerInFormIfPossible();
192  }
193  }
194 
204  {
205  foreach ($this->renderables as $renderable) {
206  $renderable->onRemoveFromParentRenderable();
207  }
208  parent::onRemoveFromParentRenderable();
209  }
210 }
moveRenderableBefore(RenderableInterface $renderableToMove, RenderableInterface $referenceRenderable)
setParentRenderable(CompositeRenderableInterface $renderable)
moveRenderableAfter(RenderableInterface $renderableToMove, RenderableInterface $referenceRenderable)