Posted by cmos on November 3, 2011 at 10:44am
I've created a custom handler for a counter virtual field for my module but it seems that I am doing something wrong since I can't sort it nor filter on it as a numeric field.
Any one can tell what I need to add to the query method in order to actually place the values in the tables for views to use it?
<?php
class mymodule_handler_field_count_for_interval extends views_handler_field_numeric {
function mymodule_count($nid){
$count=0;
if (isset($this->view->filter['timestamp'])) {
$filter_operator=strtoupper($this->view->filter['timestamp']->operator);
$filter=$this->view->filter['timestamp'];
$query=db_select('my_module_table','s');
switch ($filter_operator){
case '<':
case '<=':
case '>':
case '>=':
//building the query
$query->condition('timestamp',strtotime($filter->value['value']),$filter_operator)
->condition('nid',$nid,'=')
->fields('s',array('sid'))
->addExpression('COUNT(sid)','sid_count');
$result=$query->execute();
foreach ($result as $count_row){
//counting
$count+=$count_row->sid_count;
}
break;
case 'between':
case 'not between':
//building the query
if ($filter_operator)
$query->condition('timestamp',array(strtotime($filter->value['min']),strtotime($filter->value['max'])),$filter_operator)
->condition('nid',$nid,'=')
->fields('s',array('sid'))
->addExpression('COUNT(sid)','sid_count');
$result=$query->execute();
foreach ($result as $count_row){
//counting
$count+=$count_row->sid_count;
}
break;
}
}
return $count;
}
function query() {
$this->ensure_my_table();
$this->add_additional_fields();
}
function pre_render(&$values){
foreach ($values as $key=>$value){
$values[$key]->count=$this->my_module_count($value->nid);
}
}
function render($values) {
$count = $values->count;
return $count;
}
}
?>Thank you for reading this.
Comments
You have created a 'field'
You have created a 'field' handler which is used to display values in Views. You need to create accompanying 'filter' and 'sort' handlers to do what you want. Of course you can reuse existing sort and filter handler and extend them.
no other way of putting values in the view object?
Isn't there any way of putting actual values in the query result for the view object to process further as an actual result so it uses existing filtering and sorting handlers?