You are here

function commerce_reports_views_pre_build in Commerce Reporting 8

Implements hook_views_pre_build().

For all price fields, ensure that our PriceNumericField handler is used instead of the normal NumericField handler. This ensures proper formatting of the currency.

This assumes the currency code is available, by having it set to GROUP BY and not being aggregated by a function.

See also

commerce_reports_views_query_alter

File

./commerce_reports.module, line 72

Code

function commerce_reports_views_pre_build(ViewExecutable $view) {

  // Only act on report entity views.
  $base_entity_type = $view
    ->getBaseEntityType();
  if (!$base_entity_type || $base_entity_type
    ->id() != 'commerce_order_report') {
    return;
  }
  foreach ($view->field as $key => $field_plugin) {
    if ($field_plugin instanceof NumericField) {

      // Determine if this is a price field being aggregated.
      $intersect_test = array_intersect([
        $field_plugin->field . '_number',
        $field_plugin->field . '_currency_code',
      ], $field_plugin->additional_fields);
      if (!empty($intersect_test)) {

        // Swap out the NumericField handler with our special Price one.
        $view->field[$key] = PriceNumericField::createFromNumericField($field_plugin);
      }
    }
  }
}