Add data transformation for default_data fields in template forms: Implemented mutateFormDataBeforeSave method in Create and Edit pages for Footer, Header, and Section templates to properly format nested default_data fields into an array structure before saving. This enhancement improves data handling and ensures consistency across template forms.
This commit is contained in:
@@ -14,4 +14,28 @@ class CreateFooterTemplate extends CreateRecord
|
||||
{
|
||||
return Width::Full;
|
||||
}
|
||||
|
||||
protected function mutateFormDataBeforeSave(array $data): array
|
||||
{
|
||||
// Transform nested default_data fields (e.g., default_data.color.bg)
|
||||
// into proper array format
|
||||
$defaultData = [];
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
if (str_starts_with($key, 'default_data.')) {
|
||||
// Remove 'default_data.' prefix to get the nested key
|
||||
$nestedKey = substr($key, 13); // 'default_data.' length is 13
|
||||
$defaultData[$nestedKey] = $value;
|
||||
// Remove the flattened key from data
|
||||
unset($data[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
// Set the properly formatted default_data array
|
||||
if (!empty($defaultData)) {
|
||||
$data['default_data'] = $defaultData;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,4 +28,28 @@ class EditFooterTemplate extends EditRecord
|
||||
RestoreAction::make(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function mutateFormDataBeforeSave(array $data): array
|
||||
{
|
||||
// Transform nested default_data fields (e.g., default_data.color.bg)
|
||||
// into proper array format
|
||||
$defaultData = [];
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
if (str_starts_with($key, 'default_data.')) {
|
||||
// Remove 'default_data.' prefix to get the nested key
|
||||
$nestedKey = substr($key, 13); // 'default_data.' length is 13
|
||||
$defaultData[$nestedKey] = $value;
|
||||
// Remove the flattened key from data
|
||||
unset($data[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
// Set the properly formatted default_data array
|
||||
if (!empty($defaultData)) {
|
||||
$data['default_data'] = $defaultData;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,4 +14,28 @@ class CreateHeaderTemplate extends CreateRecord
|
||||
{
|
||||
return Width::Full;
|
||||
}
|
||||
|
||||
protected function mutateFormDataBeforeSave(array $data): array
|
||||
{
|
||||
// Transform nested default_data fields (e.g., default_data.color.bg)
|
||||
// into proper array format
|
||||
$defaultData = [];
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
if (str_starts_with($key, 'default_data.')) {
|
||||
// Remove 'default_data.' prefix to get the nested key
|
||||
$nestedKey = substr($key, 13); // 'default_data.' length is 13
|
||||
$defaultData[$nestedKey] = $value;
|
||||
// Remove the flattened key from data
|
||||
unset($data[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
// Set the properly formatted default_data array
|
||||
if (!empty($defaultData)) {
|
||||
$data['default_data'] = $defaultData;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,4 +28,28 @@ class EditHeaderTemplate extends EditRecord
|
||||
RestoreAction::make(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function mutateFormDataBeforeSave(array $data): array
|
||||
{
|
||||
// Transform nested default_data fields (e.g., default_data.color.bg)
|
||||
// into proper array format
|
||||
$defaultData = [];
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
if (str_starts_with($key, 'default_data.')) {
|
||||
// Remove 'default_data.' prefix to get the nested key
|
||||
$nestedKey = substr($key, 13); // 'default_data.' length is 13
|
||||
$defaultData[$nestedKey] = $value;
|
||||
// Remove the flattened key from data
|
||||
unset($data[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
// Set the properly formatted default_data array
|
||||
if (!empty($defaultData)) {
|
||||
$data['default_data'] = $defaultData;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,4 +14,28 @@ class CreateSectionTemplate extends CreateRecord
|
||||
{
|
||||
return Width::Full;
|
||||
}
|
||||
|
||||
protected function mutateFormDataBeforeSave(array $data): array
|
||||
{
|
||||
// Transform nested default_data fields (e.g., default_data.color.bg)
|
||||
// into proper array format
|
||||
$defaultData = [];
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
if (str_starts_with($key, 'default_data.')) {
|
||||
// Remove 'default_data.' prefix to get the nested key
|
||||
$nestedKey = substr($key, 13); // 'default_data.' length is 13
|
||||
$defaultData[$nestedKey] = $value;
|
||||
// Remove the flattened key from data
|
||||
unset($data[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
// Set the properly formatted default_data array
|
||||
if (!empty($defaultData)) {
|
||||
$data['default_data'] = $defaultData;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,4 +28,28 @@ class EditSectionTemplate extends EditRecord
|
||||
RestoreAction::make(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function mutateFormDataBeforeSave(array $data): array
|
||||
{
|
||||
// Transform nested default_data fields (e.g., default_data.color.bg)
|
||||
// into proper array format
|
||||
$defaultData = [];
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
if (str_starts_with($key, 'default_data.')) {
|
||||
// Remove 'default_data.' prefix to get the nested key
|
||||
$nestedKey = substr($key, 13); // 'default_data.' length is 13
|
||||
$defaultData[$nestedKey] = $value;
|
||||
// Remove the flattened key from data
|
||||
unset($data[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
// Set the properly formatted default_data array
|
||||
if (!empty($defaultData)) {
|
||||
$data['default_data'] = $defaultData;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -267,7 +267,7 @@ class TemplateService
|
||||
/**
|
||||
* Replace placeholders in HTML with actual data
|
||||
* Supports both {text.title} and {{text.title}} formats
|
||||
* Also supports {menu} placeholder for menu rendering
|
||||
* Also supports {menu} and {staticMenu} placeholders for menu rendering
|
||||
*/
|
||||
public static function replacePlaceholders(string $html, array $data): string
|
||||
{
|
||||
@@ -276,6 +276,12 @@ class TemplateService
|
||||
$renderedMenu = \App\Services\MenuService::render();
|
||||
$html = str_replace('{menu}', $renderedMenu, $html);
|
||||
}
|
||||
|
||||
// Handle special {staticMenu} placeholder
|
||||
if (str_contains($html, '{staticMenu}')) {
|
||||
$renderedStaticMenu = view('components.static-menu')->render();
|
||||
$html = str_replace('{staticMenu}', $renderedStaticMenu, $html);
|
||||
}
|
||||
|
||||
// First, parse all placeholders in the format {type.field_name}
|
||||
preg_match_all('/\{([a-z]+\.[a-z_]+)\}/i', $html, $matches);
|
||||
|
||||
Reference in New Issue
Block a user