Hi
I'm trying to load a view using views_get_view set values to it's exposed filters / filters
and display it.
the main reason I need it is to display an age range select and maybe translate the age range to date of birth dates I have in my nodes, I thought of either modifying the exposed and later set the values to fit my view or creating my on form and then loading the view with those params
I've tried a few things some worked better some less but I can't manage to get it right
thanks
nir
this is the code I'm using to load the view
$view = views_get_view('browser');
// embed browse view
$arguments = array();
// Age - age selection
if ((!empty($_GET['age'])) && (intval($_GET['age']) != 0)) {
$age_max = $_GET['age'];
$age_min = $age_max--;
$view->set_exposed_input(array('identifier' => 'age','value' => array('min' => $age_min, 'max' => $age_max)));
}
// s - sex selection
if (!empty($_GET['s'])) {
$view->set_exposed_input(array('identifier' => 's','value' => $_GET['s']));
}
// occu - occupation data
if (!empty($_GET['occu'])) {
$view->set_exposed_input(array('identifier' => 'occu','value' => $_GET['occu']));
}
$result = $view->render('default');
print $result;
Comments
Comment #1
dawehnerI suggest to use $view->preview instaed of render. Render does not execute everything which is needed for a full views workflow, for example pre_render.
Comment #2
nir commentedThanks,
sorry but I haven't understood fully
I want to supply filter values to a view -> I'll build a form or modify the existing exposed filter form
I want to be able according to that form output specify filters to the view
I get an age group 1-5 and I want to send the view min max dates according to that
I didn't understood how and where to use the $view-> preview
Comment #3
dawehnerYou could try out this.
Comment #4
nir commentedThanks
it doesn't seem to work
I've copied the code to my node - I giving the view different values but the display is always the same.
nir
Comment #5
nir commentedThanks for the response
I've changed the code from set_exposed_filter ($filter) to
$view->exposed_input['age'] = array('min' => $age_min, 'max' => $age_max);
and now it works
Comment #6
dawehnerFine.
You could perhaps try out
Then set the exposed input... and then use view->preview.
Comment #8
stefan81 commentedHi, how to apply non-exposed filters?
I am trying to print an embedded view, filtered by a cck date field.
With the code below,
I get the view rendered, but the filter is not applied.
Maybe I need to pass in the operator?
I already defined a "same or smaller as" operator in the view though.
should execute like this:
Comment #9
stefan81 commentedany idea?
Comment #10
stefan81 commentedOk, I exported the view and checked the filters again.
In my case, the filter "date_filter" has an additional array;
so I needed to wrap the filter into an array.
Working code:
Hope this helps someone else.
Comment #11
youngelpaso commentedThanks. great discussion!
Comment #12
JCB commentedHere is my code which I used to filter nodes base on date using embed view via views_get_view().
*Note that the filter is already created on each view with the opperators (less than or equal to).
Comment #13
smiletrl commented#10 works for my needs as well.
I was trying to use views_get_view_result($name, $display_id, arg1, arg2); to pass date filter arguments to views to get result directly. It does't work. #10 works. However, I made some changes to it to make it work. Assigning value to the item doesn't work, but 'default_date' and 'default_to_date' works.
You can also use following code to get the item/filter you want to change in code:
Comment #14
smiletrl commented#5 works for another use case;
Comment #15
Naveen Balakrishnan commentedhi please guide me to give the value for the exposed filter.
code:
$view_all = views_get_view('blog_category_display_block');
$view_all->set_exposed_input(array('identifier' => 'field_blog_category_target_id','value' => $_REQUEST['category']));
$result_all = $view_all->preview('default');
print $result_all;