What is the proper way to alter the value of a filter with argument handling code? I'm trying to use an integer argument to specify the number of years back a view should return, e.g., argument of 5 will make the view display the result for "now -5 years." Here's the filter part of my view export:

$handler->override_option('filters', array(
  'type' => array(
    'operator' => 'in',
    'value' => array(
      'graduate' => 'graduate',
    ),
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'id' => 'type',
    'table' => 'node',
    'field' => 'type',
    'relationship' => 'none',
  ),
  'date_filter' => array(
    'operator' => '>',
    'value' => array(
      'value' => array(
        'date' => '',
      ),
      'min' => array(
        'date' => '',
      ),
      'max' => array(
        'date' => '',
      ),
      'default_date' => 'now -3 years',
      'default_to_date' => '',
    ),
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'date_fields' => array(
      'node_data_field_graduation.field_graduation_value' => 'node_data_field_graduation.field_graduation_value',
    ),
    'date_method' => 'OR',
    'granularity' => 'year',
    'form_type' => 'date_text',
    'default_date' => 'now -3 years',
    'default_to_date' => '',
    'year_range' => '-10:+0',
    'id' => 'date_filter',
    'table' => 'node',
    'field' => 'date_filter',
    'relationship' => 'none',
  ),
));

I tried setting various bits of $view->filter based on the argument, but none of it seemed to have any effect--the WHERE clause for the date filter part of the query was already set, and so my changes were ignored. I was able to get my view working by directly overriding the query object's where clause, with the following argument handling code:

$argument = (int) $argument;
$time = date("Y", strtotime("now -$argument years"));
$clause = "DATE_FORMAT(STR_TO_DATE(node_data_field_graduation.field_graduation_value, '%Y-%m-%%dT%T'), '%Y') > '$time'";
$view->query->where['date']['clauses'][0] = $clause;
return TRUE;

...However, this is obviously all kinds of unholy. Is there a flag I need to set that will make views rewrite the where clause based on my changes to the filter object?

And/or, is there a way to do this simply with a date argument, without altering a filter? I initially tried adding a date argument, but it did not seem to have the sorts of options for "time since" that the filter handler did.

Thanks in advance for any assistance.

Comments

seutje’s picture

$filters = $view->display_handler->get_option('filters');
$filters['date_filter']['value'] = 'now -' . $argument . ' years';
$view->display_handler->set_option('filters', $filters);

or something?

xjm’s picture

I tried:

$argument = (int) $argument;
$filters = $view->display_handler->get_option('filters');
$filters['date_filter']['default_date'] = 'now -' . $argument . ' years';
$filters['date_filter']['value']['default_date'] = 'now -' . $argument . ' years';
dsm($filters['date_filter']);
$view->display_handler->set_option('filters', $filters);
return TRUE;

as well as

$argument = (int) $argument;
$view->display_handler->handlers['filter']['date_filter']->options['default_date'] = 'now -' . $argument . ' years';
return TRUE;

to no effect, unfortunately. The query stays the same.

Pol’s picture

It will be available soon ! :)

Check my module here: http://drupal.org/node/376932

I'm planning to add support for dates and numeric soon, but I accept help, i'm pretty new in all of this ;-)

xjm’s picture

#3 with a "dates greater than" operator would allow me to do this without the filter; instead, it'd just be a date argument with simple handling code:

$argument = (int) $argument;
$handler->argument = date('Y', strtotime('now -' . $argument . ' years'));
return TRUE;
iamjon’s picture

Status: Active » Closed (duplicate)

From what I understand using a arguments to alter filters is a feature request that is being handled here:
#357082: Pull filter value from an argument? for 3x only, and unfortunately will not be available for views 2.x. Please follow the progress of the 3x feature request from there.

Otherwise you can also filter a view with another view #720422: Write tutorial explaining how to exclude nodes from view by another view? I'm working on the tutorial there.

If you have a workaround that can be placed in a tutorial please post it and I will write up a patch.

Marking this as a duplicate of #357082: Pull filter value from an argument?