cube-agency / filament-template
Template field for Filament
Requires
- php: ^8.2
- filament/forms: ^3.0
- illuminate/contracts: ^10.0|^11.0
- spatie/laravel-package-tools: ^1.15.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.10|^8.1
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^8.0|^9.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- spatie/laravel-ray: ^1.26
This package is auto-updated.
Last update: 2024-10-23 12:42:15 UTC
README
Template field for Filament, that gives an option to have different fields based on field value or type in one Resource.
Installation
You can install the package via composer:
composer require cube-agency/filament-template
You can publish the config file with:
php artisan vendor:publish --tag="filament-template-config"
Usage
Add "template" column to your Model/table
$table->string('template');
Create your template by using console command:
php artisan filament-template:create Blog
Add it to config
return [ 'templates' => [ \App\Filament\Templates\BlogTemplate::class, ] ];
At this moment, this could be used by passing url parameter (query string) to create form
class CreatePage extends CreateRecord { public $template; protected $queryString = ['template']; }
and adding this in your Resource
public static function form(Form $form): Form { return $form ->schema([ // ... Hidden::make('template') ->default($this->getTemplate()), Template::make('content') ->template($this->resolveTemplate()), ]); } protected function resolveTemplate() { return app($this->getTemplate()); } protected function getTemplate() { if (property_exists($this, 'template')) { return $this->template; } return $this->getRecord()->getAttribute('template'); }
Override create action to show modal with template select
public function getActions(): array { return [ Action::make('create') ->form($this->actionForm()) ->action(function (array $data): void { $parameters = http_build_query($data); $this->redirect(static::$resource::getUrl('create') . '?' . $parameters); }) ]; } protected function getTemplates(): Collection { return collect(config('filament-template.templates'))->mapWithKeys(function ($template) { $templateName = explode('\\', $template); return [$template => end($templateName)]; }); } protected function actionForm(): array { return [ Select::make('template') ->label('Template') ->options($this->getTemplates()) ->required(), ]; }
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.