OctoberCMS Add List to Tab Field

1.5k Views Asked by At

I would like to implement a list into backend controller for my users under a new tab.

https://ibb.co/fkAWFR (Add Tab Field)


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 (include this list)

My _viewed_jobs.htm partial looks like this:

listRender() ?>

Which throws an error about list behavior not being initialized. After some looking I found these posts: https://octobercms.com/forum/post/listcontroller-error-need-help

So I added

$this->asExtension('ListController')->index()
to the partial and now it displays my user list controller.

I would like to display a list for my ViewedJobs controller. I also watched the tutorial here: https://octobercms.com/support/article/ob-21 to create my list manually, however, the variables are not defined when I use this code.

I also tried creating a new list config under the Users plugin (which I know is not a best practice) but it throws and error about groups() method not found.

2

There are 2 best solutions below

1
On BEST ANSWER

you can easily show lists.

I assume that you are using rain-lab user plugin and current UsersController is rain lab's user controller

and you have job table and have mm relation between user and job tabel

you need to put this code in your plugin's boot method

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

if its not working then please comment

0
On

I went ahead a did a work around using the relation manager 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
        );
    });

I then updated the partial _viewed_jobs.htm to <?= $this->relationRender('viewedJobs') ?>

I now have the list displayed as so Completed list added to tab field for user controller