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
6 changes: 4 additions & 2 deletions src/Fieldtypes/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,11 @@ public function extraValidationAttributes(): array

public function preload()
{
$defaults = $this->defaultRowData()->all();

return [
'defaults' => $this->defaultRowData()->all(),
'new' => $this->fields()->meta()->all(),
'defaults' => $defaults,
'new' => $this->fields()->addValues($defaults)->meta()->all(),
'existing' => collect($this->field->value())->mapWithKeys(function ($row, $index) {
return [$row['_id'] => $this->fields($index)->addValues($row)->meta()];
})->toArray(),
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Controllers/CP/Fields/MetaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ public function show(Request $request)

$field = (new Field($config['handle'], $config))->setValue($request->value);

$fieldtype = $field->fieldtype();
$value = $field->fieldtype()->preProcess($request->value);

$value = $fieldtype->preProcess($request->value);
$field->setValue($value);

return [
'value' => $value,
'meta' => $fieldtype->preload(),
'meta' => $field->fieldtype()->preload(),
];
}
}
18 changes: 18 additions & 0 deletions tests/Feature/Fields/MetaControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,24 @@ public function an_authorized_user_still_gets_the_full_collection_columns_via_pr
$this->assertContains('intro', $columns);
}

#[Test]
public function it_preloads_meta_using_the_preprocessed_value()
{
$response = $this->fieldMeta(User::make()->makeSuper()->save(), [
'handle' => 'test',
'type' => 'grid',
'min_rows' => 2,
'fields' => [
['handle' => 'words', 'field' => ['type' => 'text']],
],
], [])->assertOk();

$ids = collect($response->json('value'))->pluck('_id')->all();

$this->assertCount(2, $ids);
$this->assertEquals($ids, array_keys($response->json('meta.existing')));
}

#[Test]
public function it_gates_assets_through_the_preload_path()
{
Expand Down
33 changes: 33 additions & 0 deletions tests/Fieldtypes/GridTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,39 @@ public function it_preloads_preprocessed_default_values()
$this->assertSame($expected, $field->fieldtype()->preload()['defaults']);
}

#[Test]
public function it_preloads_new_row_meta_using_the_default_values()
{
$this->partialMock(RowId::class, function (MockInterface $mock) {
$mock->shouldReceive('generate')->twice()->andReturn('random-string-1', 'random-string-2');
});

$field = (new Field('test', [
'type' => 'grid',
'fields' => [
['handle' => 'nested', 'field' => [
'type' => 'grid',
'min_rows' => 2,
'fields' => [
['handle' => 'words', 'field' => ['type' => 'text']],
],
]],
],
]));

$preloaded = $field->fieldtype()->preload();

$this->assertEquals(
['random-string-1', 'random-string-2'],
collect($preloaded['defaults']['nested'])->pluck('_id')->all()
);

$this->assertEquals(
['random-string-1', 'random-string-2'],
array_keys($preloaded['new']['nested']['existing'])
);
}

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