Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private function getReplicatorField(Blueprint $blueprint, string $field): Field

private function getConfig(array $config, array $remainingFieldPathComponents): array
{
$isGroupOrGrid = isset($config['type']) && in_array($config['type'], ['group', 'grid']);
$hasNestedFields = isset($config['type']) && isset($config['fields']);
$isReplicator = isset($config['type']) && in_array($config['type'], ['bard', 'replicator']);

if ($isReplicator) {
Expand All @@ -100,7 +100,7 @@ private function getConfig(array $config, array $remainingFieldPathComponents):
return $this->getConfig($flattenedSets, $remainingFieldPathComponents);
}

if ($isGroupOrGrid) {
if ($hasNestedFields) {
array_shift($remainingFieldPathComponents);

$fields = $this->resolveFields($config['fields'] ?? []);
Expand Down
146 changes: 146 additions & 0 deletions tests/Fieldtypes/ReplicatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,152 @@ public function it_can_return_set_defaults_for_replicator_inside_grid()
], $response->json('new'));
}

#[Test]
public function it_can_return_set_defaults_for_replicator_inside_custom_fieldtype()
{
$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' => 'stuff',
'field' => [
'type' => 'custom_fieldtype',
'fields' => [
[
'handle' => 'content_blocks',
'field' => [
'type' => 'replicator',
'sets' => [
'text' => [
'fields' => [
[
'handle' => 'body',
'field' => [
'type' => 'textarea',
'default' => 'the default',
],
],
],
],
],
],
],
],
],
],
],
],
],
]);

Facades\Blueprint::partialMock();
Facades\Blueprint::shouldReceive('find')->with('collections.pages.default')->andReturn($blueprint);

$user = tap(Facades\User::make()->makeSuper())->save();

$response = $this
->actingAs($user)
->postJson(cp_route('replicator-fieldtype.set'), [
'token' => encrypt([
'fqh' => 'collections.pages.default',
'user_id' => $user->id(),
]),
'field' => 'stuff.content_blocks',
'set' => 'text',
])
->assertOk();

$this->assertEquals([
'body' => 'the default',
], $response->json('defaults'));

$this->assertEquals([
'_' => '_',
'body' => null,
], $response->json('new'));
}

#[Test]
public function it_can_return_set_defaults_for_replicator_inside_a_set_handled_fields()
{
$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' => 'form_builder',
'field' => [
'type' => 'replicator',
'sets' => [
'fields' => [
'fields' => [
[
'handle' => 'options',
'field' => [
'type' => 'replicator',
'sets' => [
'option' => [
'fields' => [
[
'handle' => 'label',
'field' => [
'type' => 'text',
'default' => 'the default',
],
],
],
],
],
],
],
],
],
],
],
],
],
],
],
]);

Facades\Blueprint::partialMock();
Facades\Blueprint::shouldReceive('find')->with('collections.pages.default')->andReturn($blueprint);

$user = tap(Facades\User::make()->makeSuper())->save();

$response = $this
->actingAs($user)
->postJson(cp_route('replicator-fieldtype.set'), [
'token' => encrypt([
'fqh' => 'collections.pages.default',
'user_id' => $user->id(),
]),
'field' => 'form_builder.fields.options',
'set' => 'option',
])
->assertOk();

$this->assertEquals([
'label' => 'the default',
], $response->json('defaults'));

$this->assertEquals([
'_' => '_',
'label' => null,
], $response->json('new'));
}

#[Test]
public function it_can_return_set_defaults_when_sets_are_stored_in_legacy_format()
{
Expand Down
Loading