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.

This commit is contained in:
Ümit Tunç
2025-11-01 04:13:42 -03:00
parent 2a4b8bed42
commit dbce745eb6
@@ -565,17 +565,41 @@ class PageForm
// Mevcut header_data'yı al
$existingData = $get('header_data') ?? [];
// 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]) ||
$existingData[$key] === null ||
$existingData[$key] === '' ||
(is_array($existingData[$key]) && empty($existingData[$key]))) {
if(is_array($defaultValue)) {
$defaultValue = reset($defaultValue);
// 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 ($flattenedDefaults as $key => $defaultValue) {
// Dot notation ile nested değeri kontrol et
$keys = explode('.', $key);
$currentValue = $existingData;
// 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);
}
}