diff --git a/docs/advanced/custom-schemata.md b/docs/advanced/custom-schemata.md index 0d949e8a..62c8a093 100644 --- a/docs/advanced/custom-schemata.md +++ b/docs/advanced/custom-schemata.md @@ -3,7 +3,72 @@ title: Custom Schemata weight: 6 --- -## Use Custom Schemata +## Add Custom Schema for fields + +Bolt allows you to add custom components (additional metadata) to the form, sections, and fields. + +### First: create the class: + +Create a class wherever you want in your app for example in `App\Zeus\CustomSchema` with the content: + +```php +schema([ + TextInput::make('options.meta.data_binding') + ->label('Data Binding'), + ]); + } + + public function hidden(?FieldsContract $field = null): array + { + return [ + Hidden::make('options.meta.data_binding'), + ]; + } +} +``` + +- make sure to return the hidden fields the same as the fields you have defined in the `make` method +- make sure to set the `state` correctly, if you want to store this info in the `options` then use `options. more data, or in a separate column then make sure to create the migration for it + +### Second: add it to your panel config: + +```php +BoltPlugin::make() + ->customSchema([ + 'form' => null, + 'section' => null, + 'field' => \App\Zeus\CustomSchema\Field::class, + ]) +``` + +### Third: catch it on the `FormSent` event, for example: + +```php +$event->response->fieldsResponses->each(function($fieldResponse){ + CustomeModel::create([ + 'column' => $fieldResponse->field->options['meta']['data_binding'] + ]); +}); +``` + +_**Warning: This is a simplified example, so don't trust your user input explicitly. That could open some pretty serious security holes.** + +## Replace the Schemata with Custom one the trait `Schemata` is the heart of the form builder, and now you can customize it to your liking. @@ -14,7 +79,7 @@ the trait `Schemata` is the heart of the form builder, and now you can customize copy the trait from `\LaraZeus\Bolt\Concerns` to your app, let say: `\App\Zeus\Bolt\Concerns` -### call the trait in a service provider +### Call the trait in a service provider in your register method of your `AppServiceProvider` add the following: @@ -22,4 +87,4 @@ in your register method of your `AppServiceProvider` add the following: \LaraZeus\Bolt\Livewire\FillForms::getBoltFormDesignerUsing(\App\Zeus\Bolt\Concerns\Designer::class); ``` -You're done. Customize the form builder to fit your needs. Remember to keep an eye on any changes in future updates so that you will avoid breaking changes. +You're done. Customize the form builder to fit your needs. Remember to keep an eye on any changes in future updates so that you will avoid breaking changes. \ No newline at end of file diff --git a/src/Concerns/Schemata.php b/src/Concerns/Schemata.php index 8d494f93..57d44479 100644 --- a/src/Concerns/Schemata.php +++ b/src/Concerns/Schemata.php @@ -61,7 +61,7 @@ protected static function sectionOptionsFormSchema(array $formOptions, array $al ->label(__('Section Description')), Accordions::make('section-options') - ->accordions([ + ->accordions(fn () => array_filter([ Accordion::make('visual-options') ->label(__('Visual Options')) ->columns() @@ -90,7 +90,8 @@ protected static function sectionOptionsFormSchema(array $formOptions, array $al ->label(__('compact section')), ]), self::visibility($allSections), - ]), + Bolt::getCustomSchema('section') ?? [], + ])), ]; } @@ -149,7 +150,7 @@ public static function getMainFormSchema(): array public static function getTabsSchema(): array { - return [ + $tabs = [ Tabs\Tab::make('title-slug-tab') ->label(__('Title & Slug')) ->columns() @@ -339,11 +340,19 @@ public static function getTabsSchema(): array ->label(__('Cover')), ]), ]; + + $customSchema = Bolt::getCustomSchema('form'); + + if ($customSchema !== null) { + $tabs[] = $customSchema; + } + + return $tabs; } public static function getSectionsSchema(): array { - return [ + return array_filter([ TextInput::make('name') ->columnSpanFull() ->required() @@ -438,7 +447,8 @@ public static function getSectionsSchema(): array Hidden::make('options.visibility.active')->default(0)->nullable(), Hidden::make('options.visibility.fieldID')->nullable(), Hidden::make('options.visibility.values')->nullable(), - ]; + ...Bolt::getHiddenCustomSchema('section') ?? [], + ]); } public static function getCleanOptionString(array $field): string diff --git a/src/Configuration.php b/src/Configuration.php index 3b0219ce..a123f13b 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -12,6 +12,12 @@ trait Configuration */ protected array $boltModels = []; + protected array $customSchema = [ + 'form' => null, + 'section' => null, + 'field' => null, + ]; + protected array $hideResources = []; /** @@ -30,6 +36,23 @@ trait Configuration protected array $showNavigationBadgesArray = []; + public function customSchema(array $schema): static + { + $this->customSchema = $schema; + + return $this; + } + + public function getCustomSchema(): array + { + return $this->customSchema; + } + + public static function getSchema(string $type): ?string + { + return (new static())::get()->getCustomSchema()[$type]; + } + public function boltModels(array $models): static { $this->boltModels = $models; diff --git a/src/Contracts/CustomFormSchema.php b/src/Contracts/CustomFormSchema.php new file mode 100644 index 00000000..5c33ba26 --- /dev/null +++ b/src/Contracts/CustomFormSchema.php @@ -0,0 +1,10 @@ +make(); + } + + if ($getClass instanceof CustomSchema) { + return $getClass->make($field); + } + } + + return null; + } + + public static function getHiddenCustomSchema(string $hook, ?FieldsContract $field = null): ?array + { + $class = BoltPlugin::getSchema($hook); + if ($class !== null) { + $getClass = new $class; + if ($getClass instanceof CustomSchema) { + return $getClass->hidden($field); + } + } + + return null; + } } diff --git a/src/Fields/Classes/CheckboxList.php b/src/Fields/Classes/CheckboxList.php index 545a2e42..c229eb95 100644 --- a/src/Fields/Classes/CheckboxList.php +++ b/src/Fields/Classes/CheckboxList.php @@ -49,6 +49,7 @@ public static function getOptions(?array $sections = null, ?array $field = null) self::visibility($sections), // @phpstan-ignore-next-line ...Bolt::hasPro() ? \LaraZeus\BoltPro\Facades\GradeOptions::schema($field) : [], + Bolt::getCustomSchema('field', resolve(static::class)) ?? [], ]), ]; } @@ -58,6 +59,7 @@ public static function getOptionsHidden(): array return [ // @phpstan-ignore-next-line Bolt::hasPro() ? \LaraZeus\BoltPro\Facades\GradeOptions::hidden() : [], + ...Bolt::getHiddenCustomSchema('field', resolve(static::class)) ?? [], self::hiddenDataSource(), self::hiddenVisibility(), self::hiddenHtmlID(), diff --git a/src/Fields/Classes/ColorPicker.php b/src/Fields/Classes/ColorPicker.php index 2c4f659a..1c4d5fba 100644 --- a/src/Fields/Classes/ColorPicker.php +++ b/src/Fields/Classes/ColorPicker.php @@ -6,6 +6,7 @@ use Filament\Forms\Components\Hidden; use LaraZeus\Accordion\Forms\Accordion; use LaraZeus\Accordion\Forms\Accordions; +use LaraZeus\Bolt\Facades\Bolt; use LaraZeus\Bolt\Fields\FieldsContract; class ColorPicker extends FieldsContract @@ -51,6 +52,7 @@ public static function getOptions(?array $sections = null): array ]), self::hintOptions(), self::visibility($sections), + Bolt::getCustomSchema('field', resolve(static::class)) ?? [], ]), ]; } @@ -58,6 +60,7 @@ public static function getOptions(?array $sections = null): array public static function getOptionsHidden(): array { return [ + ...Bolt::getHiddenCustomSchema('field', resolve(static::class)) ?? [], Hidden::make('options.colorType'), self::hiddenHtmlID(), self::hiddenHintOptions(), diff --git a/src/Fields/Classes/DatePicker.php b/src/Fields/Classes/DatePicker.php index ce94f93f..21f9e4f4 100644 --- a/src/Fields/Classes/DatePicker.php +++ b/src/Fields/Classes/DatePicker.php @@ -45,6 +45,7 @@ public static function getOptions(?array $sections = null, ?array $field = null) self::visibility($sections), // @phpstan-ignore-next-line ...Bolt::hasPro() ? \LaraZeus\BoltPro\Facades\GradeOptions::schema($field) : [], + Bolt::getCustomSchema('field', resolve(static::class)) ?? [], ]), ]; } @@ -54,6 +55,7 @@ public static function getOptionsHidden(): array return [ // @phpstan-ignore-next-line Bolt::hasPro() ? \LaraZeus\BoltPro\Facades\GradeOptions::hidden() : [], + ...Bolt::getHiddenCustomSchema('field', resolve(static::class)) ?? [], self::hiddenHtmlID(), self::hiddenHintOptions(), self::hiddenRequired(), diff --git a/src/Fields/Classes/DateTimePicker.php b/src/Fields/Classes/DateTimePicker.php index e2fa2249..b103cd80 100644 --- a/src/Fields/Classes/DateTimePicker.php +++ b/src/Fields/Classes/DateTimePicker.php @@ -45,6 +45,7 @@ public static function getOptions(?array $sections = null, ?array $field = null) self::visibility($sections), // @phpstan-ignore-next-line ...Bolt::hasPro() ? \LaraZeus\BoltPro\Facades\GradeOptions::schema($field) : [], + Bolt::getCustomSchema('field', resolve(static::class)) ?? [], ]), ]; } @@ -54,6 +55,7 @@ public static function getOptionsHidden(): array return [ // @phpstan-ignore-next-line Bolt::hasPro() ? \LaraZeus\BoltPro\Facades\GradeOptions::hidden() : [], + ...Bolt::getHiddenCustomSchema('field', resolve(static::class)) ?? [], self::hiddenHtmlID(), self::hiddenHintOptions(), self::hiddenRequired(), diff --git a/src/Fields/Classes/FileUpload.php b/src/Fields/Classes/FileUpload.php index 4a3ce4cd..804b7e39 100644 --- a/src/Fields/Classes/FileUpload.php +++ b/src/Fields/Classes/FileUpload.php @@ -47,6 +47,7 @@ public static function getOptions(?array $sections = null): array ]), self::hintOptions(), self::visibility($sections), + Bolt::getCustomSchema('field', resolve(static::class)) ?? [], ]), ]; } @@ -54,6 +55,7 @@ public static function getOptions(?array $sections = null): array public static function getOptionsHidden(): array { return [ + ...Bolt::getHiddenCustomSchema('field', resolve(static::class)) ?? [], self::hiddenHtmlID(), self::hiddenHintOptions(), self::hiddenRequired(), diff --git a/src/Fields/Classes/Paragraph.php b/src/Fields/Classes/Paragraph.php index cfc96f61..44732198 100644 --- a/src/Fields/Classes/Paragraph.php +++ b/src/Fields/Classes/Paragraph.php @@ -6,7 +6,6 @@ use Illuminate\Support\HtmlString; use LaraZeus\Accordion\Forms\Accordion; use LaraZeus\Accordion\Forms\Accordions; -use LaraZeus\Bolt\Facades\Bolt; use LaraZeus\Bolt\Fields\FieldsContract; class Paragraph extends FieldsContract @@ -42,8 +41,6 @@ public static function getOptions(?array $sections = null, ?array $field = null) self::columnSpanFull(), self::hintOptions(), ]), - // @phpstan-ignore-next-line - ...Bolt::hasPro() ? \LaraZeus\BoltPro\Facades\GradeOptions::schema($field) : [], ]), ]; } @@ -51,8 +48,6 @@ public static function getOptions(?array $sections = null, ?array $field = null) public static function getOptionsHidden(): array { return [ - // @phpstan-ignore-next-line - Bolt::hasPro() ? \LaraZeus\BoltPro\Facades\GradeOptions::hidden() : [], self::hiddenHintOptions(), self::hiddenColumnSpanFull(), ]; diff --git a/src/Fields/Classes/Radio.php b/src/Fields/Classes/Radio.php index b33be470..ca01f974 100644 --- a/src/Fields/Classes/Radio.php +++ b/src/Fields/Classes/Radio.php @@ -51,6 +51,7 @@ public static function getOptions(?array $sections = null, ?array $field = null) self::visibility($sections), // @phpstan-ignore-next-line ...Bolt::hasPro() ? \LaraZeus\BoltPro\Facades\GradeOptions::schema($field) : [], + Bolt::getCustomSchema('field', resolve(static::class)) ?? [], ]), ]; } @@ -60,6 +61,7 @@ public static function getOptionsHidden(): array return [ // @phpstan-ignore-next-line Bolt::hasPro() ? \LaraZeus\BoltPro\Facades\GradeOptions::hidden() : [], + ...Bolt::getHiddenCustomSchema('field', resolve(static::class)) ?? [], self::hiddenVisibility(), self::hiddenHtmlID(), self::hiddenHintOptions(), diff --git a/src/Fields/Classes/RichEditor.php b/src/Fields/Classes/RichEditor.php index 0fc41ddd..a9b5627d 100644 --- a/src/Fields/Classes/RichEditor.php +++ b/src/Fields/Classes/RichEditor.php @@ -45,6 +45,7 @@ public static function getOptions(?array $sections = null, ?array $field = null) self::visibility($sections), // @phpstan-ignore-next-line ...Bolt::hasPro() ? \LaraZeus\BoltPro\Facades\GradeOptions::schema($field) : [], + Bolt::getCustomSchema('field', resolve(static::class)) ?? [], ]), ]; } @@ -54,6 +55,7 @@ public static function getOptionsHidden(): array return [ // @phpstan-ignore-next-line Bolt::hasPro() ? \LaraZeus\BoltPro\Facades\GradeOptions::hidden() : [], + ...Bolt::getHiddenCustomSchema('field', resolve(static::class)) ?? [], self::hiddenVisibility(), self::hiddenHtmlID(), self::hiddenHintOptions(), diff --git a/src/Fields/Classes/Select.php b/src/Fields/Classes/Select.php index f37216a6..f50d458f 100644 --- a/src/Fields/Classes/Select.php +++ b/src/Fields/Classes/Select.php @@ -53,6 +53,7 @@ public static function getOptions(?array $sections = null, ?array $field = null) self::visibility($sections), // @phpstan-ignore-next-line ...Bolt::hasPro() ? \LaraZeus\BoltPro\Facades\GradeOptions::schema($field) : [], + Bolt::getCustomSchema('field', resolve(static::class)) ?? [], ]), ]; } @@ -62,6 +63,7 @@ public static function getOptionsHidden(): array return [ // @phpstan-ignore-next-line Bolt::hasPro() ? \LaraZeus\BoltPro\Facades\GradeOptions::hidden() : [], + ...Bolt::getHiddenCustomSchema('field', resolve(static::class)) ?? [], self::hiddenVisibility(), self::hiddenHtmlID(), self::hiddenHintOptions(), diff --git a/src/Fields/Classes/TextInput.php b/src/Fields/Classes/TextInput.php index 1fb0ebbe..1c983829 100644 --- a/src/Fields/Classes/TextInput.php +++ b/src/Fields/Classes/TextInput.php @@ -116,6 +116,7 @@ public static function getOptions(?array $sections = null, ?array $field = null) self::visibility($sections), // @phpstan-ignore-next-line ...Bolt::hasPro() ? \LaraZeus\BoltPro\Facades\GradeOptions::schema($field) : [], + Bolt::getCustomSchema('field', resolve(static::class)) ?? [], ]), ]; } @@ -125,6 +126,7 @@ public static function getOptionsHidden(): array return [ // @phpstan-ignore-next-line Bolt::hasPro() ? \LaraZeus\BoltPro\Facades\GradeOptions::hidden() : [], + ...Bolt::getHiddenCustomSchema('field', resolve(static::class)) ?? [], self::hiddenVisibility(), self::hiddenHtmlID(), self::hiddenHintOptions(), diff --git a/src/Fields/Classes/Textarea.php b/src/Fields/Classes/Textarea.php index b2608403..b50b4a8e 100644 --- a/src/Fields/Classes/Textarea.php +++ b/src/Fields/Classes/Textarea.php @@ -67,6 +67,7 @@ public static function getOptions(?array $sections = null, ?array $field = null) self::visibility($sections), // @phpstan-ignore-next-line ...Bolt::hasPro() ? \LaraZeus\BoltPro\Facades\GradeOptions::schema($field) : [], + Bolt::getCustomSchema('field', resolve(static::class)) ?? [], ]), ]; } @@ -76,6 +77,7 @@ public static function getOptionsHidden(): array return [ // @phpstan-ignore-next-line Bolt::hasPro() ? \LaraZeus\BoltPro\Facades\GradeOptions::hidden() : [], + ...Bolt::getHiddenCustomSchema('field', resolve(static::class)) ?? [], self::hiddenVisibility(), self::hiddenHtmlID(), self::hiddenHintOptions(), diff --git a/src/Fields/Classes/TimePicker.php b/src/Fields/Classes/TimePicker.php index b88ca40c..d2cba443 100644 --- a/src/Fields/Classes/TimePicker.php +++ b/src/Fields/Classes/TimePicker.php @@ -45,6 +45,7 @@ public static function getOptions(?array $sections = null, ?array $field = null) self::visibility($sections), // @phpstan-ignore-next-line ...Bolt::hasPro() ? \LaraZeus\BoltPro\Facades\GradeOptions::schema($field) : [], + Bolt::getCustomSchema('field', resolve(static::class)) ?? [], ]), ]; } @@ -54,6 +55,7 @@ public static function getOptionsHidden(): array return [ // @phpstan-ignore-next-line Bolt::hasPro() ? \LaraZeus\BoltPro\Facades\GradeOptions::hidden() : [], + ...Bolt::getHiddenCustomSchema('field', resolve(static::class)) ?? [], self::hiddenVisibility(), self::hiddenHtmlID(), self::hiddenHintOptions(), diff --git a/src/Fields/Classes/Toggle.php b/src/Fields/Classes/Toggle.php index 54cd65d3..c31f9684 100644 --- a/src/Fields/Classes/Toggle.php +++ b/src/Fields/Classes/Toggle.php @@ -79,6 +79,7 @@ public static function getOptions(?array $sections = null, ?array $field = null) self::visibility($sections), // @phpstan-ignore-next-line ...Bolt::hasPro() ? \LaraZeus\BoltPro\Facades\GradeOptions::schema($field) : [], + Bolt::getCustomSchema('field', resolve(static::class)) ?? [], ]), ]; } @@ -88,6 +89,7 @@ public static function getOptionsHidden(): array return [ // @phpstan-ignore-next-line Bolt::hasPro() ? \LaraZeus\BoltPro\Facades\GradeOptions::hidden() : [], + ...Bolt::getHiddenCustomSchema('field', resolve(static::class)) ?? [], self::hiddenVisibility(), self::hiddenHtmlID(), self::hiddenHintOptions(),