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:
@@ -556,17 +556,17 @@ class PageForm
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get existing page data, or use template defaults
|
// Get existing page data
|
||||||
$existingData = $record?->header_data ?? [];
|
$existingData = $record?->header_data ?? [];
|
||||||
$templateDefaults = $template->default_data ?? [];
|
$templateDefaults = $template->default_data ?? [];
|
||||||
|
|
||||||
// Merge: existing data takes priority over defaults
|
// Pass both existingData and defaultData separately
|
||||||
$mergedData = array_merge($templateDefaults, $existingData);
|
// TemplateService will handle empty value fallback
|
||||||
|
|
||||||
return TemplateService::generateDynamicFields(
|
return TemplateService::generateDynamicFields(
|
||||||
$template,
|
$template,
|
||||||
'header_data',
|
'header_data',
|
||||||
$mergedData
|
$existingData,
|
||||||
|
$templateDefaults
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
->visible(fn (Get $get): bool => filled($get('header_template_id')))
|
->visible(fn (Get $get): bool => filled($get('header_template_id')))
|
||||||
@@ -602,17 +602,17 @@ class PageForm
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get existing section data, or use template defaults
|
// Get existing section data
|
||||||
$existingData = $get('section_data') ?? [];
|
$existingData = $get('section_data') ?? [];
|
||||||
$templateDefaults = $template->default_data ?? [];
|
$templateDefaults = $template->default_data ?? [];
|
||||||
|
|
||||||
// Only use defaults if no existing data
|
// Pass both existingData and defaultData separately
|
||||||
$mergedData = !empty($existingData) ? $existingData : $templateDefaults;
|
// TemplateService will handle empty value fallback
|
||||||
|
|
||||||
return TemplateService::generateDynamicFields(
|
return TemplateService::generateDynamicFields(
|
||||||
$template,
|
$template,
|
||||||
'section_data',
|
'section_data',
|
||||||
$mergedData
|
$existingData,
|
||||||
|
$templateDefaults
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
->visible(fn (Get $get): bool => filled($get('section_template_id')))
|
->visible(fn (Get $get): bool => filled($get('section_template_id')))
|
||||||
@@ -653,17 +653,17 @@ class PageForm
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get existing page data, or use template defaults
|
// Get existing page data
|
||||||
$existingData = $record?->footer_data ?? [];
|
$existingData = $record?->footer_data ?? [];
|
||||||
$templateDefaults = $template->default_data ?? [];
|
$templateDefaults = $template->default_data ?? [];
|
||||||
|
|
||||||
// Merge: existing data takes priority over defaults
|
// Pass both existingData and defaultData separately
|
||||||
$mergedData = array_merge($templateDefaults, $existingData);
|
// TemplateService will handle empty value fallback
|
||||||
|
|
||||||
return TemplateService::generateDynamicFields(
|
return TemplateService::generateDynamicFields(
|
||||||
$template,
|
$template,
|
||||||
'footer_data',
|
'footer_data',
|
||||||
$mergedData
|
$existingData,
|
||||||
|
$templateDefaults
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
->visible(fn (Get $get): bool => filled($get('footer_template_id')))
|
->visible(fn (Get $get): bool => filled($get('footer_template_id')))
|
||||||
|
|||||||
@@ -18,8 +18,10 @@ class TemplateService
|
|||||||
* Generate dynamic form fields based on template placeholders
|
* Generate dynamic form fields based on template placeholders
|
||||||
* For default data (template forms), use dataKey = 'default_data'
|
* For default data (template forms), use dataKey = 'default_data'
|
||||||
* For page data (page forms), use dataKey = 'header_data' or 'footer_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) {
|
if (!$template) {
|
||||||
return [];
|
return [];
|
||||||
@@ -42,6 +44,17 @@ class TemplateService
|
|||||||
// Get existing value from existingData if provided
|
// Get existing value from existingData if provided
|
||||||
$existingValue = $existingData[$placeholder] ?? null;
|
$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) {
|
$field = match($type) {
|
||||||
// Text Input Variants
|
// Text Input Variants
|
||||||
'text' => TextInput::make("{$dataKey}.{$placeholder}")
|
'text' => TextInput::make("{$dataKey}.{$placeholder}")
|
||||||
@@ -217,9 +230,9 @@ class TemplateService
|
|||||||
->helperText("Unknown type: {$type}"),
|
->helperText("Unknown type: {$type}"),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Set default value if existingValue is provided
|
// Set default value: use finalValue (which falls back to default if existing is empty)
|
||||||
if ($existingValue !== null) {
|
if ($finalValue !== null) {
|
||||||
$field->default($existingValue);
|
$field->default($finalValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
$fields[] = $field;
|
$fields[] = $field;
|
||||||
|
|||||||
Reference in New Issue
Block a user