I have a view that list some items depending on the current node loaded. I also want to add few exposed filters to the view. The exposed filters cannot be shown to users directly. I want to provide a separate set of filters (to display) for users and based on what user selected, I want to set one or more filters of the view.
For example: I want an option for user to show only nodes that are not older than 7 days, 14 days etc so I will provide a list with All, 7 days, 14 days etc and when user select any of these, I want to set the exposed filter for node created time and with the calculated value - the date 7 days or 14 days or so from now.
There are several filters like this - ie based on what the user selected, we need to apply different filters in view.
To implement this, I plan to implement a block, in which I can programmatically call a view
$my_view_name = 'the_view';
$my_display_name = 'block';
$my_view = views_get_view($my_view_name);
$my_view->set_display($my_display_name);
if ($option == FIRST_OPTION) {
$my_view->exposed_input['filter1'] = $option1_calculated_value1;
$my_view->exposed_input['filter2'] = $option1_calculated_value2;
}
else {
$my_view->exposed_input['filter1'] = $option2_calculated_value1;
$my_view->exposed_input['filter2'] = $option2_calculated_value2;
$my_view->exposed_input['filter3'] = $option2_calculated_value3;
}
$my_view->execute();
unset($my_view->exposed_widgets);
return $my_view->preview();
Is there any problem in using this method? Is there any other way to do this? Is there any performance implications in using this method? Will views caching etc work in this case? I use Drupal 7 and Views 7.x-3.7
view::execute()
will probably be cached, and the block itself is open to render caching, so your only 'hit' is the DB query and Views processing. The only way you'd do it quicker is to write and process the query manually, I guess, bypassing the Views overhead