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;
|
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(),
|
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;
|
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(),
|
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;
|
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(),
|
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
|
* Replace placeholders in HTML with actual data
|
||||||
* Supports both {text.title} and {{text.title}} formats
|
* 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
|
public static function replacePlaceholders(string $html, array $data): string
|
||||||
{
|
{
|
||||||
@@ -276,6 +276,12 @@ class TemplateService
|
|||||||
$renderedMenu = \App\Services\MenuService::render();
|
$renderedMenu = \App\Services\MenuService::render();
|
||||||
$html = str_replace('{menu}', $renderedMenu, $html);
|
$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}
|
// First, parse all placeholders in the format {type.field_name}
|
||||||
preg_match_all('/\{([a-z]+\.[a-z_]+)\}/i', $html, $matches);
|
preg_match_all('/\{([a-z]+\.[a-z_]+)\}/i', $html, $matches);
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
// Get menu items from Pages
|
||||||
|
$menuItems = \App\Models\Page::with(['children' => function ($query) {
|
||||||
|
$query->where('status', 'published')
|
||||||
|
->where('show_in_menu', true)
|
||||||
|
->orderBy('sort_order', 'asc')
|
||||||
|
->orderBy('title', 'asc');
|
||||||
|
}])
|
||||||
|
->whereNull('parent_id')
|
||||||
|
->where('status', 'published')
|
||||||
|
->where('show_in_menu', true)
|
||||||
|
->orderBy('sort_order', 'asc')
|
||||||
|
->orderBy('title', 'asc')
|
||||||
|
->get();
|
||||||
|
?>
|
||||||
|
|
||||||
|
@if($menuItems->isNotEmpty())
|
||||||
|
<nav class="static-menu">
|
||||||
|
<ul class="static-menu-list">
|
||||||
|
@foreach($menuItems as $item)
|
||||||
|
@php
|
||||||
|
$hasChildren = $item->children->isNotEmpty();
|
||||||
|
@endphp
|
||||||
|
<li class="static-menu-item {{ $hasChildren ? 'has-children' : '' }}">
|
||||||
|
<a href="{{ route('page.show', $item->slug) }}" class="static-menu-link">
|
||||||
|
{{ $item->title }}
|
||||||
|
</a>
|
||||||
|
|
||||||
|
@if($hasChildren)
|
||||||
|
<ul class="static-submenu">
|
||||||
|
@foreach($item->children as $child)
|
||||||
|
<li class="static-submenu-item">
|
||||||
|
<a href="{{ route('page.show', $child->slug) }}" class="static-submenu-link">
|
||||||
|
{{ $child->title }}
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
@endforeach
|
||||||
|
</ul>
|
||||||
|
@endif
|
||||||
|
</li>
|
||||||
|
@endforeach
|
||||||
|
</ul>
|
||||||
|
</nav>
|
||||||
|
@endif
|
||||||
|
|
||||||
Reference in New Issue
Block a user