Skip to content

Commit 9e2f546

Browse files
committed
limit saving "fields" base on "document type's fields key"
1 parent f351fa6 commit 9e2f546

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/Base/Filament/Resources/Pages/BaseContentEditPage.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use SolutionForest\InspireCms\Filament\Actions\UpdateRouteAction;
2222
use SolutionForest\InspireCms\Helpers\FilamentActionHelper;
2323
use SolutionForest\InspireCms\Helpers\FilamentResourceHelper;
24+
use SolutionForest\InspireCms\Models\Contracts\Content;
25+
use SolutionForest\InspireCms\Models\Contracts\FieldGroup;
2426

2527
use function Filament\Support\is_app_url;
2628

@@ -158,8 +160,24 @@ protected function handleRecordUpdate(Model $record, array $data): Model
158160

159161
$record->fill(Arr::except($data, $translatableAttributes));
160162

163+
$currentFieldsForType = $record instanceof Content
164+
? $record->documentType?->fieldGroups->whereInstanceOf(FieldGroup::class)->mapWithKeys(fn (FieldGroup $fg) => [$fg->name => $fg->fields->pluck('name')->all()])->all()
165+
: [];
166+
// Limit the propertyData to the current fields for the type
167+
$propertyData = Arr::only($data['propertyData'] ?? [], array_keys($currentFieldsForType));
168+
foreach ($propertyData as $gpKey => $value) {
169+
if (!is_array($value)) {
170+
continue;
171+
}
172+
$targetFields = $currentFieldsForType[$gpKey] ?? null;
173+
if (is_null($targetFields) || !is_array($targetFields) || empty($targetFields)) {
174+
continue;
175+
}
176+
$propertyData[$gpKey] = Arr::only($value, $targetFields);
177+
}
178+
161179
// handle 'Property Data' translation here
162-
$record->setTranslation('propertyData', '', $data['propertyData'] ?? []);
180+
$record->setTranslation('propertyData', '', $propertyData);
163181

164182
foreach (Arr::only($data, $translatableAttributes) as $key => $value) {
165183
$record->setTranslation($key, $this->activeLocale, $value);

0 commit comments

Comments
 (0)