1

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' => 3.0);
}

function golf_ladder_theme($existing) {
  return array(
    'views_view_field__field_handicap' => array (
      'arguments' => array('view' => NULL, 'field' => NULL, 'row' => NULL),
      'template' => 'views-view-field--field-handicap',
      /* 'base hook' => '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' => drupal_get_path('module', 'golf_ladder'),
      'preprocess functions' => 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(&$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;
1

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

  1. Create a new field called handicap. It doesn't need to have any value as you will be computing it.

  2. 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.

  3. 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.

| improve this answer | |
  • Is there a module way of doing this rather than in the theme do you think? I would like this to work against multiple themes. – Malks Jan 12 '12 at 9:26
  • oh. all you are doing is creating a theme template for the view field, so, if you want it to work in multiple themes, just copy this exact same file to each theme you want it to work in. – Jimajamma Jan 12 '12 at 17:26
  • thanks for the answer, that pointed me in the right direction. I'll edit my own question to explain how I put the code in a module only. – Malks Jan 22 '12 at 1:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.