I'm trying to use views argument to add dynamically filter with the following argument validator code:

$view->display['default']->display_options['filters'] += array(
  'dynamic_filter_1' => array(
    'operator' => '>=',
    'value' => array('value' => $argument, 'min' => '', 'max' => ''),
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array('operator' => FALSE, 'label' => ''),
    'id' => 'field_diameter_value',
    'table' => 'node_data_field_diameter',
    'field' => 'field_diameter_value',
    'relationship' => 'none'
  )
);

return FALSE;

But there are many 'filters' arrays in $view and it looks like this is not the right one. Where should I add my dynamic filter? Is there something wrong with this style of adding filter?

Comments

merlinofchaos’s picture

Ordinarily, you should use $view->add_item() instead. However, if you're in argument validator code, it is probably too late to add it to the display; in which case, you need to add it directly to $view->filter array.

merlinofchaos’s picture

Status: Active » Fixed

I realize my answer was fairly terse but I don't have a great deal more to add; let me know if you need more information (specific questions always get better answers =)

ogi’s picture

I tried with the following validation code but it didn't work. Any ideas? Is construction of filter correct? I used as a model filters constructed by Filter pane and I what I saw with Krumo. As a difference with other items in $view->filter, I couldn't find views_handler_filter_numeric_content in http://views.doc.logrus.com/ and so I used views_handler_filter_numeric.

$filter = new views_handler_filter_numeric;
$filter->construct();
$filter->init($view, array(
  'id' => 'field_diameter_value_2',
  'table' => 'node_data_field_diameter',
  'field' => 'field_diameter_value',
  'operator' => '>=',
  'value' => array('value' => $argument, 'min' => '', 'max' => ''),
  'relationship' => 'none',
  'group' => '0',
  'exposed' => FALSE,
  'expose' => array('operator' => FALSE, 'label' => '')
));
$view->filter[$filter->options['id']] = $filter;

return FALSE;
merlinofchaos’s picture

views_handler_filter_numeric_content is probably a CCK filter (though it is misnamed).

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

mpaler’s picture

Status: Closed (fixed) » Active

Sorry if changing this back to active is poor form however, this is exactly what I'm trying to do.

To the author -- Have you managed to work this out?

To the world, has anyone managed to add/modify a filter dynamically via argument validator code for views 2? I've found articles for views 1 but nothing definitive for views 2?

Thanks,
Mike

merlinofchaos’s picture

Status: Active » Closed (won't fix)

In Views 2, you cannot modify filters with argument validators because filters are processed prior to arguments.