Link to another Backend View with custom filter settings

I try to make a button/link which links to another backend list with some custom filter settings.

So: There’s Model A and Model B and the list of Model B can be filtered if it’s related to a certain entry of Model A. So there’s an ID filter for Model A in the Model B list.

Now I would like to create a button/link in the list of Model A, which redirect to the list of Model B with the filter setup to the corresponding ID.

So far, I tryed to do this with the Data Handler API:
<a data-request="onChangeBackendView" data-request-data="id:<?= $record->id; ?>">

Within this Handler, I retreived the ID from the send data and tried to forward to the list of Model B with the parameters for the filters, but I had no success this way.

    public function onChangeBackendView()
    {
        $id = Input::get('id');
        $parameters = [
            'scopeName' => 'modela_id',
            'Filter[condition]' => "equals",
            'Filter[value]' => "123",
        ];
        return Redirect::to(Backend::url('mch/modela/modelacontroller'))->withInput($parameters);
    }

It didnt work out this way, so I tried to handle this directly via Data Handler API:

    <a 
        data-request="onChangeBackendView" 
        data-request-data="scopeName:'modela_id', Filter[condition]:'equals', Filter[value]:<?= $record->id; ?>"
        data-request-url="/backend/mch/modelb/modelbcontroller"
        data-request-redirect="/backend/mch/modelb/modelbcontroller"
    >

I guess the data-request attribute isnt optional since the link didnt work without it. I tried to pass the parameters which is send from the filters directly to the list of model B, but it also didnt work out.

So my question is: What’s wrong with my approaches? I guess I missed out a few things.

In case you see some issues with naming handlers or urls: I renamed a lot of stuff for this thread so it’s easier to understand which controller / handler I tried to call. I didnt have any problems with the forwarding / reaching a handler within my tests. Just the parameters for the filter were not applied.

Any help is welcome, thanks a lot. ^^

The data will indeed not be passed with data-request-redirect, however, the onChangeBackendView looks good. Don’t forget to use Input::old() to fetch the withInput() data.

// First page
Redirect::to('..')->withInput($data);

// Second page
$data = Input::old();

Thanks for pushing me in the right direction. ^^

So I went on with the Handler approach:

Link:
<a data-request="onChangeBackendView" data-request-data="id:<?= $record->id; ?>">

Handler in Model A:

    public function onChangeBackendView()
    {
        $id = Input::get('id');
        $parameters = [
            'condition' => "equals",
            'value' => $id,
        ];
        return Redirect::to(Backend::url('mch/plugin/controllerB'))->withInput($parameters);
    }

In order to apply the filter settings within the backend view of Model B, I used the method listFilterExtendScopes() within the ControllerB:

    public function listFilterExtendScopes(Filter $filter)
    {
        $data = Input::old();
        if (empty($data) && isset($data['condition']) && isset($data['value'])) return;
        $filter->putScopeValue('filter_name', $data);
    }

For those who try to achieve the same goal:
With $filter->getScopes());, you can get all Scopes and with $filter->getScopeValue('filter_name')); you can get the array with the values of a certain scope.

Here’re the doc pages:
https://octobercms.com/docs/api/backend/widgets/filter
https://octobercms.com/docs/api/backend/classes/filterscope

BTW: I think the Documentation is missing the part about putScopeValue(), I only find setScopeValue() which is flagged as deprecated, but works the same way.

So this is my solution. Any improvement suggestions? Thx for any input :wink: