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:

1 Like

I just came back to this issue since it still was a open task in my git and had another look into it since there was still a problem:

So, to summarize it quickly again:
I have Model A which have a link to the related Model B entries. If I click on this link, I’ll be linked to Model B’s list and the filter “filter by id” is set to filter the list.

This approach from above works:
LINK:
<a data-request="onShowChanges" data-request-data="id:<?= $record->id; ?>">

Handler:

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

Filter from Model B:

    modela_id:
        modelClass: My\Plugin\Models\ModelA
        type: number
        conditions: modela_id in (:filtered)

Controller of Model B

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

This works and if I call the link, the list of Model B is filtered by the ID of Model A.
Just if I open the Filter, the input box with “is equal to” is empty, even if there’s the green badge telling me the filter is active. Also if I change another filter, the ID filter is obviously forgotten, even if the ID filter is still showing as active.

So… not sure now if something had changed or if I have a very old bug in here.

Someone has an idea? Any help is welcome :slight_smile: