From dbce745eb69d9c46ad9ed8e8ec31fa04d93734fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Cmit=20Tun=C3=A7?= Date: Sat, 1 Nov 2025 04:13:42 -0300 Subject: [PATCH] Enhance default data handling in PageForm schema: Introduced a recursive function to flatten nested arrays for default data assignment, ensuring proper population of header data fields. This improvement streamlines the logic for filling in default values, enhancing form usability and data consistency. --- .../Resources/Pages/Schemas/PageForm.php | 44 ++++++++++++++----- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/app/Filament/Admin/Resources/Pages/Schemas/PageForm.php b/app/Filament/Admin/Resources/Pages/Schemas/PageForm.php index 1579050..daa2377 100644 --- a/app/Filament/Admin/Resources/Pages/Schemas/PageForm.php +++ b/app/Filament/Admin/Resources/Pages/Schemas/PageForm.php @@ -565,18 +565,42 @@ class PageForm // Mevcut header_data'yı al $existingData = $get('header_data') ?? []; + // Nested array'leri dot notation'a çeviren recursive fonksiyon + $flattenDefaultData = function ($array, $prefix = '') use (&$flattenDefaultData) { + $result = []; + foreach ($array as $key => $value) { + $newKey = $prefix ? "{$prefix}.{$key}" : $key; + if (is_array($value)) { + $result = array_merge($result, $flattenDefaultData($value, $newKey)); + } else { + $result[$newKey] = $value; + } + } + return $result; + }; + + // Default data'yı düzleştir + $flattenedDefaults = $flattenDefaultData($defaultData); + // Her default veri için boş olan alanları doldur - foreach ($defaultData as $key => $defaultValue) { - // Eğer bu alan boşsa veya yoksa, default değeri kullan - if (!isset($existingData[$key]) || + foreach ($flattenedDefaults as $key => $defaultValue) { + // Dot notation ile nested değeri kontrol et + $keys = explode('.', $key); + $currentValue = $existingData; - $existingData[$key] === null || - $existingData[$key] === '' || - (is_array($existingData[$key]) && empty($existingData[$key]))) { - if(is_array($defaultValue)) { - $defaultValue = reset($defaultValue); - } - $set("header_data.{$key}", $defaultValue); + // Nested değere eriş + foreach ($keys as $k) { + $currentValue = $currentValue[$k] ?? null; + if ($currentValue === null) { + break; + } + } + + // Eğer bu alan boşsa veya yoksa, default değeri kullan + if ($currentValue === null || + $currentValue === '' || + (is_array($currentValue) && empty($currentValue))) { + $set("header_data.{$key}", $defaultValue); } } })