【问题标题】:OctoberCMS - Extend a plugin filterFields methodOctoberCMS - 扩展插件 filterFields 方法
【发布时间】:2017-07-14 16:53:46
【问题描述】:

我正在尝试扩展 RainLab 用户插件,并且需要过滤后端表单中的字段。

如果我直接编辑用户模型,我可以让它发挥作用,但我试图从我自己的插件注册文件中使用“addDynamicMethod”进行操作,但没有运气。 用户模型文件上的代码:

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;
        }
    }
}

【问题讨论】:

    标签: octobercms


    【解决方案1】:

    下面是我在一个自定义插件中为扩展我的后端用户插件所做的示例代码。您可以在自定义插件的 boot() 函数中添加以下逻辑。

    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                    
                ]
            ]);
        });
    }
    

    在上述功能中,我已禁用该字段以供用户更新。

    您可以以上面的代码为例,并根据您的要求使用它。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-24
      • 2019-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多