When using "group results", price fields render correctly if the group column if Number and additionally the Currency Code field is selected.

However, choosing SUM or AVG causes the price field to render as a normal integer field.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mglaman created an issue. See original summary.

mglaman’s picture

In commerce_price.views.inc

      $data[$table_name][$field_name . '_number']['field'] = [
        'id' => 'numeric',
        'field_name' => $table_data[$field_name]['field']['field_name'],
        'entity_type' => $table_data[$field_name]['field']['entity_type'],
        'label' => t('number from @field_name', ['@field_name' => $field_name]),
      ];

The number field is marked to use the numeric field. When aggregating I am guessing that Views will default to the "first" field property as being the "real field", which is *_number. So on aggregation it sums to amount and it is rendering it using the numeric field handler.

Looks like we'll need to provide a custom handler.

mglaman’s picture

It might be more than that.

In \Drupal\views\Plugin\views\query\Sql::getAggregationInfo the aggregate options define the field handler.

      'sum' => [
        'title' => $this->t('Sum'),
        'method' => 'aggregationMethodSimple',
        'handler' => [
          'argument' => 'groupby_numeric',
          'field' => 'numeric',
          'filter' => 'groupby_numeric',
          'sort' => 'groupby_numeric',
        ],
      ],
mglaman’s picture

Okay, so by picking any form of aggregate function it overrides the price field to be a Numeric handler and not even EntityField.

mglaman’s picture

There's no way to override the handler...easily.

In \Drupal\views\Plugin\views\display\DisplayPluginBase::getHandlers

          $aggregate = $this->view->query->getAggregationInfo();
          if (!empty($aggregate[$info['group_type']]['handler'][$type])) {
            $override = $aggregate[$info['group_type']]['handler'][$type];
          }

The handler is overridden from that aggregation info. We would have to create a new display plugin which attempted to override this, using its own handler to support SUM on the field while GROUP on the currency code.

mglaman’s picture

mglaman’s picture

Status: Active » Needs work
FileSize
4.68 KB

Here is a patch which implements the core patch fix to commerce_order_report views. It unsets aggregate functions to additional fields added in. Now we need to fix the formatting, which should be possible since we are grouping by the currency code, now.

mglaman’s picture

Okay, it looks like we can't format the output in the pre_render Views hook. The \Drupal\views\Plugin\views\field\NumericField::render method calls $value = round($value, $precision); which causes the formatter price to return 0.

mglaman’s picture

Status: Needs work » Needs review
FileSize
109.31 KB
6.72 KB

This makes it work! Swaps out the field handler later on.

Status: Needs review » Needs work

The last submitted patch, 9: 2975099-9.patch, failed testing. View results

mglaman’s picture

Status: Needs work » Needs review
FileSize
4.27 KB

Whoops, that had other cruft in it.

mglaman’s picture

Status: Needs review » Needs work
+++ b/commerce_reports.module
@@ -10,3 +15,62 @@ function commerce_reports_query_commerce_reports_alter(AlterableInterface $query
+  // Swap out the NumericField handler with our special Price one.
+  $view->field['amount'] = \Drupal\commerce_reports\Plugin\views\field\PriceNumericField::createFromNumericField(
+    $view->field['amount']
+  );

Forgot I didn't make this dynamic and just hardcoded it for testing.

mglaman’s picture

Status: Needs work » Needs review
FileSize
5.15 KB

Using an array_intersect we can check if there are matching additional fields and determine the price fields.

      $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);
      }

  • mglaman committed 7147c91 on 8.x-1.x
    Issue #2975099 by mglaman: Price fields do not work properly when using...
mglaman’s picture

Status: Needs review » Fixed

Committed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.