diff --git a/Classes/Core/Model/Renderable/AbstractCompositeRenderable.php b/Classes/Core/Model/Renderable/AbstractCompositeRenderable.php index ada8c24..4154cb0 100644 --- a/Classes/Core/Model/Renderable/AbstractCompositeRenderable.php +++ b/Classes/Core/Model/Renderable/AbstractCompositeRenderable.php @@ -70,7 +70,6 @@ protected function moveRenderableBefore(RenderableInterface $renderableToMove, R } $reorderedRenderables = []; - $i = 0; foreach ($this->renderables as $renderable) { if ($renderable === $renderableToMove) { continue; @@ -78,14 +77,11 @@ protected function moveRenderableBefore(RenderableInterface $renderableToMove, R if ($renderable === $referenceRenderable) { $reorderedRenderables[] = $renderableToMove; - $renderableToMove->setIndex($i); - $i++; } $reorderedRenderables[] = $renderable; - $renderable->setIndex($i); - $i++; } $this->renderables = $reorderedRenderables; + $this->reindexRenderables(); } /** @@ -107,23 +103,19 @@ protected function moveRenderableAfter(RenderableInterface $renderableToMove, Re } $reorderedRenderables = []; - $i = 0; foreach ($this->renderables as $renderable) { if ($renderable === $renderableToMove) { continue; } $reorderedRenderables[] = $renderable; - $renderable->setIndex($i); - $i++; if ($renderable === $referenceRenderable) { $reorderedRenderables[] = $renderableToMove; - $renderableToMove->setIndex($i); - $i++; } } $this->renderables = $reorderedRenderables; + $this->reindexRenderables(); } /** @@ -170,10 +162,20 @@ protected function removeRenderable(RenderableInterface $renderableToRemove) $updatedRenderables[] = $renderable; } $this->renderables = $updatedRenderables; + $this->reindexRenderables(); $renderableToRemove->onRemoveFromParentRenderable(); } + protected function reindexRenderables() + { + $i = 0; + foreach ($this->renderables as $renderable) { + $renderable->setIndex($i); + $i++; + } + } + /** * Register this element at the parent form, if there is a connection to the parent form. * diff --git a/Tests/Unit/Core/Model/FormDefinitionTest.php b/Tests/Unit/Core/Model/FormDefinitionTest.php index f723123..6b95b75 100644 --- a/Tests/Unit/Core/Model/FormDefinitionTest.php +++ b/Tests/Unit/Core/Model/FormDefinitionTest.php @@ -583,12 +583,16 @@ public function removePageRemovesPageFromForm() $formDefinition = new FormDefinition('foo1'); $page1 = new Page('bar1'); $page2 = new Page('bar2'); + $page3 = new Page('bar3'); $formDefinition->addPage($page1); $formDefinition->addPage($page2); + $formDefinition->addPage($page3); - $formDefinition->removePage($page1); - $this->assertNull($page1->getParentRenderable()); - Assert::assertSame([$page2], $formDefinition->getPages()); + $formDefinition->removePage($page2); + $this->assertNull($page2->getParentRenderable()); + Assert::assertSame(0, $page1->getIndex()); + Assert::assertSame(1, $page3->getIndex()); + Assert::assertSame([$page1, $page3], $formDefinition->getPages()); } /**