在area17/twill项目中自定义创建模态框字段指南

在area17/twill项目中自定义创建模态框字段指南

twill Twill is an open source CMS toolkit for Laravel that helps developers rapidly create a custom admin console that is intuitive, powerful and flexible. Chat with us on Discord at https://discord.gg/cnWk7EFv8R. twill 项目地址: https://gitcode.com/gh_mirrors/tw/twill

理解创建模态框的基本结构

在area17/twill项目中,创建模态框是管理员界面中用于添加新内容的核心组件。默认情况下,创建模态框包含标题(title)、别名(slug)字段以及一些发布设置选项。这种设计适用于大多数基础场景,但在实际开发中,我们经常需要根据业务需求添加额外的必填字段。

自定义创建模态框的两种方法

方法一:通过控制器FormBuilder方式

这是推荐的方式,通过在模块控制器中重写getCreateForm方法来实现:

public function getCreateForm(): Form
{
    return Form::make([
        Input::make()
            ->name('title')
            ->label('自定义标题')
            ->translatable()
            ->onChange('formatPermalink'),
        Wysiwyg::make()
            ->name('description')
            ->label('详细描述')
            ->translatable(),
        Input::make()
            ->name('slug')
            ->label('永久链接')
            ->translatable()
            ->ref('permalink')
            ->prefix($this->getPermalinkPrefix($this->getPermalinkBaseUrl())),
    ]);
}

关键点说明:

  1. translatable()方法表示该字段支持多语言
  2. onChange('formatPermalink')确保标题变更时自动更新别名
  3. 建议保留标题和别名字段,除非确实不需要

方法二:通过视图FormView方式

这种方式提供了更大的灵活性,可以通过创建create.blade.php文件来自定义:

@include('twill::partials.create')

<x-twill::input
    name="description"
    label="详细描述"
    :translated="true"
    :maxlength="100"
/>

高级自定义示例:

<x-twill::input
    :name="$titleFormKey ?? 'title'"
    :label="$titleFormKey === 'title' ? '标题' : ucfirst($titleFormKey)"
    :translated="$translateTitle ?? false"
    :required="true"
    on-change="formatPermalink"
/>

<x-twill::input
    name="description"
    label="详细描述"
    :translated="true"
    :maxlength="100"
/>

@if ($permalink ?? true)
    <x-twill::input
        name="slug"
        label="永久链接"
        :translated="true"
        ref="permalink"
        :prefix="$permalinkPrefix ?? ''"
    />
@endif

动态数据绑定技巧

当需要在下拉选择框中显示动态数据时,可以通过控制器的indexData方法提供数据:

protected function indexData($request)
{
    return [
        'example_options' => [
            ['value' => 'a', 'label' => '选项A'],
            ['value' => 'b', 'label' => '选项B'],
            ['value' => 'c', 'label' => '选项C'],
        ],
    ];
}

然后在视图中使用:

<x-twill::select
    name="example_options_field"
    label="示例选项"
    :unpack="true"
    :options="$example_options"
/>

移除发布开关的高级配置

在某些场景下,可能需要移除创建模态框中的发布开关和多语言选择框:

  1. 在控制器中配置:
protected function setUpController(): void
{
    $this->disablePublish();
    $this->disableBulkPublish();
}

protected function formData($request) 
{ 
    return ['controlLanguagesPublication' => false];
}
  1. 在仓库中设置所有语言默认发布:
public function prepareFieldsBeforeCreate($fields): array
{
    foreach ($fields['languages'] as $key => $language) {
        $fields['languages'][$key]['published'] = true;
    }
    return parent::prepareFieldsBeforeCreate($fields);
}

最佳实践建议

  1. 优先使用FormBuilder方式,保持与框架更新的一致性
  2. 复杂布局需求时再考虑FormView方式
  3. 动态数据尽量通过控制器提供,保持逻辑清晰
  4. 谨慎移除发布开关,确保符合业务需求
  5. 多语言字段务必添加translatable()方法

通过以上方法,您可以灵活地定制area17/twill项目中的创建模态框,满足各种业务场景的需求,同时保持代码的可维护性和扩展性。

twill Twill is an open source CMS toolkit for Laravel that helps developers rapidly create a custom admin console that is intuitive, powerful and flexible. Chat with us on Discord at https://discord.gg/cnWk7EFv8R. twill 项目地址: https://gitcode.com/gh_mirrors/tw/twill

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花淑云Nell

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值