diff --git a/README.md b/README.md index eb4724d..71673bd 100644 --- a/README.md +++ b/README.md @@ -13,11 +13,6 @@ return [ // Path to the file to be used as storage 'path' => storage_path('app/settings.json'), - - // The fields to be stored - 'fields' => [ - \Filament\Forms\Components\TextInput::make('title') - ] ]; ``` @@ -40,8 +35,12 @@ php artisan vendor:publish --tag=filament-settings-views ## Usage -To use this package fill in the `fields` array in the published config file. -When saving the form the contents will be stored inside the specified file. +Define your fields by adding the following in the `boot` method of your `AppServiceProvider` +```php +\Reworck\FilamentSettings\FilamentSettings::setFormFields([ + \Filament\Forms\Components\TextInput::make('title'), +]); +``` After that you can access your values as you usually would using [spatie/valuestore](https://github.com/spatie/valuestore) diff --git a/config/filament-settings.php b/config/filament-settings.php index 695babf..acc2da8 100644 --- a/config/filament-settings.php +++ b/config/filament-settings.php @@ -1,13 +1,12 @@ 'Settings', + // Sidebar label 'label' => 'Settings', + // Path to the file to be used as storage 'path' => storage_path('app/settings.json'), - - 'fields' => [ - \Filament\Forms\Components\TextInput::make('title') - ] ]; diff --git a/resources/views/components/values.blade.php b/resources/views/components/values.blade.php deleted file mode 100644 index d2c2b99..0000000 --- a/resources/views/components/values.blade.php +++ /dev/null @@ -1,8 +0,0 @@ -
- - {{ $this->form }} - - - Save - -
diff --git a/resources/views/pages/settings.blade.php b/resources/views/pages/settings.blade.php index 8a3cc04..0093783 100644 --- a/resources/views/pages/settings.blade.php +++ b/resources/views/pages/settings.blade.php @@ -1,3 +1,10 @@ - @livewire(\Reworck\FilamentSettings\Components\RenderValues::class) +
+ + {{ $this->form }} + + + @lang('Save') + +
diff --git a/src/Components/RenderValues.php b/src/Components/RenderValues.php deleted file mode 100644 index 102edce..0000000 --- a/src/Components/RenderValues.php +++ /dev/null @@ -1,54 +0,0 @@ -form->fill( - Valuestore::make( - $this->filePath() - )->all() - ); - } - - public function submit(): void - { - foreach ($this->data as $key => $data) { - Valuestore::make( - $this->filePath() - )->put($key, $data); - } - } - - public function render(): View - { - return view('filament-settings::components.values'); - } -} diff --git a/src/FilamentSettings.php b/src/FilamentSettings.php index 8fd1b59..1bdf673 100644 --- a/src/FilamentSettings.php +++ b/src/FilamentSettings.php @@ -4,5 +4,10 @@ class FilamentSettings { + public static array $fields = []; + public static function setFormFields(array $fields): void + { + self::$fields = $fields; + } } diff --git a/src/FilamentSettingsProvider.php b/src/FilamentSettingsProvider.php index b82f3f3..b328d91 100644 --- a/src/FilamentSettingsProvider.php +++ b/src/FilamentSettingsProvider.php @@ -18,18 +18,4 @@ protected function getPages(): array Settings::class, ]; } - - public function packageBooted(): void - { - parent::packageBooted(); - - Livewire::component(RenderValues::getName(), RenderValues::class); - } - - public function configurePackage(Package $package): void - { - parent::configurePackage($package); - - $package->hasConfigFile('filament-settings'); - } } diff --git a/src/Pages/Settings.php b/src/Pages/Settings.php index 39c698e..8de9620 100644 --- a/src/Pages/Settings.php +++ b/src/Pages/Settings.php @@ -2,14 +2,50 @@ namespace Reworck\FilamentSettings\Pages; +use Filament\Forms\Concerns\InteractsWithForms; +use Filament\Forms\Contracts\HasForms; use Filament\Pages\Page; +use Reworck\FilamentSettings\FilamentSettings; +use Spatie\Valuestore\Valuestore; -class Settings extends Page +class Settings extends Page implements HasForms { - protected static ?string $navigationIcon = 'heroicon-o-cog'; + use InteractsWithForms; + public array $data; + protected static ?string $navigationIcon = 'heroicon-o-cog'; protected static string $view = 'filament-settings::pages.settings'; + protected function getFormStatePath(): string + { + return 'data'; + } + + protected function getFormSchema(): array + { + return FilamentSettings::$fields; + } + + public function mount(): void + { + $this->form->fill( + Valuestore::make( + config('filament-settings.path') + )->all() + ); + } + + public function submit(): void + { + foreach ($this->data as $key => $data) { + Valuestore::make( + config('filament-settings.path') + )->put($key, $data); + } + + $this->notify('success', 'Saved!'); + } + protected static function getNavigationGroup(): ?string { return config('filament-settings.group');