diff --git a/src/Http/Controllers/CP/Fieldtypes/ReplicatorSetController.php b/src/Http/Controllers/CP/Fieldtypes/ReplicatorSetController.php index 6871fb8dd2..b4f4c56e70 100644 --- a/src/Http/Controllers/CP/Fieldtypes/ReplicatorSetController.php +++ b/src/Http/Controllers/CP/Fieldtypes/ReplicatorSetController.php @@ -11,6 +11,7 @@ use Statamic\Fields\Field; use Statamic\Fields\Fields; use Statamic\Http\Controllers\CP\CpController; +use Statamic\Support\Arr; class ReplicatorSetController extends CpController { @@ -31,9 +32,7 @@ public function __invoke(Request $request) $field = $this->getReplicatorField($blueprint, $request->field); - $replicatorSet = collect($field->get('sets')) - ->flatMap(fn (array $setGroup) => $setGroup['sets'] ?? []) - ->get($request->set); + $replicatorSet = $this->flattenSets($field->get('sets'))[$request->set]; if (! $replicatorSet) { throw new \Exception("Cannot find Replicator set [$request->set]"); @@ -76,9 +75,7 @@ private function getConfig(array $config, array $remainingFieldPathComponents): $isReplicator = isset($config['type']) && in_array($config['type'], ['bard', 'replicator']); if ($isReplicator) { - $flattenedSets = collect($config['sets']) - ->flatMap(fn (array $setGroup): array => $setGroup['sets'] ?? []) - ->all(); + $flattenedSets = $this->flattenSets($config['sets'] ?? []); if (count($remainingFieldPathComponents) === 1) { return $config; @@ -104,6 +101,17 @@ private function getConfig(array $config, array $remainingFieldPathComponents): return $this->getConfig($fields[$remainingFieldPathComponents[0]]['field'], $remainingFieldPathComponents); } + private function flattenSets(array $sets): array + { + if (! Arr::has(Arr::first($sets), 'sets')) { + return $sets; + } + + return collect($sets) + ->flatMap(fn (array $setGroup): array => $setGroup['sets'] ?? []) + ->all(); + } + private function resolveFields(array $fields): array { return collect($fields) diff --git a/tests/Fieldtypes/ReplicatorTest.php b/tests/Fieldtypes/ReplicatorTest.php index 5b62c44d19..7d2a2b097f 100644 --- a/tests/Fieldtypes/ReplicatorTest.php +++ b/tests/Fieldtypes/ReplicatorTest.php @@ -1105,6 +1105,58 @@ public function it_can_return_set_defaults_for_replicator_inside_grid() ], $response->json('new')); } + #[Test] + public function it_can_return_set_defaults_when_sets_are_stored_in_legacy_format() + { + $this->partialMock(RowId::class, function (MockInterface $mock) { + $mock->shouldReceive('generate')->andReturn('random-string-1', 'random-string-2'); + }); + + $blueprint = Facades\Blueprint::make()->setHandle('default')->setNamespace('collections.pages'); + $blueprint->setContents([ + 'sections' => [ + 'main' => [ + 'fields' => [ + [ + 'handle' => 'content', + 'field' => [ + 'type' => 'replicator', + 'sets' => [ + 'video' => [ + 'fields' => [ + ['handle' => 'video_url', 'field' => ['type' => 'text', 'default' => 'https://youtu.be/dQw4w9WgXcQ']], + ], + ], + ], + ], + ], + ], + ], + ], + ]); + + Facades\Blueprint::partialMock(); + Facades\Blueprint::shouldReceive('find')->with('collections.pages.default')->andReturn($blueprint); + + $response = $this + ->actingAs(tap(Facades\User::make()->makeSuper())->save()) + ->postJson(cp_route('replicator-fieldtype.set'), [ + 'blueprint' => 'collections.pages.default', + 'field' => 'content', + 'set' => 'video', + ]) + ->assertOk(); + + $this->assertEquals([ + 'video_url' => 'https://youtu.be/dQw4w9WgXcQ', + ], $response->json('defaults')); + + $this->assertEquals([ + '_' => '_', + 'video_url' => null, + ], $response->json('new')); + } + public static function groupedSetsProvider() { return [