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
<?php
$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
dawehner CreditAttribution: dawehner commentedI 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 CreditAttribution: 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
dawehner CreditAttribution: dawehner commentedYou could try out this.
Comment#4
nir CreditAttribution: 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 CreditAttribution: 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
dawehner CreditAttribution: dawehner commentedFine.
You could perhaps try out
$view->set_display('default');
Then set the exposed input... and then use view->preview.
Comment#8
stefan81 CreditAttribution: 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.
$view = views_get_view('Treffpunkt');
$display_id = 'default';
$view->set_display($display_id);
$filter = $view->get_item($display_id, 'filter', 'field_eventdate');
$filter['value'] = '2011-02-11';
$view->set_item($display_id, 'filter', 'field_eventdate', $filter);
$view->set_items_per_page(3);
$viewsoutput = $view->render();
print $viewsoutput;
should execute like this:
SELECT node.nid AS nid, node.title AS node_title, node_data_field_eventdate.field_eventdate_value AS node_data_field_eventdate_field_eventdate_value, node_data_field_eventdate.field_eventdate_value2 AS node_data_field_eventdate_field_eventdate_value2, node.type AS node_type, node.vid AS node_vid FROM node node LEFT JOIN content_field_eventdate node_data_field_eventdate ON node.vid = node_data_field_eventdate.vid WHERE DATE_FORMAT(STR_TO_DATE(node_data_field_eventdate.field_eventdate_value, '%Y-%m-%%dT%T'), '%Y-%m-%%d') <= '2011-02-11' ORDER BY node_data_field_eventdate_field_eventdate_value DESC
Comment#9
stefan81 CreditAttribution: stefan81 commentedany idea?
Comment#10
stefan81 CreditAttribution: stefan81 commentedOk, I exported the view and checked the filters again.
In my case, the filter "date_filter" has an additional array;
$handler->override_option('filters', array(
'date_filter' => array(
'operator' => '<',
'value' => array(
'value' => NULL,
'min' => NULL,
'max' => NULL,
'default_date' => '',
'default_to_date' => '',
),
so I needed to wrap the filter into an array.
Working code:
<?php
$view = views_get_view('Treffpunkt');
$display_id = 'default';
$view->set_display($display_id);
$item = $view->get_item($display_id, 'filter', 'date_filter');
$item['value'] = array('value'=>'2010-09-27');
$view->set_item($display_id, 'filter', 'date_filter', $item);
$view->set_items_per_page(3);
$viewsoutput = $view->render();
print $viewsoutput;
?>
Hope this helps someone else.
Comment#11
youngelpaso CreditAttribution: youngelpaso commentedThanks. great discussion!
Comment#12
JCB CreditAttribution: 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).
$arg_discharge_completed = '2011-05-09';
$nameshipped = 'vessels_shipped';
$viewshipped = views_get_view($nameshipped);
$display_id_shipped = 'default';
$viewshipped->set_display($display_id_shipped);
$filtershippped = $viewshipped->get_item($display_id_shipped, 'filter', 'field_vessel_date_shipped_value');
$filtershippped['value'] = array('value'=>$arg_discharge_completed);
$viewshipped->set_item($display_id_shipped, 'filter', 'field_vessel_date_shipped_value', $filtershippped);
$viewshippedoutput = $viewshipped->render();
$nametipped = 'reports_trains_tipped_for_stockp';
$viewtipped = views_get_view($nametipped);
$display_id_tipped = 'default';
$viewtipped->set_display($display_id_tipped);
$filtertipped = $viewtipped->get_item($display_id_tipped, 'filter', 'field_tracking_discharge_end_value');
$filtertipped['value'] = array('value'=>$arg_discharge_completed);
$viewtipped->set_item($display_id_tipped, 'filter', 'field_tracking_discharge_end_value', $filtertipped);
$viewtippedoutput = $viewtipped->render();