Skip to content

Commit

Permalink
Simplify implementation and move form fields to static property
Browse files Browse the repository at this point in the history
  • Loading branch information
quintenbuis committed Jan 14, 2022
1 parent 723e8d9 commit 7fb81a3
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 90 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')
]
];
```

Expand All @@ -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)

Expand Down
7 changes: 3 additions & 4 deletions config/filament-settings.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?php

return [
// Group the menu item belongs to
'group' => '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')
]
];
8 changes: 0 additions & 8 deletions resources/views/components/values.blade.php

This file was deleted.

9 changes: 8 additions & 1 deletion resources/views/pages/settings.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
<x-filament::page>
@livewire(\Reworck\FilamentSettings\Components\RenderValues::class)
<form wire:submit.prevent="submit">

{{ $this->form }}

<x-tables::button type="submit" class="mt-2">
@lang('Save')
</x-tables::button>
</form>
</x-filament::page>
54 changes: 0 additions & 54 deletions src/Components/RenderValues.php

This file was deleted.

5 changes: 5 additions & 0 deletions src/FilamentSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@

class FilamentSettings
{
public static array $fields = [];

public static function setFormFields(array $fields): void
{
self::$fields = $fields;
}
}
14 changes: 0 additions & 14 deletions src/FilamentSettingsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
40 changes: 38 additions & 2 deletions src/Pages/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 7fb81a3

Please sign in to comment.