Skip to content

Commit 5af1ebf

Browse files
committed
new trait to render schemas with fields
1 parent 2ebe975 commit 5af1ebf

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
namespace Backstage\Fields\Concerns;
4+
5+
use Backstage\Fields\Models\Field;
6+
use Backstage\Fields\Models\Schema;
7+
8+
trait CanMapSchemasWithFields
9+
{
10+
use CanMapDynamicFields;
11+
12+
protected function loadAllFieldsIntoRecord(): void
13+
{
14+
$allFields = $this->record->fields;
15+
16+
foreach ($this->record->schemas as $schema) {
17+
$schemaFields = Field::where('schema_id', $schema->ulid)->get();
18+
$allFields = $allFields->merge($schemaFields);
19+
}
20+
21+
$this->record->setRelation('fields', $allFields);
22+
}
23+
24+
protected function loadDefaultValuesIntoRecord(): void
25+
{
26+
$defaultValues = [];
27+
$allFields = $this->record->fields;
28+
29+
foreach ($allFields as $field) {
30+
$defaultValue = $field->config['defaultValue'] ?? null;
31+
32+
if ($field->field_type === 'select' && $defaultValue === null) {
33+
continue;
34+
}
35+
36+
$defaultValues[$field->ulid] = $defaultValue;
37+
}
38+
39+
$this->record->setAttribute('values', $defaultValues);
40+
}
41+
42+
protected function getFieldsFromSchema(Schema $schema): \Illuminate\Support\Collection
43+
{
44+
$fields = collect();
45+
$schemaFields = Field::where('schema_id', $schema->ulid)->get();
46+
$fields = $fields->merge($schemaFields);
47+
48+
$childSchemas = $schema->children()->get();
49+
foreach ($childSchemas as $childSchema) {
50+
$fields = $fields->merge($this->getFieldsFromSchema($childSchema));
51+
}
52+
53+
return $fields;
54+
}
55+
56+
protected function getAllSchemaFields(): \Illuminate\Support\Collection
57+
{
58+
$allFields = collect();
59+
$rootSchemas = $this->record->schemas()
60+
->whereNull('parent_ulid')
61+
->orderBy('position')
62+
->get();
63+
64+
foreach ($rootSchemas as $schema) {
65+
$allFields = $allFields->merge($this->getFieldsFromSchema($schema));
66+
}
67+
68+
return $allFields;
69+
}
70+
71+
protected function initializeFormData(): void
72+
{
73+
$this->loadAllFieldsIntoRecord();
74+
$this->loadDefaultValuesIntoRecord();
75+
$this->data = $this->mutateBeforeFill($this->data);
76+
}
77+
78+
protected function mutateBeforeFill(array $data): array
79+
{
80+
if (! $this->hasValidRecordWithFields()) {
81+
return $data;
82+
}
83+
84+
$builderBlocks = $this->extractBuilderBlocksFromRecord();
85+
$allFields = $this->getAllFieldsIncludingBuilderFields($builderBlocks);
86+
87+
if (! isset($data[$this->record->valueColumn])) {
88+
$data[$this->record->valueColumn] = [];
89+
}
90+
91+
return $this->mutateFormData($data, $allFields, function ($field, $fieldConfig, $fieldInstance, $data) use ($builderBlocks) {
92+
if ($field->field_type === 'select') {
93+
if (isset($this->record->values[$field->ulid])) {
94+
$data[$this->record->valueColumn][$field->ulid] = $this->record->values[$field->ulid];
95+
}
96+
return $data;
97+
}
98+
99+
return $this->applyFieldFillMutation($field, $fieldConfig, $fieldInstance, $data, $builderBlocks);
100+
});
101+
}
102+
}

0 commit comments

Comments
 (0)