Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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.
Comment | File | Size | Author |
---|---|---|---|
#13 | 2975099-13.patch | 5.15 KB | mglaman |
| |||
#11 | 2975099-11.patch | 4.27 KB | mglaman |
| |||
#9 | 2975099-9.patch | 6.72 KB | mglaman |
#9 | Revenue_report__Order_report____Drush_Site-Install.png | 109.31 KB | mglaman |
#7 | 2975099-7.patch | 4.68 KB | mglaman |
Comments
Comment #2
mglamanIn commerce_price.views.inc
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.
Comment #3
mglamanIt might be more than that.
In \Drupal\views\Plugin\views\query\Sql::getAggregationInfo the aggregate options define the field handler.
Comment #4
mglamanOkay, so by picking any form of aggregate function it overrides the price field to be a Numeric handler and not even EntityField.
Comment #5
mglamanThere's no way to override the handler...easily.
In \Drupal\views\Plugin\views\display\DisplayPluginBase::getHandlers
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.
Comment #6
mglamanThis looks to be blocked, partially, by core #2975149: When using aggregation, additional fields should not be included in the grouping function
Comment #7
mglamanHere 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.
Comment #8
mglamanOkay, 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 return0
.Comment #9
mglamanThis makes it work! Swaps out the field handler later on.
Comment #11
mglamanWhoops, that had other cruft in it.
Comment #12
mglamanForgot I didn't make this dynamic and just hardcoded it for testing.
Comment #13
mglamanUsing an array_intersect we can check if there are matching additional fields and determine the price fields.
Comment #15
mglamanCommitted.