Creating a custom aggregation for views I have implemented the data described here and now want to create a view that will work out the handicap for a particular player. I have so far created my view which list players and using a relationship to results for the player, and then aggregating these fields, can display things like the players best score and their average score.

Next I want to display the players "Handicap", which is a complex aggregation. Namely, it's the average of the best 8 of the last 20 results multiplied by 0.93. Can you write custom aggregation functions? Or should I create a new field on the result? I'm not sure of the easiest way to go about this.

Thanks!

EDIT: Thanks to @Jimajamma I was pointed in the right direction. You can do what he suggested purely from a module. I used the Fields UI to create a field that I can then manipulate to keep it simple. In the module I've used hook_theme to register a tpl.php file. I struggled to get my preprocess function to be recognised because of the base_hook directive which seems to then cause views/core to ignore the preprocess functions directive. The following is the simplest module you can create:

function golf_ladder_views_api() {
  return array('api' =&gt; 3.0);
}

function golf_ladder_theme($existing) {
  return array(
    'views_view_field__field_handicap' =&gt; array (
      'arguments' =&gt; array('view' =&gt; NULL, 'field' =&gt; NULL, 'row' =&gt; NULL),
      'template' =&gt; 'views-view-field--field-handicap',
      /* 'base hook' =&gt; 'views_view_field',
       * Don't do this if you want to use a preprocess that you control.
       * If you define base hook views seems to want to ignore the 
       * 'preprocess functions' directive below
       */
      'path' =&gt; drupal_get_path('module', 'golf_ladder'),
      'preprocess functions' =&gt; array(
        'template_preprocess',
        'template_preprocess_views_view_field',
        'golf_ladder_preprocess_views_view_field__field_handicap',
      ),
    ),
  );
}

function golf_ladder_preprocess_views_view_field__field_handicap(&amp;$variables) {
  $variables['output'] = '4'; // TODO: calculate proper handicap
}


After that create a template file views-view-filed--field-handicap.tpl.php which is simply:

print $output;
Whether this is the best, I don't know, but it is pretty easy:


Create a new field called handicap.  It doesn't need to have any value as you will be computing it.
Create a views template for that field.  It would be something like views-view-field--field-handicap-value.tpl.php. If you want it specific to the view, you will need to include its name in there as well, but just look in the Views Styles/Themes to find it. 
In that template, do your custom aggregation function, and then output that result instead of the default $output.


Now, if all is good in the world, you will have your handicap output in the view and you can aggregate on that value as well later on in the view, too.