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

CanThis
CanThis

Hey there!

I have two controllers/models in my plugin - Cars and Items. Car hasMany items. Item belongTo Car. In my Cars backend list I've added column with 'Info' button to show detailed information of a Car in popup, calling action onShowInfo() of Cars controller.

Info button's partial:

    <a
         target="_blank"
         class="btn btn-default btn-sm btn-info oc-icon-info"
         data-control="popup"
         data-handler="onShowInfo"
         data-request-data="car_id: <?= $record['id'] ?>">
         Info
    </a>

onShowInfo action in Cars controller:

    public function onShowInfo() {
        if (post('car_id')) {
            $car = CarModel::with('manufacturer', 'auto', 'transmission_type', 'carcase', 'engine_type', 'engine_features', 'color')->find(post('car_id'));
        }

        return [
            'result' => $this->makePartial('car_info', ['var' => $car])
        ];
    }

It works nice. But let's say I have to call this action from my Items list or any other place? How can I achieve this without adding exactly the same action to another controllers? I have tried passing target controllers name to 'data-handler' attribute like this:

data-handler="Cars::onShowInfo"

but unfortunately it throws exception: "A widget with class name 'Cars' has not been bound to the controller" on line 512 of /modules/backend/classes/Controller.php"

I'm sorry for a noob question. Thanks in advance.

Last updated

philipptempel
philipptempel

I think what you want to do use create a widget - in this case neither a report nor a form widget but a simple 'generic widget'. You can find the info on that here in the docs

CanThis
CanThis

Thanks Philipp! I followed the docs you provided and created my "infoPopup" widget. Moved all necessary functions to it. Now I can call it like this from anywhere I want. Thanks!

data-handler="infoPopup::onShow"

1-3 of 3

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