I have a view that includes a date field that only uses the year part. I want provide a default filter value for this field based on the current date. So far, I have this

function results_view_views_pre_view(&$view, &$display_id, &$args) {
  if($view->name == 'results_view') {
    $year = date("Y");
    $view->display['default']->handler->options['title'] = $year . ' Results';
    $view->display['page']->handler->options['filters']['field_season_value']['value']['value'] =  = $year;

This doesn't throw any errors but the filter view isn't filtered

I'm not sure what I'm doing wrong here, or if I'm even going down the right path. The value I assigned above is based on the fact that when I hard code the year into my view, the view object looks like this

[field_season_value] => Array
        [id] => field_season_value
        [table] => field_data_field_season
        [field] => field_season_value
        [relationship] => none
        [group_type] => group
        [ui_name] => 
        [operator] => =
        [value] => Array
                        [min] => 
                        [max] => 
                        [value] => 2013
                        [type] => date

Just to clarify, the date field is based on a fiscal year that runs September through August. So if its May 2013, the date would need to be 2012. My example above is just a simplified version if what I'm going to need to do in the final version.

  • Any particular reason not to set default to now via gui?
    – Mołot
    Commented May 17, 2013 at 22:22
  • Because I don't want to go in every year and change it to the current year.
    – Robbert
    Commented May 17, 2013 at 22:23
  • Also: consider changing title. Is access to filters from code what you really want? Or simply default to current year in filter??
    – Mołot
    Commented May 17, 2013 at 22:29

2 Answers 2


If you don't mind having "code in the database" you should be able to do all of this with an argument/contextual filter instead by providing a default value via PHP Code, eg, set one based upon your season field and then inside the GUI do something like this drupal 6 example...

enter image description here

...with obviously more logic than just returning 2013 :)

  • Thank you. This was a lot simpler than I was making it out to be. I did this (substituting my fiscal year calculation code) and also had to change the path of the view because adding the contextual filter was causing a conflict with another set of nodes.
    – Robbert
    Commented May 18, 2013 at 20:59

Set the default value to now using GUI. This should give you what you need without any code.

If you need to use fiscal years, either create own custom filter, one that will translate real date to fiscal year. Read manual and more detailed manual to see how to do it. Simply changing default value in date will not be enough, as user may inject date with more than a year set, making built-in filter not work the way you want it to. And then, well, then use now as default value.

You probably want to inherit from class views_handler_filter_date and override it's extra_options() method

  • Actually it's a little more complex than the current year. T runs Ina fiscal year which starts in September. That's why I need to do it programmatically.
    – Robbert
    Commented May 17, 2013 at 22:40
  • Hello, As would be done with Drupal 8?
    – chalo
    Commented Jun 18, 2019 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.