Creating custom views sort handler

Events happening in the community are now at Drupal community events on www.drupal.org.
Alun's picture

Hi guys & gals,

I've been playing around with Views and getting it integrated into my module recently. Everything is working great - I am managing a sports league, so can now get people to display their leagues using views. Most of the information is stored in databases for simple retrieval, but I had to create custom field handlers for some, as some of the fields do not have fields in the table - for example I have the following table structure to store a teams position in a league

Standings
won
lost
drawn
goals_for
goals_against
bonus_points

As you can see, fields such as games played, and points scored are not in the table, as they are calculated using a custom handler (games played = won + lost + drawn)
My points fields is causing the most bother for me at the moment. I calculate points by multiplying games won, drawn and lost with values from another table. I managed to get the field displayed using a custom handler (I will show the code below) but I need to sort by this field, as teams are ranked in their league based on the points they have. Since there is no value for points in the database, I will need to create a custom sort handler to deal with this problem, but how exactly do I go about this?

I look forward to your help. The relevant custom handler code for the points field is below. Thanks!

from leaguesite_handler_field_points.inc

<?php
class leaguesite_handler_field_points extends views_handler_field_numeric{
    function
construct(){
     
parent::construct();
      
$this->additional_fields['won'] = 'won';
   
$this->additional_fields['lost'] = 'lost';
   
$this->additional_fields['drawn'] = 'drawn';
   
$this->additional_fields['bonus'] = 'bonus';
   
$this->additional_fields['win'] = array(
     
'table' => 'leaguesite_scores',
     
'field' => 'win',
    );
   
$this->additional_fields['draw'] = array(
     
'table' => 'leaguesite_scores',
     
'field' => 'draw',
    );
   
$this->additional_fields['loss'] = array(
     
'table' => 'leaguesite_scores',
     
'field' => 'loss',
    );
   }
 
   function
query(){
     
$this->ensure_my_table();
      
$this->add_additional_fields();
  }
 
  function
render($values){
     
$played = ($values->leaguesite_standings_won * $values->leaguesite_scores_win) + ($values->leaguesite_standings_lost * $values->leaguesite_scores_loss) + ($values->leaguesite_standings_drawn * $values->leaguesite_scores_draw) + $values->leaguesite_standings_bonus;
     
//for some reason, this comes out as blank when a zero is returned. Not a massive problem, however as you can just use the empty text to display a zero
   
return $played;
  }
}
?>

Comments

Hey, I got this answered on

Alun's picture

Hey, I got this answered on the IRC channel, so no worries about answering it. For those interested, you can't sort by a field that doesn't exist, which kind of makes sense as the sorting takes place in the DB query. Fortunately, because my points are worked out from a number of fields that DO exist, I think I might be able to work something out.
All the best guys!

Virtual virtual numeric field sorting and filtering issue

cmos's picture

if you got it answered can you please post the solution for us?

Did you try this???

abhitesh.das's picture

http://drupal.org/project/viewsphpfilter

I think this should help you..

Views Developers

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: