Render RelationToolbar conditionally

Hello,

i have a relation to a model and the relationconfig.yaml is

blocks:
    label: Blocks
    deferredBinding: false
    showCheckboxes: false
    structure:
        showTree: false
        showReorder: true
        showSorting: true
    view:
        list: $/abc/typeBlocks/models/typeBlock/columns.yaml
        toolbarButtons: create|delete|addcomponent
    manage:
        form: $/abc/typeBlocks/models/typeBlock/fields.yaml
        recordsPerPage: 20

The toolbar depends on some conditions. How can i conditionally hide and show toolbar buttons.

Thanks alot

@daft any idea how can i achieve this?

Hi,

You could do it extending the controller. For example:

\abc\typeBlocks\controllers\typeBlocks::extend(function($controller) {

            if (!$controller instanceof \abc\typeBlocks\controllers\typeBlocks) {
                return;
            }

            $user = \BackendAuth::getUser();

             if ($user?->role?->code == 'admin') {
                $controller->formConfig = '$/abc/typeBlocks/controllers/typeBlocks/config_form_admin.yaml';
                $controller->listConfig = '$/abc/typeBlocks/controllers/typeBlocks/config_list_admin.yaml';
                $controller->relationConfig = '$/abc/typeBlocks/controllers/typeBlocks/config_relation_admin.yaml';
            } 
});

@apinard thanks alot. However i want to render the relation without toolbar.

view:
        list: $/abc/typeBlocks/models/typeBlock/columns.yaml
        toolbarButtons: false

Or try to modify it before it includes the toolbar partial (displayPage event? not sure).

@apinard yes that what i mean.

But question is that how can i render this conditionally.

Through the config_list, you can add or remove the toolbar buttons. You can tell the system what config_list to use doing what I said above.

toolbar:
    buttons: list_toolbar
    search:
        prompt: 'backend::lang.list.search_prompt'

To remove the toolbar buttons:

toolbar:
    search:
        prompt: 'backend::lang.list.search_prompt'
1 Like

@apinard ah okay. Yes that will work. Thanks alot