Refactor TemplateService and page form schemas for improved data handling: Updated generateDynamicFields method to accept default data as a separate parameter, allowing for better fallback logic. Adjusted page form schemas to pass existing and default data separately, enhancing template management and usability.

This commit is contained in:
Ümit Tunç
2025-11-01 03:41:24 -03:00
parent 5946bbed88
commit 4d4cbfc7ed
2 changed files with 32 additions and 19 deletions
@@ -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')))
+17 -4
View File
@@ -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;