OctoberCMS:如何设置转发器小部件自动加载

时间:2018-02-09 01:51:34

标签: octobercms octobercms-plugins octobercms-widgets

可以在窗体打开时将转发器窗口小部件设置为自动加载吗?

它有最大项目属性,可以自动加载到最大项目编号

因此用户无需点击提示按钮

重新更新

这是我的fields.yaml

fields:
nowarta:
    label: 'No. Warta'
    oc.commentPosition: ''
    span: auto
    placeholder: 'format penulisan ''No. 34 Tahun XIX'''
    type: text
tanggal:
    label: Tanggal
    oc.commentPosition: ''
    mode: date
    format: 'd - F - Y'
    span: right
    type: datepicker
    ignoreTimezone: false
renungan:
    label: 'Renungan Mingguan'
    oc.commentPosition: ''
    span: full
    type: section
renungan[judul]:
    label: 'Judul Renungan'
    oc.commentPosition: ''
    span: storm
    type: text
    cssClass: 'col-sm-6 col-sm-push-0'
renungan[bahanbaca]:
    label: 'Bahan Bacaan'
    oc.commentPosition: ''
    span: storm
    type: text
    cssClass: col-sm-6
renungan[isi]:
    label: Renungan
    oc.commentPosition: ''
    span: storm
    cssClass: 'col-sm-12 col-sm-push-0'
    type: richeditor
    size: huge
tabs:
fields:
    temakebum:
        label: 'Kebaktian Umum'
        oc.commentPosition: ''
        span: full
        type: section
        tab: 'Kebaktian Umum & Komisi'
    temakebum[tema]:
        tab: 'Kebaktian Umum & Komisi'
        label: Tema
        oc.commentPosition: ''
        span: storm
        cssClass: col-sm-11
        type: text
    temakebum[bacaan]:
        label: 'Bahan Bacaan'
        oc.commentPosition: ''
        span: storm
        cssClass: col-sm-6
        type: text
        tab: 'Kebaktian Umum & Komisi'
    temakebum[pujian]:
        label: Pujian
        oc.commentPosition: ''
        span: storm
        cssClass: col-sm-6
        type: text
        tab: 'Kebaktian Umum & Komisi'
    kebum:
        label: ''
        oc.commentPosition: ''
        prompt: 'Tambah Data'
        maxItems: '3'
        span: full
        type: repeater
        tab: 'Kebaktian Umum & Komisi'
        form:
            fields:
                jeniskeb:
                    label: 'Jenis Kebaktian'
                    oc.commentPosition: ''
                    span: storm
                    cssClass: col-sm-4
                    type: dropdown
                    options: jenisKeb
                khotbah:
                    label: Pengkhotbah
                    oc.commentPosition: ''
                    span: storm
                    cssClass: col-sm-4
                    placeholder: ''
                    type: text

dd($ form-> fields)

array:6 [▼
  "nowarta" => array:5 [▶]
  "tanggal" => array:7 [▶]
  "renungan" => array:4 [▶]
  "renungan[judul]" => array:5 [▶]
  "renungan[bahanbaca]" => array:5 [▶]
  "renungan[isi]" => array:6 [▶]
]

这就是我在控制器中所做的就像你想要的那样......

class WartaRutin extends Controller
{
    public $implement = ['Backend\Behaviors\ListController','Backend\Behaviors\FormController','Backend\Behaviors\ReorderController'    ];

    public $listConfig = 'config_list.yaml';
    public $formConfig = 'config_form.yaml';
    public $reorderConfig = 'config_reorder.yaml';

    public function __construct()
    {
        parent::__construct();
        BackendMenu::setContext('Mismaiti.MyWarta', 'main-menu-item', 'side-menu-rutin');
    }

    public function formExtendFieldsBefore($form) {    
    // we are checking we are creating record or updating
    // or even we can use $form->context here but I used $model->id
    // if $form->context == 'update'
    // if $form->context == 'create'
        if(is_null($form->model->id)) {
            // create time
            $iteration = $form->fields['kebum']['maxItems'];

            if(is_numeric($iteration) && $iteration > 0) {
                $emptyFields = [];
                while($iteration > 0) {                        
                    $emptyFields[] = ['jeniskeb' => ''];
                    $iteration--;
                }
                $form->model->kebum = $emptyFields;
            }

        }

    }
}

当我尝试执行时它会返回错误 - 我已将所有repeater_data替换为kebum

  

"未定义的索引:kebum"在C:\ xampp \ htdocs \ mywarta \ plugins \ mismaiti \ mywarta \ controllers \ WartaRutin.php第30行

我在这里错过了一些东西..?

1 个答案:

答案 0 :(得分:2)

嗯,我们可以使用one trick,因为您可能只需要create time,或者可以在update time as well扩展它,

我使用了字段repeater_data,您可以将其替换为字段field

  

<强> fields.yaml

fields:

    ... other fields ...

    repeater_data:
        label: Repeater
        oc.commentPosition: ''
        prompt: 'Add new item'
        maxItems: '5'
        span: auto
        type: repeater
        form:
            fields:
                title:
                    label: Text
                    oc.commentPosition: ''
                    span: auto
                    type: text
                category:
                    label: Dropdown
                    oc.commentPosition: ''
                    options:
                        1: 'Item 1'
                        2: 'Item 2'
                    span: auto
                    type: dropdown
  

<强>控制器

controller内,您需要添加此method and code

public function formExtendFieldsBefore($form) {

    // we are checking we are creating record or updating
    // or even we can use $form->context here but I used $model->id
    // if $form->context == 'update'
    // if $form->context == 'create'
    if(is_null($form->model->id)) {

        // create time **logic**
        $iteration = $form->fields['repeater_data']['maxItems'];
        if(is_numeric($iteration) && $iteration > 0) {
            $emptyFields = [];
            while($iteration > 0) {

                // here you need to provide at least one field 
                // which you used in repeater I used `title`
                // and let it be empty or may be some default value 
                $emptyFields[] = ['title' => ''];
                $iteration--;
            }
            // dummy fields assignment to current form model
            $form->model->repeater_data = $emptyFields;
        }
        // **logic**

    }
    else {

        // if you need to adjust data in update time
        // you need to find difference and then need to add
        // additional dummy data as we are adding above
    }
}

2个方案 create timeupdate time

  

创建时间

当用户创建记录时,我们会添加dummy fields,因此转发器会将它们显示为already there,并使用maxItems property field so its fully dynamic来完成1}},现在用户无需按下该按钮。

  

更新时间(假设maxItems = 5)

现在2nd scenario我们有其他条件如果你希望你可以添加一些逻辑并在那里做你的东西,假设用户一次只能添加2条记录所以下次我们需要添加3 dummy fields (2填充+ 3个虚拟=总共5个作为maxItem),这样您就可以calculate thereadd it from else part

我希望这会对你有所帮助,如果你发现任何困难请发表评论。