In my case , I extended the user plguin by Rainlab to add some fields in backend form and list. So how does it work ?

  • Firstly , We can simple extend the model you want by simply calling User::extend(function($model) function.
  • Inside this function , you can write everything that you would write while creating a model which may be adding a property or defining a relation.
  • If you need to add a function you can use $model->addDynamicMethod function.
  • Once your model is ready , you can starting adding fields in backend form and columns in backend list as follows. You can also remove the existing fields and columns
  • You can use YAMP parser if you like defining fields for form and list in a separate file , which is cleaner to do.

It's just that . Its amazing how we can modify existing plugin just simple as that. This is the reason i love OctoberCMS so much.

Quote Note : You must do these inside register function in Plugin.php file.

/**
     * Extend plugin Rainlab.User
     */
    private function bootRainlabUserExtend() {

        if (class_exists("\RainLab\User\Models\User")) {

            /* Get User Fields from yaml file */
            $configFile = __DIR__ . '/models/rainlabuser/fields.yaml';
            $profileFields = Yaml::parse(File::get($configFile));
            $profileTabFields=  array_slice($profileFields, -1, 1);
            $profileFields=array_slice($profileFields, 0, count($profileFields)-1);

            /* Alter user model */
            \RainLab\User\Models\User::extend(function($model) {

                $model->belongsTo['clinic']=['\Verdikt\Clinic\Models\Clinic', 'Key' => 'clinic_id','otherKey' => 'id'];
                $model->belongsTo['group']=['RainLab\User\Models\UserGroup'];
                $model->attachOne['signature']=['System\Models\File'];
                $model->attachOne['driver_license']=['System\Models\File'];

                $model->addFillable([
                    "group_id",
                    "is_account",
                    "is_superuser",
                    "clinic_by_user",
                    "is_offline_user",
                    "salutation",
                    "license_no",
                    "home_phone",
                    "work_phone",
                    "fax",
                    "mobile",
                    "is_newsletter_allowed",
                    "shipping_address_type",
                    "clinic_by_user",
                    "notes",
                    "created_by",
                    "updated_by",
                ]);

                $model->bindEvent('model.beforeValidate', function() use ($model) {
                    $model->rules['email']= 'required|between:6,255|email';
                    $model->rules['username']= 'required|between:6,255';
                });

                if(!empty(BackendAuth::getUser())){
                    $model->bindEvent('model.beforeCreate', function() use ($model) {
                        $model->created_by=BackendAuth::getUser()->id;
                    });
                    $model->bindEvent('model.beforeSave', function() use ($model) {
                        $model->updated_by=BackendAuth::getUser()->id;
                    });
                }

                $model->addDynamicMethod('getUserFriends', function() use ($model) {
                    if(!empty($model->clinic->id)) {   
                        $possibleFriends= \RainLab\User\Models\User::where('clinic_id', $model->clinic->id)->get();
                        $friends=array();
                        foreach($possibleFriends as $friend){
                            if($friend->id!=$model->id)
                                 array_push($friends,$friend);
                            }
                        return $friends;
                    } else {
                        return false;
                    }
                });

            });

            /* Alter form for user in backend */
            \RainLab\User\Controllers\Users::extendFormFields(function($widget) use ($profileFields,$profileTabFields) {

                // Prevent extending of related form instead of the intended User form
                if (!$widget->model instanceof \RainLab\User\Models\User) {
                    return;
                }

                $remove_fields=array(
                    'name',
                    'surname',
                    'send_invite',
                    'block_mail',
                    'password@create',
                    'password@update',
                    'password_confirmation',
                    'username',
                    'groups',
                );

                foreach ($remove_fields as $field) {
                    $widget->removeField($field);
                }
                $widget->addFields($profileFields);
                $widget->addTabFields($profileTabFields['tabs']['fields']);
                $widget->removeField('jkshop_contact_email');
                $widget->removeField('jkshop_contact_phone');

            });

            /* Alter coulmn for user in backend */
            $configFile = __DIR__ . '/models/rainlabuser/columns.yaml';
            $columns = Yaml::parse(File::get($configFile));
            \RainLab\User\Controllers\Users::extendListColumns(function($list, $model) use ($columns){

                if (!$model instanceof \RainLab\User\Models\User)
                    return;
                $remove_columns=array(
                    'created_at',
                );
                foreach ($remove_columns as $column) {
                    $list->removeColumn($column);
                }
                $list->addColumns($columns);

            });

        } 
    }