diff --git a/app/Filament/Admin/Resources/Pages/Schemas/PageForm.php b/app/Filament/Admin/Resources/Pages/Schemas/PageForm.php index f1e227c..3b17e6e 100644 --- a/app/Filament/Admin/Resources/Pages/Schemas/PageForm.php +++ b/app/Filament/Admin/Resources/Pages/Schemas/PageForm.php @@ -556,17 +556,17 @@ class PageForm return []; } - // Get existing page data, or use template defaults + // Get existing page data $existingData = $record?->header_data ?? []; $templateDefaults = $template->default_data ?? []; - // Merge: existing data takes priority over defaults - $mergedData = array_merge($templateDefaults, $existingData); - + // Pass both existingData and defaultData separately + // TemplateService will handle empty value fallback return TemplateService::generateDynamicFields( $template, 'header_data', - $mergedData + $existingData, + $templateDefaults ); }) ->visible(fn (Get $get): bool => filled($get('header_template_id'))) @@ -602,17 +602,17 @@ class PageForm return []; } - // Get existing section data, or use template defaults + // Get existing section data $existingData = $get('section_data') ?? []; $templateDefaults = $template->default_data ?? []; - // Only use defaults if no existing data - $mergedData = !empty($existingData) ? $existingData : $templateDefaults; - + // Pass both existingData and defaultData separately + // TemplateService will handle empty value fallback return TemplateService::generateDynamicFields( $template, 'section_data', - $mergedData + $existingData, + $templateDefaults ); }) ->visible(fn (Get $get): bool => filled($get('section_template_id'))) @@ -653,17 +653,17 @@ class PageForm return []; } - // Get existing page data, or use template defaults + // Get existing page data $existingData = $record?->footer_data ?? []; $templateDefaults = $template->default_data ?? []; - // Merge: existing data takes priority over defaults - $mergedData = array_merge($templateDefaults, $existingData); - + // Pass both existingData and defaultData separately + // TemplateService will handle empty value fallback return TemplateService::generateDynamicFields( $template, 'footer_data', - $mergedData + $existingData, + $templateDefaults ); }) ->visible(fn (Get $get): bool => filled($get('footer_template_id'))) diff --git a/app/Services/TemplateService.php b/app/Services/TemplateService.php index b2b6c59..a2a02d1 100644 --- a/app/Services/TemplateService.php +++ b/app/Services/TemplateService.php @@ -18,8 +18,10 @@ class TemplateService * Generate dynamic form fields based on template placeholders * For default data (template forms), use dataKey = 'default_data' * For page data (page forms), use dataKey = 'header_data' or 'footer_data' + * + * @param array|null $defaultData Template's default_data array */ - public static function generateDynamicFields($template, string $dataKey, ?array $existingData = null): array + public static function generateDynamicFields($template, string $dataKey, ?array $existingData = null, ?array $defaultData = null): array { if (!$template) { return []; @@ -42,6 +44,17 @@ class TemplateService // Get existing value from existingData if provided $existingValue = $existingData[$placeholder] ?? null; + // Get default value from template's default_data + $defaultValue = $defaultData[$placeholder] ?? null; + + // If existing value is empty (null, empty string, empty array), use default + $isValueEmpty = $existingValue === null + || $existingValue === '' + || (is_array($existingValue) && empty($existingValue)); + + // Use default if existing value is empty + $finalValue = $isValueEmpty && $defaultValue !== null ? $defaultValue : $existingValue; + $field = match($type) { // Text Input Variants 'text' => TextInput::make("{$dataKey}.{$placeholder}") @@ -217,9 +230,9 @@ class TemplateService ->helperText("Unknown type: {$type}"), }; - // Set default value if existingValue is provided - if ($existingValue !== null) { - $field->default($existingValue); + // Set default value: use finalValue (which falls back to default if existing is empty) + if ($finalValue !== null) { + $field->default($finalValue); } $fields[] = $field;