For whatever reason, I'm not understanding the *_item method of adding a filter or sort to my default view, and desperately need to in order to finish my project in time for the looming launch date, so I really appreciate any help on this.

To get a head start, I created the view with all of the filters and sorts I would ever need, so I could see their parameters. Then I exported the view into code and cut out all of the filters and sorts from it, so I can add them as needed in my page callback function.

I'm trying to "translate" the override_option method used in the default view code into the *_item method without success.

Here's an example filter from my default view:

$handler->override_option('filters', array(
  'type' => array(
    'operator' => 'in',
    'value' => array(
      'ctype1' => 'ctype1',
      'ctype2' => 'ctype2',
    ),
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'id' => 'type',
    'table' => 'node',
    'field' => 'type',
    'relationship' => 'none',
  ),
  // rest of filters

Here's the example code provided by a Views-related information source (http://groups.drupal.org/node/10129):

$display_id = 'default';
$view->set_display($display_id);
$id = $view->add_item($display_id, 'filter', 'og_uid', 'is_admin');
$item = $view->get_item($display_id, 'filter', $id);
$item['value'] = array(0, 1);
$view->set_item($display_id, 'filter', $id, $item);
$view->is_cacheable = FALSE;

If someone would tell me how to replace values in the example code with those in my example filter, then I could probably figure out the rest of my filters and sorts.

Comments

Scott McCabe’s picture

Thanks to a post in the Views issue queue (http://drupal.org/node/333829), I found the solution.

I did the following:

$display_id = 'default';
$options = array(
  'operator' => 'in',
  'value' => array(
    'ctype1' => 'ctype1',
    'ctype2' => 'ctype2',
  ),
  'group' => '0',
  'exposed' => FALSE,
  'expose' => array(
    'operator' => FALSE,
    'label' => '',
  ),
  'id' => 'type',
  'relationship' => 'none',
);
$view->add_item($display_id, 'filter', 'node', 'type', $options);

Note: Since they are not needed in the $options variable, I removed the following parameters from override_option example:

'table' => 'node',
'field' => 'type',

At some point, I may experiment to see if there are other unnecessary options, but for now I'm happy.

JCB’s picture

Here 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();
caspercash’s picture

Hi! I was wondering if you could post here in what hook or name of the function did you put these lines of code on adding a filter to an existing view? Would greatly appreciate it. Thanks!

JCB’s picture

Hi Casper,

Sorry for only getting back to you now.

I am sure it is not the correct way of doing it; however, I added the provided code to the template files tpl.php (e.g. page-blabla.tpl.php or node-blabla.tpl.php)

I also used it in mime mail template file (mimemail-message.tpl.php) in order to embed the view into automated emails that are sent on daily basis which is sent using rules (scheduled rules).

You can even go as far as to create a node with php input format and insert the code there.

Hope this helps.