Skip to content

Commit e724982

Browse files
[6.x] Fix adding sets stored in the legacy format (#13743)
1 parent f75333e commit e724982

2 files changed

Lines changed: 66 additions & 6 deletions

File tree

src/Http/Controllers/CP/Fieldtypes/ReplicatorSetController.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Statamic\Fields\Field;
1212
use Statamic\Fields\Fields;
1313
use Statamic\Http\Controllers\CP\CpController;
14+
use Statamic\Support\Arr;
1415

1516
class ReplicatorSetController extends CpController
1617
{
@@ -31,9 +32,7 @@ public function __invoke(Request $request)
3132

3233
$field = $this->getReplicatorField($blueprint, $request->field);
3334

34-
$replicatorSet = collect($field->get('sets'))
35-
->flatMap(fn (array $setGroup) => $setGroup['sets'] ?? [])
36-
->get($request->set);
35+
$replicatorSet = $this->flattenSets($field->get('sets'))[$request->set];
3736

3837
if (! $replicatorSet) {
3938
throw new \Exception("Cannot find Replicator set [$request->set]");
@@ -76,9 +75,7 @@ private function getConfig(array $config, array $remainingFieldPathComponents):
7675
$isReplicator = isset($config['type']) && in_array($config['type'], ['bard', 'replicator']);
7776

7877
if ($isReplicator) {
79-
$flattenedSets = collect($config['sets'])
80-
->flatMap(fn (array $setGroup): array => $setGroup['sets'] ?? [])
81-
->all();
78+
$flattenedSets = $this->flattenSets($config['sets'] ?? []);
8279

8380
if (count($remainingFieldPathComponents) === 1) {
8481
return $config;
@@ -104,6 +101,17 @@ private function getConfig(array $config, array $remainingFieldPathComponents):
104101
return $this->getConfig($fields[$remainingFieldPathComponents[0]]['field'], $remainingFieldPathComponents);
105102
}
106103

104+
private function flattenSets(array $sets): array
105+
{
106+
if (! Arr::has(Arr::first($sets), 'sets')) {
107+
return $sets;
108+
}
109+
110+
return collect($sets)
111+
->flatMap(fn (array $setGroup): array => $setGroup['sets'] ?? [])
112+
->all();
113+
}
114+
107115
private function resolveFields(array $fields): array
108116
{
109117
return collect($fields)

tests/Fieldtypes/ReplicatorTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,58 @@ public function it_can_return_set_defaults_for_replicator_inside_grid()
11051105
], $response->json('new'));
11061106
}
11071107

1108+
#[Test]
1109+
public function it_can_return_set_defaults_when_sets_are_stored_in_legacy_format()
1110+
{
1111+
$this->partialMock(RowId::class, function (MockInterface $mock) {
1112+
$mock->shouldReceive('generate')->andReturn('random-string-1', 'random-string-2');
1113+
});
1114+
1115+
$blueprint = Facades\Blueprint::make()->setHandle('default')->setNamespace('collections.pages');
1116+
$blueprint->setContents([
1117+
'sections' => [
1118+
'main' => [
1119+
'fields' => [
1120+
[
1121+
'handle' => 'content',
1122+
'field' => [
1123+
'type' => 'replicator',
1124+
'sets' => [
1125+
'video' => [
1126+
'fields' => [
1127+
['handle' => 'video_url', 'field' => ['type' => 'text', 'default' => 'https://youtu.be/dQw4w9WgXcQ']],
1128+
],
1129+
],
1130+
],
1131+
],
1132+
],
1133+
],
1134+
],
1135+
],
1136+
]);
1137+
1138+
Facades\Blueprint::partialMock();
1139+
Facades\Blueprint::shouldReceive('find')->with('collections.pages.default')->andReturn($blueprint);
1140+
1141+
$response = $this
1142+
->actingAs(tap(Facades\User::make()->makeSuper())->save())
1143+
->postJson(cp_route('replicator-fieldtype.set'), [
1144+
'blueprint' => 'collections.pages.default',
1145+
'field' => 'content',
1146+
'set' => 'video',
1147+
])
1148+
->assertOk();
1149+
1150+
$this->assertEquals([
1151+
'video_url' => 'https://youtu.be/dQw4w9WgXcQ',
1152+
], $response->json('defaults'));
1153+
1154+
$this->assertEquals([
1155+
'_' => '_',
1156+
'video_url' => null,
1157+
], $response->json('new'));
1158+
}
1159+
11081160
public static function groupedSetsProvider()
11091161
{
11101162
return [

0 commit comments

Comments
 (0)