diff --git a/src/Structures/Tree.php b/src/Structures/Tree.php index 214179be77..dc99bf279d 100644 --- a/src/Structures/Tree.php +++ b/src/Structures/Tree.php @@ -55,9 +55,27 @@ public function tree($tree = null) return $this->structure()->validateTree($tree, $this->locale()); }); }) + ->setter(function ($tree) { + return $this->removeNullItems($tree); + }) ->args(func_get_args()); } + protected function removeNullItems($tree) + { + return collect($tree) + ->reject(fn ($item) => is_null($item)) + ->map(function ($item) { + if (isset($item['children'])) { + $item['children'] = $this->removeNullItems($item['children']); + } + + return $item; + }) + ->values() + ->all(); + } + public function root() { if (! $this->structure()->expectsRoot()) { diff --git a/tests/Data/Entries/EntryTest.php b/tests/Data/Entries/EntryTest.php index cb40c9c235..f788585982 100644 --- a/tests/Data/Entries/EntryTest.php +++ b/tests/Data/Entries/EntryTest.php @@ -1185,6 +1185,32 @@ public function it_gets_the_order_from_the_collections_structure() $this->assertEquals(3, $four->order()); } + #[Test] + public function it_gets_the_order_from_the_collections_structure_when_the_tree_contains_a_null_item() + { + $collection = tap(Collection::make('ordered'))->save(); + + $one = tap((new Entry)->locale('en')->id('one')->collection($collection))->save(); + $two = tap((new Entry)->locale('en')->id('two')->collection($collection))->save(); + $three = tap((new Entry)->locale('en')->id('three')->collection($collection))->save(); + + $collection->structureContents([ + 'max_depth' => 3, + ])->save(); + $collection->structure()->in('en')->tree( + [ + ['entry' => 'three'], + null, + ['entry' => 'one'], + ['entry' => 'two'], + ] + )->save(); + + $this->assertEquals(2, $one->order()); + $this->assertEquals(3, $two->order()); + $this->assertEquals(1, $three->order()); + } + #[Test] public function it_gets_the_order_from_the_data_if_not_structured() { diff --git a/tests/Data/Structures/TreeTest.php b/tests/Data/Structures/TreeTest.php index bf51e5025a..625338b9c0 100644 --- a/tests/Data/Structures/TreeTest.php +++ b/tests/Data/Structures/TreeTest.php @@ -426,8 +426,8 @@ public function the_structure_validates_the_tree_when_getting_it_the_first_time( $structure = $this->mock(Structure::class); $structure->shouldReceive('handle')->andReturn('test'); - $firstContents = ['first' => 'time']; - $secondContents = ['second' => 'time']; + $firstContents = [['entry' => 'first']]; + $secondContents = [['entry' => 'second']]; $structure->shouldReceive('validateTree')->with($firstContents, 'the-locale')->once()->andReturn($firstContents); $structure->shouldReceive('validateTree')->with($secondContents, 'the-locale')->once()->andReturn($secondContents); @@ -451,6 +451,40 @@ public function the_structure_validates_the_tree_when_getting_it_the_first_time( $tree->tree(); } + #[Test] + public function it_removes_null_items_when_setting_the_tree() + { + $tree = $this->tree([ + [ + 'id' => 'pages-home', + ], + null, + [ + 'id' => 'pages-about', + 'children' => [ + [ + 'id' => 'pages-board', + ], + null, + ], + ], + ]); + + $this->assertEquals([ + [ + 'id' => 'pages-home', + ], + [ + 'id' => 'pages-about', + 'children' => [ + [ + 'id' => 'pages-board', + ], + ], + ], + ], $tree->tree()); + } + #[Test] public function it_cannot_move_into_root_if_structure_expects_root() {