cube-agency/filament-template

Filament 的模板字段

v1.0.2 2024-05-23 11:37 UTC

This package is auto-updated.

Last update: 2024-09-23 12:25:28 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Filament 的模板字段,可以在一个资源中根据字段值或类型选择不同的字段。

安装

您可以通过 composer 安装此包

composer require cube-agency/filament-template

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="filament-template-config"

使用方法

将 "template" 列添加到您的模型/表中

$table->string('template');

使用控制台命令创建您的模板

php artisan filament-template:create Blog

将其添加到配置中

return [
    'templates' => [
        \App\Filament\Templates\BlogTemplate::class,
    ]
];

目前,可以通过传递 URL 参数(查询字符串)来使用它创建表单

class CreatePage extends CreateRecord
{
    public $template;

    protected $queryString = ['template'];
}

并在您的资源中添加此内容

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');
}

覆盖创建操作以显示带有模板选择的模态框

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(),
    ];
}

测试

composer test

更新日志

请参阅 更新日志 了解最近的变化。

贡献

有关详细信息,请参阅 贡献指南

安全漏洞

请参阅 我们的安全策略 了解如何报告安全漏洞。

鸣谢

许可证

MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件