Add file type support in settings: Updated SettingForm to include a file upload field for settings, modified the Setting model to handle file values, and added a migration to accommodate the new file type. Enhanced localization files to support the new file type label in both English and Turkish.
This commit is contained in:
@@ -6,6 +6,7 @@ use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Forms\Components\FileUpload;
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Schemas\Components\Utilities\Get;
|
||||
use Filament\Schemas\Components\Utilities\Set;
|
||||
@@ -54,6 +55,7 @@ class SettingForm
|
||||
'float' => __('settings.type_float'),
|
||||
'array' => __('settings.type_array'),
|
||||
'json' => __('settings.type_json'),
|
||||
'file' => __('settings.type_file'),
|
||||
])
|
||||
->live()
|
||||
->afterStateUpdated(fn (Set $set) => $set('value', null)),
|
||||
@@ -78,10 +80,10 @@ class SettingForm
|
||||
->label(__('settings.value'))
|
||||
->helperText(__('settings.value_helper'))
|
||||
->required()
|
||||
->visible(fn (Get $get) => !in_array($get('type'), ['boolean']))
|
||||
->visible(fn (Get $get) => !in_array($get('type'), ['boolean', 'file']))
|
||||
->rows(fn (Get $get) => $get('type') === 'text' ? 5 : 3)
|
||||
->formatStateUsing(function ($state, $record) {
|
||||
if ($record && $record->type !== 'boolean') {
|
||||
if ($record && $record->type !== 'boolean' && $record->type !== 'file') {
|
||||
return $record->value;
|
||||
}
|
||||
return $state;
|
||||
@@ -101,6 +103,23 @@ class SettingForm
|
||||
})
|
||||
->columnSpanFull(),
|
||||
|
||||
FileUpload::make('value_file')
|
||||
->label(__('settings.value'))
|
||||
->helperText(__('settings.value_helper'))
|
||||
->required()
|
||||
->visible(fn (Get $get) => $get('type') === 'file')
|
||||
->disk('public')
|
||||
->directory('settings')
|
||||
->acceptedFileTypes(['image/*', 'application/pdf', 'text/*'])
|
||||
->maxSize(10240) // 10MB
|
||||
->formatStateUsing(function ($state, $record) {
|
||||
if ($record && $record->type === 'file') {
|
||||
return $record->value;
|
||||
}
|
||||
return $state;
|
||||
})
|
||||
->columnSpanFull(),
|
||||
|
||||
Toggle::make('is_active')
|
||||
->label(__('settings.is_active'))
|
||||
->helperText(__('settings.is_active_helper'))
|
||||
|
||||
@@ -15,6 +15,7 @@ class Setting extends Model
|
||||
'value',
|
||||
'value_text',
|
||||
'value_boolean',
|
||||
'value_file',
|
||||
'type',
|
||||
'group',
|
||||
'label',
|
||||
@@ -50,6 +51,14 @@ class Setting extends Model
|
||||
$this->attributes['value'] = $value ? '1' : '0';
|
||||
}
|
||||
|
||||
/**
|
||||
* Set value from different field names
|
||||
*/
|
||||
public function setValueFileAttribute($value)
|
||||
{
|
||||
$this->attributes['value'] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get setting by key
|
||||
*/
|
||||
@@ -91,6 +100,7 @@ class Setting extends Model
|
||||
'integer' => (int) $value,
|
||||
'float' => (float) $value,
|
||||
'array', 'json' => json_decode($value, true),
|
||||
'file' => $value, // File path as string
|
||||
default => $value,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user