Skip to content

Commit 445acb1

Browse files
authored
Merge pull request #41 from evelynfredin/fix/add-fields-to-grid
Add fields parameter to Grid constructor and initialize fields property
2 parents d0b319e + 492ed8d commit 445acb1

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/FieldTypes/Grid.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@ class Grid extends Field
2222

2323
protected $min_rows;
2424

25+
protected $fields;
26+
2527
protected $mode = GridModeOption::Table;
2628

27-
public function __construct(string $handle)
29+
public function __construct(string $handle, array $fields = [])
2830
{
2931
parent::__construct($handle);
32+
33+
$this->fields = collect($fields);
3034
}
3135

3236
public function fieldToArray(): Collection
@@ -38,6 +42,7 @@ public function fieldToArray(): Collection
3842
'max_rows' => $this->max_rows,
3943
'min_rows' => $this->min_rows,
4044
'mode' => $this->mode->value,
45+
'fields' => $this->fieldsToArray(),
4146
]);
4247
}
4348

tests/Unit/Fields/GridTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,11 @@
6565

6666
expect($field->toArray()['field']['mode'])->toBe(\Tdwesten\StatamicBuilder\Enums\GridModeOption::Table->value);
6767
});
68+
69+
it('can render to a array with fields', function () {
70+
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Grid('grid', [
71+
new \Tdwesten\StatamicBuilder\FieldTypes\Text('title'),
72+
]);
73+
74+
expect($field->toArray()['field']['fields'][0]['field']['type'])->toBe('text');
75+
});

0 commit comments

Comments
 (0)