I have created a Drupal view for which I am unable to find any module that provides me with the filter that I need. How do we create a custom filter for Drupal views?
$options should be an array containing whatever options the argument handler needs. As argument handlers are quite configuration heavy, you probably will need this. You can get the options by setting up a view the way you want it in the UI, then exporting it and looking through the view code for the options portion of the argument in question.
More detailed code can be this:
<?php $view_args = array(); //Array of arguments $display_id = 'page'; $view = views_get_view('viewname'); //Name of your view $view->set_display($display_id); $view->display_handler->set_option('items_per_page',5); //Set the number of items to return
You can create your own filters as follows:
<?php
$view = views_get_view($viewname);
$view = views_add_argument_handler($view, 'handlername');
$view->add_item('display_id', 'argument', 'tablename', 'fieldname', $options);
$output .= views_embed_view($view, $arg);
?>
$options should be an array containing whatever options the argument handler needs. As argument handlers are quite configuration heavy, you probably will need this. You can get the options by setting up a view the way you want it in the UI, then exporting it and looking through the view code for the options portion of the argument in question.
More detailed code can be this:
<?php
$view_args = array(); //Array of arguments
$display_id = 'page';
$view = views_get_view('viewname'); //Name of your view
$view->set_display($display_id);
$view->display_handler->set_option('items_per_page',5); //Set the number of items to return
$view->set_item($display_id,'filter','type',NULL);
$options= array(
'operator' => 'in',
'value' => array(
'nid' => array(1,2), // List of nids
),
'group' => '0',
'exposed' => FALSE,
'expose' => array(
'operator' => FALSE,
'label' => '',
));
$view->add_item($display_id,'filter','node','nid',$options); //Add the new filter back to the $view object.
print $view->execute_display($display_id, $args);
?>
Check out code here for more details: Drupal Views filters
Also, you can use Views PHP Filter module to pass your own filter (http://drupal.org/project/viewsphpfilter)
Does this API code help:
<?phpviews_db_object::add_item($display_id, $type, $table, $field, $options = array(), $id = NULL)
?>
Add an item with a handler to the view.
These items may be fields, filters, sort criteria, or arguments.
Definition at line 1871 of file view.inc.
References $display_id, $field, and views_object_types().
<?php
$types = views_object_types();
$this->set_display($display_id);
$fields = $this->display[$display_id]->handler->get_option($types[$type]['plural']);
if (empty($id)) {
$count = 0;
$id = $field;
while (!empty($fields[$id])) {
$id = $field . '_' . ++$count;
}
}
$new_item = array(
'id' => $id,
'table' => $table,
'field' => $field,
) + $options;
$handler = views_get_handler($table, $field, $type);
$fields[$id] = $new_item;
$this->display[$display_id]->handler->set_option($types[$type]['plural'], $fields);
return $id;
}
?>
- Tao