I use the Views API to programmatically build a view and execute it. To pass to this view the content of its exposed filters, I use the method view::set_exposed_input(), which simulates a user's choice of filter values.
Unfortunately, date_api_filter_handler does not respond to filter values if they are set in this manner. Other filter handlers I've used, and notably the ones bundled with the Views module, all respond correctly to values set using view::set_exposed_input(). In contrast, date_api_filter_handler behaves as if no filter value was passed, and therefore uses default filter values instead.
I've traced the problem to the date_api_filter_handler::init method, specifically the lines
$this->force_value = FALSE;
if (empty($this->options['exposed']) || (isset($this->options['expose']['identifier']) && !isset($_GET[$this->options['expose']['identifier']]))) {
$this->force_value = TRUE;
}
which explicitly check for $_GET variables instead of checking the view's exposed_filter attribute. Maybe the init method is called too early, but in any case the force_value flag later causes date_api_filter_handler::exposed_submit() to load default values, which causes the problem.
My workaround has been to add the filter values to $_GET itself, in addition to calling view::set_exposed_input(). However, I feel this is a hack and should be rectified in the Date API filter handler.
Comments
Comment #1
benkewell commentedRecently run into the same issue when trying to execute a view with exposed date filter by program.
Is there any way to solve this in the date filter code?
Comment #2
attiks commentedWe have the same problem, for now we commented the line
$this->force_value = TRUE;and for now all is still working.Anyone knows any downsides to this solution?
Comment #3
tauno commentedRan into the same problem but worse when using AJAX paging. Setting $_GET worked for the non-ajax first page view, but subsequent pages loaded with AJAX fail:
#934104: hook_views_pre_view called later when using ajax.
Comment #4
dman commentedTHANKS for this workaround. I spent ages thinking I was getting something (like the format of the date argument) wrong.
Expected to work:
THANKS for showing a fix that lets me know I'm not doing it wrong!
Comment #5
develcuy commentedSame behavior in D7 with Views 3
Comment #6
muschpusch commentedit should be like this
$view->set_exposed_input(array('YOUR_IDENTIFIER' =>array('min' => $date, 'max' => $date2)));