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

时间:2017-12-13 15:42:24

标签: octobercms octobercms-backend october-form-controller octobercms-widgets october-partial

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

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 个答案:

答案 0 :(得分:2)

您可以轻松地显示列表。

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

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

您需要将此代码放入插件的启动方法

// 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') ?>

如果它不起作用,请发表评论

答案 1 :(得分: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

相关问题