From 8a58e632594a573e57d57297bb1d46e70cb53346 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Cmit=20Tun=C3=A7?= Date: Thu, 30 Oct 2025 16:48:30 -0300 Subject: [PATCH] Enhance Page Edit functionality: Implemented conversion of old format sections to a new structured format in the EditPage class, allowing for better data management. Updated PageForm schema to conditionally hydrate fields based on their type, improving the user experience and ensuring proper data handling for various input types. --- .../Admin/Resources/Pages/Pages/EditPage.php | 52 ++++++++ .../Resources/Pages/Schemas/PageForm.php | 114 ++++++++++++++++-- 2 files changed, 155 insertions(+), 11 deletions(-) diff --git a/app/Filament/Admin/Resources/Pages/Pages/EditPage.php b/app/Filament/Admin/Resources/Pages/Pages/EditPage.php index 569c930..6fc9970 100644 --- a/app/Filament/Admin/Resources/Pages/Pages/EditPage.php +++ b/app/Filament/Admin/Resources/Pages/Pages/EditPage.php @@ -45,6 +45,58 @@ class EditPage extends EditRecord // Load existing translations $translationData = TranslationTabs::fillFromRecord($this->record); + // Convert old format sections to new format + if (isset($data['sections']) && is_array($data['sections'])) { + $data['sections'] = array_map(function ($section) { + if (!isset($section['data']) || !is_array($section['data'])) { + return $section; + } + + // Check if already in new format + $firstKey = array_key_first($section['data']); + if (is_numeric($firstKey) && isset($section['data'][$firstKey]['key'])) { + return $section; + } + + // Convert old format to new format + $newData = []; + foreach ($section['data'] as $key => $value) { + $type = 'text'; // Default + + // Auto-detect type based on key name and value + if (str_contains($key, 'image') || str_contains($key, 'photo')) { + $type = 'image'; + } elseif (str_contains($key, 'icon') && is_string($value) && str_starts_with($value, 'assets/')) { + $type = 'image'; + } elseif (str_contains($key, 'color')) { + $type = 'color'; + } elseif (str_contains($key, 'url') || str_contains($key, 'link')) { + $type = 'url'; + } elseif (is_string($value) && (str_contains($key, 'subtitle') || str_contains($key, 'description'))) { + $type = 'textarea'; + } elseif (is_string($value) && str_contains($value, '<') && str_contains($value, '>') && strlen($value) > 200) { + $type = 'html'; // Long HTML content + } elseif (is_string($value) && strlen($value) > 100) { + $type = 'textarea'; + } elseif (is_array($value)) { + $type = 'array'; + } elseif (is_bool($value)) { + $type = 'boolean'; + } + // 'title' gibi kısa HTML snippet'leri 'text' olarak kalır + + $newData[] = [ + 'key' => $key, + 'type' => $type, + 'value' => $value, + ]; + } + + $section['data'] = $newData; + return $section; + }, $data['sections']); + } + return array_merge($data, $translationData); } diff --git a/app/Filament/Admin/Resources/Pages/Schemas/PageForm.php b/app/Filament/Admin/Resources/Pages/Schemas/PageForm.php index 7b5e6df..0c37c7e 100644 --- a/app/Filament/Admin/Resources/Pages/Schemas/PageForm.php +++ b/app/Filament/Admin/Resources/Pages/Schemas/PageForm.php @@ -245,7 +245,16 @@ class PageForm ->placeholder('Değer girin...') ->live(onBlur: true) ->afterStateUpdated(fn ($state, callable $set) => $set('value', $state)) - ->afterStateHydrated(fn ($component, $state, Get $get) => $component->state($get('value'))) + ->afterStateHydrated(function ($component, $state, Get $get) { + // Only hydrate if this field type is active + if (!in_array($get('type'), ['text', 'url', 'email', 'phone', 'number'])) { + return; + } + $value = $get('value'); + if ($value !== null) { + $component->state($value); + } + }) ->dehydrated(false) ->columnSpanFull(), @@ -257,7 +266,16 @@ class PageForm ->placeholder(fn (Get $get) => $get('type') === 'html' ? '
HTML kodu girin...
' : 'Metin girin...') ->live(onBlur: true) ->afterStateUpdated(fn ($state, callable $set) => $set('value', $state)) - ->afterStateHydrated(fn ($component, $state, Get $get) => $component->state($get('value'))) + ->afterStateHydrated(function ($component, $state, Get $get) { + // Only hydrate if this field type is active + if (!in_array($get('type'), ['textarea', 'json', 'html'])) { + return; + } + $value = $get('value'); + if ($value !== null) { + $component->state($value); + } + }) ->dehydrated(false) ->columnSpanFull(), @@ -276,7 +294,33 @@ class PageForm ]) ->live(onBlur: true) ->afterStateUpdated(fn ($state, callable $set) => $set('value', $state)) - ->afterStateHydrated(fn ($component, $state, Get $get) => $component->state($get('value'))) + ->afterStateHydrated(function ($component, $state, Get $get) { + // Only hydrate if this field type is richtext + if ($get('type') !== 'richtext') { + return; + } + + $value = $get('value'); + + // Skip if null or empty + if ($value === null || $value === '' || $value === []) { + return; + } + + // Only set if it's valid TipTap JSON format + if (is_array($value) && isset($value['type']) && $value['type'] === 'doc') { + $component->state($value); + } elseif (is_string($value)) { + try { + $decoded = json_decode($value, true); + if (is_array($decoded) && isset($decoded['type']) && $decoded['type'] === 'doc') { + $component->state($decoded); + } + } catch (\Exception $e) { + // Invalid JSON, skip + } + } + }) ->dehydrated(false) ->columnSpanFull(), @@ -286,7 +330,13 @@ class PageForm ->visible(fn (Get $get) => $get('type') === 'markdown') ->live(onBlur: true) ->afterStateUpdated(fn ($state, callable $set) => $set('value', $state)) - ->afterStateHydrated(fn ($component, $state, Get $get) => $component->state($get('value'))) + ->afterStateHydrated(function ($component, $state, Get $get) { + if ($get('type') !== 'markdown') return; + $value = $get('value'); + if ($value !== null && is_string($value)) { + $component->state($value); + } + }) ->dehydrated(false) ->columnSpanFull(), @@ -300,7 +350,13 @@ class PageForm ->imageEditor() ->live() ->afterStateUpdated(fn ($state, callable $set) => $set('value', $state)) - ->afterStateHydrated(fn ($component, $state, Get $get) => $component->state($get('value'))) + ->afterStateHydrated(function ($component, $state, Get $get) { + if ($get('type') !== 'image') return; + $value = $get('value'); + if ($value !== null && is_string($value)) { + $component->state($value); + } + }) ->dehydrated(false) ->columnSpanFull(), @@ -312,7 +368,13 @@ class PageForm ->directory('pages/sections') ->live() ->afterStateUpdated(fn ($state, callable $set) => $set('value', $state)) - ->afterStateHydrated(fn ($component, $state, Get $get) => $component->state($get('value'))) + ->afterStateHydrated(function ($component, $state, Get $get) { + if ($get('type') !== 'file') return; + $value = $get('value'); + if ($value !== null && is_string($value)) { + $component->state($value); + } + }) ->dehydrated(false) ->columnSpanFull(), @@ -322,7 +384,13 @@ class PageForm ->visible(fn (Get $get) => $get('type') === 'boolean') ->live() ->afterStateUpdated(fn ($state, callable $set) => $set('value', $state)) - ->afterStateHydrated(fn ($component, $state, Get $get) => $component->state($get('value'))) + ->afterStateHydrated(function ($component, $state, Get $get) { + if ($get('type') !== 'boolean') return; + $value = $get('value'); + if ($value !== null && is_bool($value)) { + $component->state($value); + } + }) ->dehydrated(false) ->columnSpanFull(), @@ -332,7 +400,13 @@ class PageForm ->visible(fn (Get $get) => $get('type') === 'color') ->live(onBlur: true) ->afterStateUpdated(fn ($state, callable $set) => $set('value', $state)) - ->afterStateHydrated(fn ($component, $state, Get $get) => $component->state($get('value'))) + ->afterStateHydrated(function ($component, $state, Get $get) { + if ($get('type') !== 'color') return; + $value = $get('value'); + if ($value !== null && is_string($value)) { + $component->state($value); + } + }) ->dehydrated(false) ->columnSpanFull(), @@ -343,7 +417,13 @@ class PageForm ->displayFormat('d/m/Y') ->live() ->afterStateUpdated(fn ($state, callable $set) => $set('value', $state)) - ->afterStateHydrated(fn ($component, $state, Get $get) => $component->state($get('value'))) + ->afterStateHydrated(function ($component, $state, Get $get) { + if ($get('type') !== 'date') return; + $value = $get('value'); + if ($value !== null && is_string($value)) { + $component->state($value); + } + }) ->dehydrated(false) ->columnSpanFull(), @@ -355,7 +435,13 @@ class PageForm ->seconds(false) ->live() ->afterStateUpdated(fn ($state, callable $set) => $set('value', $state)) - ->afterStateHydrated(fn ($component, $state, Get $get) => $component->state($get('value'))) + ->afterStateHydrated(function ($component, $state, Get $get) { + if ($get('type') !== 'datetime') return; + $value = $get('value'); + if ($value !== null && is_string($value)) { + $component->state($value); + } + }) ->dehydrated(false) ->columnSpanFull(), @@ -370,7 +456,13 @@ class PageForm ) ->live(onBlur: true) ->afterStateUpdated(fn ($state, callable $set) => $set('value', $state)) - ->afterStateHydrated(fn ($component, $state, Get $get) => $component->state($get('value'))) + ->afterStateHydrated(function ($component, $state, Get $get) { + if ($get('type') !== 'array') return; + $value = $get('value'); + if ($value !== null && is_array($value)) { + $component->state($value); + } + }) ->dehydrated(false) ->columnSpanFull(), ])