function _views_pivot_format_aggregate_field in Pivot Tables for Views 7
Parameters
$view:
$fields:
$field:
$values:
$mode:
Return value
mixed
1 call to _views_pivot_format_aggregate_field()
- template_preprocess_views_view_pivot_table in ./
views_pivot.theme.inc - Callback to prepare row data for later rendering.
File
- ./
views_pivot.theme.inc, line 330
Code
function _views_pivot_format_aggregate_field($view, $fields, $field, $values, $mode) {
/* @var $field_handler views_handler_field_field */
$field_handler = $fields[$field];
$value = '';
switch ($mode) {
case 'sum':
$value = array_sum($values);
break;
case 'avg':
$sum = 0.0;
$count = 0;
foreach ($values as $value) {
// Do not count empty or non-numeric cells.
// Strip out any spaces and thousand makers.
$stripped = str_replace(array(
' ',
',',
), '', $value);
$value = preg_match('/[-+]?\\d*\\.?\\d+/', $stripped, $matches) ? (double) $matches[0] : FALSE;
if ($value !== FALSE) {
$sum += $value;
$count++;
}
}
$value = $count == 0 ? 0.0 : $sum / $count;
break;
case 'count':
// Counting most likely doesn't require the rendering like the field.
return count($values);
break;
case 'max':
$value = max($values);
break;
case 'min':
$value = min($values);
break;
}
$alias = empty($field_handler->aliases[$field_handler->real_field]) ? $field_handler->real_field : $field_handler->aliases[$field_handler->real_field];
// Let's "compile" an aggregate that has all the fields with their aliases with an empty value.
static $aggregate;
if (!isset($aggregate)) {
$field_data = NULL;
$names = array();
foreach ($view->result as $result) {
$names += array_keys((array) $result);
if (!isset($field_data) && isset($result->_field_data)) {
$field_data = $result->_field_data;
}
}
$aggregate = new stdClass();
foreach ($names as $name) {
$aggregate->{$name} = '';
}
$aggregate->_field_data = $field_data;
}
$aggregate->{$alias} = $value;
// In $field_handler->render($aggregate), the value rendered is
// $field_handler->field_alias which is not always the same as $field_handler->real_field
// Not sure whether we should change the determination of $alias
// For now, set both
$aggregate->{$field_handler->field_alias} = $value;
if (method_exists($field_handler, 'get_value')) {
$entity = $field_handler
->get_value($aggregate, 'entity');
if (!empty($entity) && !empty($aggregate->_field_data[$field_handler->field_alias]['entity_type'])) {
$entity_type = $aggregate->_field_data[$field_handler->field_alias]['entity_type'];
$langcode = $field_handler
->field_language($entity_type, $entity);
$entity->{$field}[$langcode][0]['value'] = $value;
$aggregate->_field_data[$field_handler->field_alias]['entity'] = $entity;
}
}
if (method_exists($field_handler, 'set_items')) {
$rendered = $field_handler
->set_items($aggregate, 0);
return $rendered[0]['rendered']['#markup'];
}
if (method_exists($field_handler, 'render')) {
return $field_handler
->render($aggregate);
}
return $value;
}