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

                    $model->bindEvent('model.beforeCreate', function() use ($model) {
                    $model->bindEvent('model.beforeSave', function() use ($model) {

                $model->addDynamicMethod('getUserFriends', function() use ($model) {
                    if(!empty($model->clinic->id)) {   
                        $possibleFriends= \RainLab\User\Models\User::where('clinic_id', $model->clinic->id)->get();
                        foreach($possibleFriends as $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) {


                foreach ($remove_fields as $field) {


            /* 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)
                foreach ($remove_columns as $column) {