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.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user