Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
If you need to set a default value for an exposed views filter, you can do it like this:
this example code will set a ranged date to have a default value of -30 days for the start date, and today for the end date
function hook_form_views_exposed_form_alter(&$form, $form_state, $form_id) {
switch ($form_state['view']->name) {
case 'my_view_name':
// the Date object works only with this date format:
$datemask = 'Y-m-d';
$form['field_date_value']['min']['#default_value'] = date($datemask, time()-30*86400);
$form['field_date_value']['max']['#default_value'] = date($datemask);
break;
$datemask = 'd/m/Y';
$form['field_date_value']['min']['#date_format'] = $datemask;
$form['field_date_value']['max']['#date_format'] = $datemask;
}
}