I have some complex logic that I'd like to use to set the default values for the exposed date filter in my View. It's more complex than the current default options allow. So I tried using hook_form_alter() but that doesn't seem to work. Is there another place I can override the defaults when the exposed filter form is generated?

Thanks for your help.

Comments

Mat77’s picture

I did it like this :

function MYMODULE_form_views_exposed_form_alter(&$form, &$form_state) {

  if($form['#id'] == 'views-exposed-form-Courses-page-3') // change "views-exposed-form-Courses-page-3" by your form id
  {
    $form['date_filter']['min']['#date_format'] = 'd/m/y'; // I'm changing it to the format 13/01/90
    $form['date_filter']['max']['#date_format'] = 'd/m/y';
  }

}

Hope this can help

Mat

mstrelan’s picture

This won't help until the form is submitted. I think the OP wants the initial display of the view to be defaulted. I think something like hook_views_pre_build() will help but I haven't quite worked that one out.

mstrelan’s picture

I was able to achieve this with the following code. It may not be the best way.

<?php
function MYMODULE_views_pre_build($view) {
  $dates = array(
    'start' => mktime(),
    'end' => mktime() + 86400,
  );
  $_GET['date_filter'] = $_REQUEST['date_filter'] = array(
    'min' => array('date' => date('d/m/Y', $dates['start'])),
    'max' => array('date' => date('d/m/Y', $dates['end'])),
  );  
}
?>

You would probably be better off altering $view->display['default']->handler->options['filters']['date_filter'] though.

fourmi4x’s picture

Hi,
I tried to modify directly the view data with:

function MY_MODULE_views_pre_build($view) {

  if ($view->name=='MY_VIEW') {

	  $view->display['page_1']->handler->handlers['filter']['timestamp']->value['min'] = '2012-02-18 00:00:00';
	  $view->display['page_1']->handler->handlers['filter']['timestamp']->value['max'] = '2012-02-20 00:00:00';
	  $view->display['page_1']->handler->handlers['filter']['timestamp']->value['value'] = '';
	  
	  $view->display['page_1']->handler->handlers['filter']['timestamp']->options['value']['min'] = '2012-02-18 00:00:00';
	  $view->display['page_1']->handler->handlers['filter']['timestamp']->options['value']['max'] = '2012-02-20 00:00:00';
	  $view->display['page_1']->handler->handlers['filter']['timestamp']->options['value']['value'] = '';
	  
	  return $view;
  }
}

but nothing appears in the exposed filter... SHould I modify something else somewhere else?
Or is the pre_build hook simply not the right one for this?

purabdk’s picture

Instead of this you can use folloing code

/**
 * hook_views_pre_view
 * @param type $view
 * @param type $display_id
 * @param type $args
 */
function MODULE_NAME_views_pre_view(&$view, &$display_id, &$args) {
  if ($view->name == 'VIEW_NAME') {
    $filters = $view->display_handler->get_option('filters');    
    $view->display_handler->override_option('filters', $filters);
  }
}



/**
 * hook__views_pre_build
 * @param type $view
 * @return type
 */
function MODULE_NAME_views_pre_build($view) { 
  if ($view->name=='VIEW_NAME') {    
    $view->display['page']->handler->handlers['filter']['filter_field']->value['value'] = 8;
$view->display['page']->handler->handlers['filter']['timestamp']->options['value']['min'] = '2012-02-18 00:00:00';
  $view->display['page']->handler->handlers['filter']['timestamp']->options['value']['max'] = '2012-02-20 00:00:00';
    return $view;
  }     
}
liquidcms’s picture

thanks @purabkharat, from your posting i figured out how to set a term ref field default value:

function umd_views_pre_build(&$view) {
  if ($view->display[$view->current_display]->handler->handlers['filter']['field_term_tid']) {
    $current = _umd_get_current_term();
    $view->display[$view->current_display]->handler->handlers['filter']['field_term_tid']->value = array($current => $current);
  }
  
  return;  
}

where _umd_get_current_term() is simply a custom function which returns the required/dynamic tid value.

DamienMcKenna’s picture

Issue summary: View changes
Status: Active » Closed (outdated)

We're sorry but the D6 release of Date module is no longer being supported. You are encouraged to update to Drupal 7 or 8, or direct questions to Drupal Answers.