This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

arall
arall

Hi there,

I'm trying to hide some columns and fields on my backend plugin. Here is what I have:

I've set my permission (acme.test.admin), and have some user groups without it. I've created /models/post/columns.yaml and /models/post/columns_limited.yaml. And finaly, I have /controllers/posts/config.list.yaml (poiting list parameter to /models/post/columns.yaml) and /controllers/posts/config_limited.list.yaml (poiting list parameter to /models/post/columns_limited.yaml).

What I'm trying to do is, on Posts controller, switch config files, like this:

public function __construct()
{
    // Permissions
    if (!$this->user->hasAccess('acme.test.admin')) {
        $this->listConfig = 'config_list_limited.yaml';
    }

    parent::__construct();

    BackendMenu::setContext('Acme.Test', 'test', 'posts');
}

I've checked the Backend\Classes\Controller code, and user is beign set on run method, not in __contruct.

How I can do this in best practice?

Last updated

arall
arall

I think I should use Extend list columns method.

omdreamreg6295
omdreamreg6295

Have you found a solution? I have the same problem. I need to disable field in back end form depending on permission.

omdreamreg6295
omdreamreg6295

I understand :)

Just override field options

public function formExtendFields($form)
    {
        if (!$this->user->hasAccess('acme.plugin.edit_limited')) {
            $form->addFields([
                'someFieldName' => [
                    'label' => 'SomeLabel',
                    'type' => 'text',
                    'hidden' => 'true'
                ],
            ]);
        }
    }

Last updated

ademin
ademin
'someFieldName'       => ['invisible' => 'true']

Last updated

Sebastiaan Kloos (ICTBelang)
Sebastiaan Kloos (ICTBelang)

You can change the path to the list in the variable in the controller. If you you make several forms, you can assign the path for the right permission.

Davox
Davox

In the columns.yaml file you must place only the columns that an user without permissions can view. Then, you can extend the column definitions in the Controller, for example:

public function listExtendColumns($list)
{
    if ($this->user->hasAccess('acme.plugin.permission')) {
        $list->addColumns([     
            'is_published' => [
              'label' =>  'Is published?',
              'type' =>  'switch',
            ],
        ]);
    }
}

If you need to filter the fields.yaml depending of the permissions you can use the filterFields() function in your Model. This example is from RainLab Blog plugin:

public function filterFields($fields, $context = null)
{
    if (!isset($fields->published, $fields->published_at)) {
        return;
    }

    $user = BackendAuth::getUser();

    if (!$user->hasAnyAccess(['rainlab.blog.access_publish'])) {
        $fields->published->hidden = true;
        $fields->published_at->hidden = true;
    }
    else {
        $fields->published->hidden = false;
        $fields->published_at->hidden = false;
    }
}

Last updated

tech.computeraid55120
tech.computeraid55120

While trying to do this from BackendAuth from the model, this worked for me. Notice that I've had to retweet BackendAuth basis permission from within the controller and that readOnly setting to '1' or '0' instead of true or false -

public function formExtendFields($form)
{
    if (!$this->user->hasAnyAccess(['daf.erp.manage_employee_tickets'])) {
        $form->addFields([
            'backenduser_id' => [
                'label' => 'daf.erp::lang.ticket.backenduser',
                'emptyOption' => '-- Select --',
                'showSearch' => 'false',
                'span' => 'auto',
                'required' => '1',
                'readOnly' => '1',
                'default' => BackendAuth::getUser()->id,
                'type' => 'dropdown'
            ],
        ]);
    }else {
        $form->addFields([
            'backenduser_id' => [
                'label' => 'daf.erp::lang.ticket.backenduser',
                'emptyOption' => '-- Select --',
                'showSearch' => 'true',
                'span' => 'auto',
                'required' => '1',
                'readOnly' => '0',
                'default' => BackendAuth::getUser()->id,
                'type' => 'dropdown'
            ],
        ]);
    }
}

1-8 of 8

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.