I have a view of nodes (type='instance'), each of which has a start date (required) and an end date (optional, as the instance still might be ongoing). Using arguments, one can see all instances for a particular year. E.g.,

http://mbtn.net/rostertest/2007

The argument handling code is:

$year = ($args(0)) ? $args[0] : 0;

$view->filter[] = array(
  'vid' => $view->vid,
  'tablename' => '',
  'field' => 'node_data_field_start_date.field_start_date_value_year',
  'value' => $year,
  'operator' => '<=',
  'options' => '',
  'position' => count($view->filter),
  'id' => 'node_data_field_start_date.field_start_date_value_year',
);

$view->filter[] = array(
  'vid' => $view->vid,
  'tablename' => '',
  'field' => 'node_data_field_end_date.field_end_date_value_year',
  'value' => $year,
  'operator' => '>=',
  'options' => '',
  'position' => count($view->filter),
  'id' => 'node_data_field_end_date.field_end_date_value_year',
);

This works great. The only problem is that instances that are still happening (end date field = NULL) won't show up. I thought about a hook_form_alter() that changes the end date to today's date, but Views doesn't use node_load, it directly accesses the DB. Looking at the end query, it seems that the key part is this bit of the WHERE clause:

... AND (EXTRACT(YEAR FROM( REPLACE(node_data_field_start_date.field_start_date_value,'T',' '))) <= 2007)

It would be great if I could alter that to something like:

... AND (EXTRACT(YEAR FROM( REPLACE(node_data_field_end_date.field_end_date_value,'T',' '))) >= 2007 OR node_data_field_end_date.field_end_date_value=NULL)

I've been struggling to figure out how to do this. It seems that I could add a custom handler to my $view->filter[], yes? That (somehow) does an add_where to the query object? Is that right? (Based on [#99794] ) I've looked high and low for a tutorial on this, but no dice?

Can anyone steer me in the right direction?

Thanks!

Comments

crookednumber’s picture

UPDATE:

I think I'm a bit warmer. I've added a handler to my filter array:

$view->filter[] = array(
  'vid' => $view->vid,
  'tablename' => '',
  'field' => 'node_data_field_end_date.field_end_date_value_year',
  'value' => $year,
  'operator' => '>=',
  'options' => '',
  'handler' => 'mbtn_views_filter_handler',
  'position' => count($view->filter),
  'id' => 'node_data_field_end_date.field_end_date_value_year',
);

And elsewhere added that function:

function mbtn_views_filter_handler($op, $filterdata, $filterinfo, &$query) {
  $year = $filterdata['value'];
  $query->add_where("EXTRACT(YEAR FROM(REPLACE(node_data_field_end_date.field_end_date_value,'T',' '))) >= " . $year . " OR node_data_field_end_date.field_end_date_value IS NULL");
}

But it looks like the handler isn't even being fired (e.g., I've added print and exit statements to the handler, and nothing has happened).

What am I doing wrong?

scottrigby’s picture

Status: Active » Closed (duplicate)
scottrigby’s picture

Version: 5.x-1.6 » 6.x-2.x-dev