octobercmsoctobercms-backendoctobercms-widgets

Octobercms: How can I make a repeater field jsonable because I am creating this repeater field into a different plugin


How can I make a repeater field jsonable because I am creating this repeater field into a different plugin and I have created my own plugin. for example: I want to add a repeater field in rainlab.user plugin model but I want to do this by my own plugin so updates on rainlab.user plugin won't affect my work. Thanks


Solution

  • You should read about Extending Plugins.

    1) First register the events in your custom plugin.php - Example

    2) Add the related fields to your migration file - Example - Make sure the field type is set to json or text : $table->json('field_name')->nullable();

    Let's say you want to add a Dogs repeater field to the User Model ;

    public function boot()
    {
    
        UserModel::extend(function($model)
        {
            $model->addJsonable([
                'dogs',
            ]);
        });
    
        UsersController::extendFormFields(function($form, $model, $context){
    
            if (!$model instanceof UserModel) {
                return;
            }
    
            $form->addTabFields([
                'dogs' => [
                    'label'      => 'My Dogs',
                    'type'       => 'repeater',
                    'form'       => [
                        'fields' => [
                            'breed' => [
                                'label' => 'Breed',
                                'type' => 'dropdown',
                                'options' => [
                                    'labrador' => "Labrador",
                                    'cocker'   => "Cocker Spaniel"
                                ]
                            ],
                            'name' => [
                                'label' => 'Name',
                                'type' => 'text',
                            ]
                        ],
                    ],
                ],
            ]);
    
        });
    
    }