首页 文章

OctoberCMS将列表添加到选项卡字段

提问于
浏览
0

我想在新标签下为我的用户实现一个列表到后端控制器 .

https://ibb.co/fkAWFR(添加标签字段)

UsersController::extendFormFields(function($form, $model, $context){
            if (!$model instanceof UserModel)
                return;
            if (!$model->exists)
                return;
            $form->addTabFields([
                'activity' => [
                    'tab' => 'Activity',
                    'type'  => 'partial',
                    'path' => '$/acme/plugin/controllers/viewedjobs/_viewed_jobs.htm'
                ]
            ]);
        });

https://ibb.co/ktHdvR(包括此列表)

我的_viewed_jobs.htm部分看起来像这样:
listRender()?>
这会引发有关未初始化列表行为的错误 . 经过一番看,我找到了这些帖子:https://octobercms.com/forum/post/listcontroller-error-need-help

所以我补充道

$this->asExtension('ListController')->index()

到局部,现在它显示我的用户列表控制器 .

我想显示我的ViewedJobs控制器的列表 . 我也在这里观看了教程:https://octobercms.com/support/article/ob-21手动创建我的列表,但是,当我使用这段代码时,没有定义变量 .

我也尝试在Users插件下创建一个新的列表配置(我知道这不是一个最佳实践),但它抛出了有关找不到groups()方法的错误 .

2 回答

  • 2

    你可以轻松地显示列表 .

    我假设您正在使用rain-lab用户插件,而当前 UsersController 是rain lab的用户控制器

    并且您有 job 表,并且用户和作业表之间有mm关系

    你需要将这段代码放在插件的 boot 方法中

    // first we extend users model with jobs relation
    \RainLab\User\Models\User::extend(function($model) {
        $model->belongsToMany['jobs'] = [\Hardiksatasiya\Test\Models\Job::class, 'table' => 'hardiksatasiya_test_job_user'];
    });
    
    // we now extend users controller to add relation behavior
    // also add relational configuration
    // we are doing this with not destructive method
    // so our extend will play nice to other's extends
    \RainLab\User\Controllers\Users::extend(function($controller) {
        // Implement behavior if not already implemented
        if (!$controller->isClassExtendedWith('Backend.Behaviors.RelationController')) {
            $controller->implement[] = 'Backend.Behaviors.RelationController';
        }
    
        // Define property if not already defined
        if (!isset($controller->relationConfig)) {
            $controller->addDynamicProperty('relationConfig');
        }
    
        // Splice in configuration safely
        $myConfigPath = '$/hardiksatasiya/test/models/job/config_relation_for_users.yaml';
    
        $controller->relationConfig = $controller->mergeConfig(
            $controller->relationConfig,
            $myConfigPath
        );
    });
    
    // now your actual code for extending fields
    \RainLab\User\Controllers\Users::extendFormFields(function($form, $model, $context){
        if (!$model instanceof \RainLab\User\Models\User)
            return;
        if (!$model->exists)
            return;
        $form->addTabFields([
            'jobs' => [
                'tab' => 'Activity',
                'type'  => 'partial',
                'path' => '$/hardiksatasiya/test/controllers/job/_user_job_relation.htm'
            ]
        ]);
    });
    

    relation config => config_relation_for_users.yaml

    jobs:
      label: Jobs
      view:
        showCheckboxes: false
        toolbarButtons: false
        list: $/hardiksatasiya/test/models/job/columns.yaml
    

    relation partial => _user_job_relation.htm

    <?= $this->relationRender('jobs') ?>
    

    如果它不起作用那么请评论

  • 0

    我继续使用关系管理器做了一个工作OctoberCMS relations

    UsersController::extend(function($controller){         
            // Splice in configuration safely
            $myConfigPath = '$/acme/plugin/controllers/ControllerName/config_relation.yaml';
    
            $controller->relationConfig = $controller->mergeConfig(
                $controller->relationConfig,
                $myConfigPath
            );
        });
    

    然后我将部分_viewed_jobs.htm更新为 <?= $this->relationRender('viewedJobs') ?>

    我现在将列表显示为Completed list added to tab field for user controller

相关问题