From 579082c271845a58bab5c856423702ebcd9a83f4 Mon Sep 17 00:00:00 2001 From: carly Date: Tue, 13 Aug 2024 15:33:16 +0800 Subject: [PATCH] Add more config --- README.md | 34 ++++++++++++++++++++++++++------ src/FilamentFieldGroupPlugin.php | 24 ++++++++++++++++++---- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 6d09d9d..48533a9 100644 --- a/README.md +++ b/README.md @@ -63,18 +63,26 @@ return [ ## Usage -1. Enable the Field Group resource by setting `enabled` to `true` in the config file: +1. Add `FilamentFieldGroupPlugin` to you panel. +2. Enable the Field Group resource by setting `enabled` to `true` in the config file: ```php // config/filament-field-group.php return [ -'enabled' => true, -// ... other config options + 'enabled' => true, + // ... other config options ]; ``` +Or enable the plugin on `FilamentFieldGroupPlugin` +```php +use SolutionForest\FilamentFieldGroup\FilamentFieldGroupPlugin; + +$panel + ->plugin(FilamentFieldGroupPlugin::make()->enablePlugin()); +``` ![Filament Field Group](./docs-assets/images/initial-resource.png) -2. Create field groups and fields, for example: +3. Create field groups and fields, for example: - Navigate to the Field Group resource in your Filament admin panel. - Create a new field group (e.g., "User Basic Info"). @@ -83,7 +91,7 @@ return [ ![Create Field Group and Field](./docs-assets/images/add-field-2.png) ![Create Field Group and Field](./docs-assets/images/add-field-3.png) -3. Apply field groups to your form schema: +4. Apply field groups to your form schema: ```php use SolutionForest\FilamentFieldGroup\Facades\FilamentFieldGroup; @@ -99,7 +107,7 @@ public static function form(Form $form): Form } ``` ![Apply Field Group](./docs-assets/images/apply-field-group.png) - + ## Available Components Currently, this package provides the following components: @@ -114,6 +122,20 @@ Currently, this package provides the following components: More components can be added in the future. Feel free to submit a pull request if you have ideas for additional components! + +## Advanced Usage +### Custom Resources +You can call `overrideResources` on `FilamentFieldGroupPlugin` to replace original resource: +```php + +use SolutionForest\FilamentFieldGroup\FilamentFieldGroupPlugin; + +$panel + ->plugin(FilamentFieldGroupPlugin::make()->overrideResources([ + // your resource + ])); +``` + ## Testing ```bash diff --git a/src/FilamentFieldGroupPlugin.php b/src/FilamentFieldGroupPlugin.php index 96a7ab7..e2d3573 100644 --- a/src/FilamentFieldGroupPlugin.php +++ b/src/FilamentFieldGroupPlugin.php @@ -8,6 +8,9 @@ class FilamentFieldGroupPlugin implements Plugin { + protected ?array $overrideResources = null; + protected bool $enablePlugin = false; + public function getId(): string { return 'filament-field-group'; @@ -16,10 +19,9 @@ public function getId(): string public function register(Panel $panel): void { // Register the FieldGroup resource - if (config('filament-field-group.enabled', false)) { - $panel->resources([ - FieldGroupResource::class, - ]); + if (config('filament-field-group.enabled', false) || $this->enablePlugin) { + $resources = $this->overrideResources ?? [FieldGroupResource::class]; + $panel->resources($resources); } } @@ -39,4 +41,18 @@ public static function get(): static return $plugin; } + + public function overrideResources(array $resources): static + { + $this->overrideResources = $resources; + + return $this; + } + + public function enablePlugin(bool $enable = true): static + { + $this->enablePlugin = $enable; + + return $this; + } }