Search code examples
phplaraveloctobercms

October / Wintercms : set default list filter values programatically


I'm using Wintercms (fork of Octobercms) to create a backend application which needs to display some data according to a date range.

I've used the list filters to be able to select custom date range: https://wintercms.com/docs/backend/lists#list-filters

But when we land on the list, I would like to have a default date range selected. The rule would be: "From 1st of february to 31st of october of the current year":

enter image description here

I haven't found any way of doing so in the documentations nor in internet examples..


Solution

  • Calculate dynamic defaults by adding scope definition in ListFilterExtendScopes event: https://wintercms.com/docs/backend/lists#extend-filter-scopes

    Default for daterange scope is array of :afterDate and :beforeDate values:

            $filter->addScopes([
                'latest' => [
                    'label' => 'Latest',
                    'type'  => 'daterange',
                    'conditions' => 'latest >= \':afterDate\' AND latest <= \':beforeDate\'',
                    'yearRange'  => '20',
                    'default'    => [
                        0: Carbon::now()->subDays(10),
                        1: Carbon::now()->addDays(10),
                    ],
                ],
            ]);