Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom schema #301

Merged
merged 16 commits into from
Jun 10, 2024
71 changes: 68 additions & 3 deletions docs/advanced/custom-schemata.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php

namespace App\Zeus\CustomSchema;

use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\TextInput;
use LaraZeus\Accordion\Forms\Accordion;
use LaraZeus\Bolt\Contracts\CustomSchema;
use LaraZeus\Bolt\Fields\FieldsContract;

class Field implements CustomSchema
{
public function make(?FieldsContract $field = null): Accordion
{
return Accordion::make('meta-data')
->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.

Expand All @@ -14,12 +79,12 @@ 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:

```php
\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.
20 changes: 15 additions & 5 deletions src/Concerns/Schemata.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -90,7 +90,8 @@ protected static function sectionOptionsFormSchema(array $formOptions, array $al
->label(__('compact section')),
]),
self::visibility($allSections),
]),
Bolt::getCustomSchema('section') ?? [],
])),
];
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ trait Configuration
*/
protected array $boltModels = [];

protected array $customSchema = [
'form' => null,
'section' => null,
'field' => null,
];

protected array $hideResources = [];

/**
Expand All @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions src/Contracts/CustomFormSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace LaraZeus\Bolt\Contracts;

use Filament\Forms\Components\Tabs\Tab;

interface CustomFormSchema
{
public function make(): Tab;
}
13 changes: 13 additions & 0 deletions src/Contracts/CustomSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace LaraZeus\Bolt\Contracts;

use LaraZeus\Accordion\Forms\Accordion;
use LaraZeus\Bolt\Fields\FieldsContract;

interface CustomSchema
{
public function make(?FieldsContract $field = null): Accordion;

public function hidden(?FieldsContract $field = null): array;
}
36 changes: 36 additions & 0 deletions src/Facades/Bolt.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
namespace LaraZeus\Bolt\Facades;

use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\Tabs\Tab;
use Filament\Support\Facades\FilamentView;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Facade;
use LaraZeus\Accordion\Forms\Accordion;
use LaraZeus\Bolt\BoltPlugin;
use LaraZeus\Bolt\Contracts\CustomFormSchema;
use LaraZeus\Bolt\Contracts\CustomSchema;
use LaraZeus\Bolt\Fields\FieldsContract;

class Bolt extends Facade
{
Expand Down Expand Up @@ -111,4 +117,34 @@ public static function hasPro(): bool
{
return class_exists(\LaraZeus\BoltPro\BoltProServiceProvider::class);
}

public static function getCustomSchema(string $hook, ?FieldsContract $field = null): Tab | Accordion | null
{
$class = BoltPlugin::getSchema($hook);
if ($class !== null) {
$getClass = new $class;
if ($hook === 'form' && $getClass instanceof CustomFormSchema) {
return $getClass->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;
}
}
2 changes: 2 additions & 0 deletions src/Fields/Classes/CheckboxList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) ?? [],
]),
];
}
Expand All @@ -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(),
Expand Down
3 changes: 3 additions & 0 deletions src/Fields/Classes/ColorPicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -51,13 +52,15 @@ public static function getOptions(?array $sections = null): array
]),
self::hintOptions(),
self::visibility($sections),
Bolt::getCustomSchema('field', resolve(static::class)) ?? [],
]),
];
}

public static function getOptionsHidden(): array
{
return [
...Bolt::getHiddenCustomSchema('field', resolve(static::class)) ?? [],
Hidden::make('options.colorType'),
self::hiddenHtmlID(),
self::hiddenHintOptions(),
Expand Down
2 changes: 2 additions & 0 deletions src/Fields/Classes/DatePicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) ?? [],
]),
];
}
Expand All @@ -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(),
Expand Down
2 changes: 2 additions & 0 deletions src/Fields/Classes/DateTimePicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) ?? [],
]),
];
}
Expand All @@ -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(),
Expand Down
2 changes: 2 additions & 0 deletions src/Fields/Classes/FileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ public static function getOptions(?array $sections = null): array
]),
self::hintOptions(),
self::visibility($sections),
Bolt::getCustomSchema('field', resolve(static::class)) ?? [],
]),
];
}

public static function getOptionsHidden(): array
{
return [
...Bolt::getHiddenCustomSchema('field', resolve(static::class)) ?? [],
self::hiddenHtmlID(),
self::hiddenHintOptions(),
self::hiddenRequired(),
Expand Down
5 changes: 0 additions & 5 deletions src/Fields/Classes/Paragraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -42,17 +41,13 @@ 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) : [],
]),
];
}

public static function getOptionsHidden(): array
{
return [
// @phpstan-ignore-next-line
Bolt::hasPro() ? \LaraZeus\BoltPro\Facades\GradeOptions::hidden() : [],
self::hiddenHintOptions(),
self::hiddenColumnSpanFull(),
];
Expand Down
2 changes: 2 additions & 0 deletions src/Fields/Classes/Radio.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) ?? [],
]),
];
}
Expand All @@ -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(),
Expand Down
Loading