October CMS resources and help articles

Simple and to the point. Optimized by the community.

Add a custom button to a relation manager

8
by alxy, last modified on August 12th, 2019

Sometimes you may want to add a custom button to a relation widget to execute a custom action or extend the function of the default buttons. You can easily do this by specifying a custom key in the toolbarButtons option of your config_relation.yaml:

# ===================================
#  Relation Behavior Config
# ===================================

items:
    label: Invoice Line Item
    view:
        list: $/acme/pay/models/invoiceitem/columns.yaml
        toolbarButtons: create|delete|custom # Add this
    manage:
        form: $/acme/pay/models/invoiceitem/fields.yaml
        recordsPerPage: 10

Next, create a new file _relation_button_custom.htm (the last part corresponds to the item in your configuration file) in your controller's partial directory (e. g. /plugins/acme/controllers/invoices/) with the following contents:

<button
        class="btn btn-sm btn-secondary"
        data-request="onDoSomething"
        data-stripe-load-indicator>
    Custom
</button>

In this case, the button will just call the handler onDoSomething of your controller. But you can also go more advanced and pass the selected (checked) items or any custom variables to the controller. You can even open a custom modal and render complex controls. A lot of the markup can be reused from the existing button partials.

Discussion

2 comments

0
PrestaSafe
Post on August 2nd, 2020 8:55 PM

Thanks for this trick, it's possible to extends that form a plugin and load the views (of the button) from another plugin ? Thanks

0
Slevin
Post on November 16th, 2021 8:03 AM

Hi, just to let you know that the "existing button partials" link is broken.

We use cookies to measure the performance of this website. Do you want to accept these cookies?