6

I've built a view (Drupal 6.x, Views 2.x). I'd like to be able to add a summary row at the end of this view — total up several columns and include the totals in the summary row.

How can I do this? Is there some Views data-altering hook I can implement, to change the constructed data (before it gets themed)?

(Note that I can't use views_calc because some of the data in this view is coming from Views Relationships, which views_calc doesn't support at the time of writing.)

smokris
  • 11,740
  • 2
  • 39
  • 59

5 Answers5

6

To answer my own question a couple hours later... One way would be to implement hook_views_pre_render():

/**
 * Implementation of hook_views_pre_render().
 */
function mymodule_views_pre_render(&$view) {
  if ($view->name == 'myview') {
    // perform calculations on each row
    $pointsEarned = $pointsPossible = 0;
    foreach($view->result as $submission) {
      if (is_numeric($submission->node_data_field_pointsearned_field_pointsearned_value)) {
        $pointsEarned += $submission->node_data_field_pointsearned_field_pointsearned_value;
        $pointsPossible += $submission->node_node_data_field_pointspossible_field_pointspossible_value;
      }
    }


    // insert a 'total' row
    $row = new stdClass();
    $row->node_data_field_pointsearned_field_pointsearned_value = $pointsEarned;
    $row->node_node_data_field_pointspossible_field_pointspossible_value = $pointsPossible;
    $view->result[] = $row;

    if ($pointsPossible > 0) {
      // insert an 'average' row
      $row = new stdClass();
      $row->users_name = 'Average:';
      $row->node_data_field_pointsearned_field_pointsearned_value = round($pointsEarned/$pointsPossible * 100) . "%";
      $view->result[] = $row;
    }
  }
}
smokris
  • 11,740
  • 2
  • 39
  • 59
  • If you have extra columns then that code will create lots of warnings. To remove these add $row->field_YOUR_FIELD = array(); after creating the new row to remove them. – Felix Eve Dec 02 '13 at 15:40
4

Glancing around, it looks like Views Calc can do what you want.

John Fiala
  • 4,561
  • 3
  • 30
  • 26
  • 3
    Views Calc doesn't support Views Relationships. The original question says that Views Calc can't be used for that reason. – smokris Jan 30 '13 at 02:51
2

Views Calc has a lot of open bugs. View Summarize seems more stable.

Simon Yost
  • 21
  • 2
1

If anyone else comes across this question, you can use Views Summarize that adds a summarized table display. You just set the display and then choose how you want each column summarized. I haven't been able to get this to work with Views Data Export yet, but it works if you just want to see the data on the site.

smokris
  • 11,740
  • 2
  • 39
  • 59
Stacey
  • 21
  • 1
1

Personally, I would handle this in the view templates. Create a views-view.tpl.php for your view, and then edit it calculate and print out the summary.

Another option is to create another display for the same view, and then create a views-view-unformatted.tpl.php and calculate and print out the summary w/o doing a print $row; to avoid calling the fields template. The add use view display where needed.

mpdonadio
  • 2,891
  • 3
  • 35
  • 54