Skip to content

Commit df2d74f

Browse files
committed
Merge branch 'main' into feature/builder-field
2 parents 3c3abd9 + cebffef commit df2d74f

File tree

8 files changed

+67
-15
lines changed

8 files changed

+67
-15
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,13 @@ class EditContent extends EditRecord
164164
return $form
165165
->schema($this->resolveFormFields());
166166
}
167+
168+
public function mutateFormDataBeforeSave(array $data): array
169+
{
170+
$this->mutateBeforeSave($data);
171+
172+
return $data;
173+
}
167174
}
168175
```
169176

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::table('fields', function (Blueprint $table) {
12+
$table->dropUnique(['model_type', 'model_key', 'slug']);
13+
14+
$table->string('model_type', 191)->change();
15+
$table->string('model_key', 191)->change();
16+
$table->string('slug', 191)->change();
17+
18+
$table->unique(['model_type', 'model_key', 'slug', 'parent_ulid'], 'fields_unique_parent');
19+
});
20+
}
21+
};

database/migrations/create_fields_table.php.stub

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ return new class extends Migration
2828
$table->unique(['model_type', 'model_key', 'slug']);
2929
});
3030

31-
if (config('fields.tenancy.is_tenant_aware')) {
32-
$tenant = config('fields.tenancy.relationship');
33-
$key = config('fields.tenancy.key');
31+
if (config('backstage.fields.tenancy.is_tenant_aware')) {
32+
$tenant = config('backstage.fields.tenancy.relationship');
33+
$key = config('backstage.fields.tenancy.key');
3434

3535
Schema::create('field_' . $tenant, function (Blueprint $table) use ($tenant, $key) {
3636
$table->foreignUlid($tenant . '_' . $key)->constrained(table: $tenant, column: $key)->cascadeOnUpdate()->cascadeOnDelete();

src/Concerns/CanMapDynamicFields.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ protected function mutateFormDataBeforeFill(array $data): array
7070
});
7171
}
7272

73-
protected function mutateFormDataBeforeSave(array $data): array
73+
protected function mutateBeforeSave(array $data): array
7474
{
7575
if (! isset($this->record)) {
7676
return $data;
@@ -85,15 +85,32 @@ protected function mutateFormDataBeforeSave(array $data): array
8585
});
8686
}
8787

88+
private function resolveFieldConfigAndInstance(Model $field): array
89+
{
90+
$fieldConfig = Field::tryFrom($field->field_type)
91+
? $this->fieldInspector->initializeDefaultField($field->field_type)
92+
: $this->fieldInspector->initializeCustomField($field->field_type);
93+
94+
return [
95+
'config' => $fieldConfig,
96+
'instance' => new $fieldConfig['class'],
97+
];
98+
}
99+
88100
protected function mutateFormData(array $data, callable $mutationStrategy): array
89101
{
90102
foreach ($this->record->fields as $field) {
91-
$fieldConfig = Field::tryFrom($field->field_type)
92-
? $this->fieldInspector->initializeDefaultField($field->field_type)
93-
: $this->fieldInspector->initializeCustomField($field->field_type);
103+
$field->load('children');
94104

95-
$fieldInstance = new $fieldConfig['class'];
105+
['config' => $fieldConfig, 'instance' => $fieldInstance] = $this->resolveFieldConfigAndInstance($field);
96106
$data = $mutationStrategy($field, $fieldConfig, $fieldInstance, $data);
107+
108+
if (! empty($field->children)) {
109+
foreach ($field->children as $nestedField) {
110+
['config' => $nestedFieldConfig, 'instance' => $nestedFieldInstance] = $this->resolveFieldConfigAndInstance($nestedField);
111+
$data = $mutationStrategy($nestedField, $nestedFieldConfig, $nestedFieldInstance, $data);
112+
}
113+
}
97114
}
98115

99116
return $data;

src/Concerns/HasOptions.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ public static function addOptionsToInput(mixed $input, mixed $field): mixed
1717
$options = [];
1818

1919
foreach ($field->config['relations'] as $relation) {
20-
$resources = config('fields.selectable_resources');
20+
$resources = config('backstage.fields.selectable_resources');
2121
$resourceClass = collect($resources)->first(function ($resource) use ($relation) {
2222
$res = new $resource;
2323
$model = $res->getModel();
2424
$model = new $model;
2525

26+
if (! isset($relation['resource'])) {
27+
return false;
28+
}
29+
2630
return $model->getTable() === $relation['resource'];
2731
});
2832

@@ -121,7 +125,7 @@ public function optionFormFields(): Fieldset
121125
->columnSpanFull()
122126
->live(debounce: 250)
123127
->afterStateUpdated(function (Forms\Set $set, ?string $state) {
124-
$resources = config('fields.selectable_resources');
128+
$resources = config('backstage.fields.selectable_resources');
125129
$resourceClass = collect($resources)->first(function ($resource) use ($state) {
126130
$res = new $resource;
127131
$model = $res->getModel();
@@ -150,7 +154,7 @@ public function optionFormFields(): Fieldset
150154
$set('relationValue_options', $columnOptions);
151155
})
152156
->options(function () {
153-
$resources = config('fields.selectable_resources');
157+
$resources = config('backstage.fields.selectable_resources');
154158

155159
return collect($resources)->map(function ($resource) {
156160
$res = new $resource;
@@ -204,7 +208,7 @@ public function optionFormFields(): Fieldset
204208
return [];
205209
}
206210

207-
$resources = config('fields.selectable_resources');
211+
$resources = config('backstage.fields.selectable_resources');
208212
$resourceClass = collect($resources)->first(function ($r) use ($resource) {
209213
$res = new $r;
210214
$model = $res->getModel();

src/Fields/Repeater.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public function getForm(): array
159159
->label(__('Name'))
160160
->required()
161161
->placeholder(__('Name'))
162-
->live(debounce: 250)
162+
->live(onBlur: true)
163163
->afterStateUpdated(fn (Set $set, ?string $state) => $set('slug', Str::slug($state))),
164164
TextInput::make('slug')
165165
->readonly(),
@@ -169,6 +169,7 @@ public function getForm(): array
169169
->label(__('Field Type'))
170170
->live(debounce: 250)
171171
->reactive()
172+
->default(FieldEnum::Text->value)
172173
->options(
173174
function () {
174175
$options = array_merge(
@@ -215,7 +216,7 @@ private static function generateSchemaFromChildren(Collection $children): array
215216
continue;
216217
}
217218

218-
$schema[] = $field::make($child['name'], $child);
219+
$schema[] = $field::make($child['slug'], $child);
219220
}
220221

221222
return $schema;

src/FieldsServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ protected function getMigrations(): array
151151
{
152152
return [
153153
'create_fields_table',
154+
'change_unique_column_in_fields',
154155
];
155156
}
156157
}

src/Filament/RelationManagers/FieldsRelationManager.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function form(Form $form): Form
4242
->label(__('Name'))
4343
->required()
4444
->placeholder(__('Name'))
45-
->live(debounce: 250)
45+
->live(onBlur: true)
4646
->afterStateUpdated(fn (Set $set, ?string $state) => $set('slug', Str::slug($state))),
4747

4848
TextInput::make('slug')
@@ -54,6 +54,7 @@ public function form(Form $form): Form
5454
->label(__('Field Type'))
5555
->live(debounce: 250)
5656
->reactive()
57+
->default(FieldEnum::Text->value)
5758
->options(function () {
5859
return collect([
5960
...FieldEnum::array(),

0 commit comments

Comments
 (0)