1
votes

I'm trying to extend the RainLab User plugin, and need to filter a field in the backend form.

If I edit the User model directly I get it to function, but I'm trying to do from my own Plugin registration file with "addDynamicMethod" without luck. Code on the User model file:

public function filterFields($fields, $context = null)
{
    if (property_exists($fields, 'usertype')) {

        $userType = $fields->usertype->value;

        if($userType == $this->AGENT || $userType == null) {
            $fields->agent->hidden = true;
        }
    }
}
1

1 Answers

1
votes

Below is the example code I did in one of my custom plugins to extend my Backend user plugin. You can put below kind of logic in your custom plugin's boot() function.

use Backend\Models\User as BackendUserModel;
public function boot()
{
    // Add Team field in user administartor form 
    BackendUsersController::extendFormFields(function($form, $model, $context){

        if (!$model instanceof BackendUserModel)
            return;

        $form->addTabFields([
            'team' => [
                'label'   => 'Team',
                'comment' => 'Associate this user with a team.',
                'type' => 'recordfinder',
                'list' => '$/technobrave/team/models/team/columns.yaml',
                'prompt' => 'Click the %s to find a team',
                'select' => 'id',
                'nameFrom'=> 'name',
                'tab' => 'Account',
                'disabled' => true                    
            ]
        ]);
    });
}

Here in above function, I have disabled the field to update for users.

You can take above code as an example and work around with it as per your requirements.

Hope this helps.