[development] D6 Views 2 adding filter programmatically

Earl Miles merlin at logrus.com
Thu Jul 28 05:20:20 UTC 2011


On 7/27/2011 2:35 PM, Jeff Hartman wrote:
> I have a block view set up, but I need to add a filter that uses a value from the node.
> 
> Adding a dynamic value to the filter seems pretty straight forward:
> 
> $view = views_get_view('related_cities');
> $display = 'block_1';
> $view->set_display($display);
> $filter_name = 'field_city_parent_id_value';
> $filter = $view->get_item($display, 'filter', $filter_name);
> $filter['value'] = 'my_new_search_value';
> $view->set_item($display, 'filter', 'field_city_parent_id_value', $filter);
> 
> 
> Problem is that I don't know where I should be running this code. Module or template? If I put it into a module, which function should it go in? I've tried it in hook_views_pre_render(), but I'm not sure how to get the results based on the new value.
> 
> Thoughts?

If adding a filter that uses a value from the node, typically you do
that using an argument (contextual filter in D7) and a default argument
(you can use PHP code to get the value you want).


More information about the development mailing list