Problem/Motivation
Have view with a decimal field, say A.
Choosing comma as decimal separator.
Choosing 2 in Scale selector (number of digits to the right of the separator).
Result without aggregation correct. All values display with 2 decimals and comma as decimal point.
With aggregation the sum of A display with decimal point and 14 digits left of the separator.
Seams that the up-summing totally ignore the option I chose for the field A in views field selector.
Steps to reproduce
- Add a Decimal field, multi value, to a content type
- Add a node of that content type, with two values in the decimal field
- Create a view for that content type
- Enable aggregation
- Add the Decimal field to the view and set the Decimal marker to 'Comma' .
- In the Aggregation settings, set the Aggregation type to 'Sum'
Proposed resolution
Revert accidental commit of field handlers on aggregation field settings within get_aggregation_info()
from this commit:
https://git.drupalcode.org/project/drupal/-/commit/684b4a036e736b16d21a1...
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
Comment | File | Size | Author |
---|---|---|---|
#48 | 2735997-48.patch | 2.33 KB | Akram Khan |
| |||
#47 | 2735997-47.patch | 4.56 KB | Akram Khan |
#44 | after_patch.png | 55.45 KB | asad_ahmed |
#44 | before_patch.png | 55.7 KB | asad_ahmed |
#38 | 2735997-decimal-settings-38.patch | 1.74 KB | ramil g |
Comments
Comment #2
dagmarThanks for your report. Moving to the right component.
Comment #3
MarkoT91 CreditAttribution: MarkoT91 at Develomon commentedComment #4
MarkoT91 CreditAttribution: MarkoT91 at Develomon for FFW commentedTesting on 8.1.2. Tested also on 8.0.x versions.
This is the problem on views since the first stable release of drupal 8.
This problem occurs when aggregation is set on SUM or AVERAGE aggregation function.
Comment #5
MarkoT91 CreditAttribution: MarkoT91 at Develomon for FFW commentedSetting this to unassigned for now. Posted the screenshot of the issue.
Comment #7
chrotto CreditAttribution: chrotto commentedI am now using 8.2.0-rc1 and the wrong decimal handling is still there.
Perhaps the decimal point (point or comma) is not of great priority for most of you out there, but the number of decimals in the up summing must be!
Comment #8
dawehnerWell, noone had time/motivation to work on this issue :)
Comment #9
chrotto CreditAttribution: chrotto commentedSo the thing to do is to rewrite the result with Twig.
Solved the number of decimals with {{ field_myfield|round(2, 'common') }}. With a little bit more of Twig perhaps I can manage to also make a comma instead of a point.
Think it is a bit strange if it not is anyone that is interested to make the settings in Views to be right when using aggregate option.
Comment #10
dawehnerWell, what about you?
Here is a quick outline of thoughts:
\Drupal\views\Plugin\views\field\NumericField::buildOptionsForm
has support for float fields, using$this->definition['float']
\Drupal\views\Plugin\views\display\DisplayPluginBase::getHandlers
we initialize the fields, and provide a special way for aggregated fields\Drupal\views\Plugin\views\query\Sql::getAggregationInfo
defines these special fieldsThis should be totally doable in a BC compatible way.
Comment #11
chrotto CreditAttribution: chrotto commentedWould if I could.
Unfortunately I do not have the skills for it. At most my use of Drupal is depending on UI.
Comment #12
_Archy_ CreditAttribution: _Archy_ as a volunteer and commentedComment #13
_Archy_ CreditAttribution: _Archy_ at PitechPlus commentedUnassigning myself for now, but I'll be back.
Comment #15
Mschudders CreditAttribution: Mschudders as a volunteer commentedIt seems like this piece of code is responsible for the bad output.
It seems to me "Precision should be set."
or else you'll get:
A temporary solution could be:
The solution if we can fix this
ISSUE
In "NumericField.php"
This is actually not set if your field is a decimal or a float. :/ (I think it should be ?)
and this allows you to set the precision and other configuration via the views interface and thus resolving the issue. I could put the if switch in comment, but I don't know how it should exactly work.
And I don't know where "definition" is set actually :/
Comment #16
Ben Greenberg CreditAttribution: Ben Greenberg commentedTry using Twig's "number_format" filter instead of "round":
{{ field_myfield|number_format(2, '.', ',') }}
Arguments:
From: http://twig.sensiolabs.org/doc/2.x/filters/number_format.html
Comment #20
PanchoComment #21
chrotto CreditAttribution: chrotto commentedLooks like Mschudders have some good thinking about this. Is there not anyone who can help him with a solution to this.
Comment #22
nikita_ttIf aggregation is enabled then views field definition will be overriden here \Drupal\views\Plugin\views\query\Sql::getAggregationInfo() (See screenshot for more details).
If you want to have an ability to set "Precision", "Thousands marker", "Decimal point" then you need to enable additional options for the float numbers. You can do this by adding "float" option to your field definition. This can be done at least on hook_views_data_alter().
Here is an example:
Comment #23
super_romeo CreditAttribution: super_romeo as a volunteer commentedDear @nikita_tt,
for me works this:
Anyway, I think it is just a workaround.
Comment #24
LendudeUpdated the title a bit to make it clearer what this is about
Comment #25
playful CreditAttribution: playful commentedI tried controlling the decimal precision by using twig in a custom text field, such as the following:
{{ field_value|round }}
{{ field_value|round(1, 'common') }}
But none of these worked. They all returned a 0.
On a side note, no other twig math functions worked either, even for basic arithmetic. Is that a separate bug or are twig functions in the Views UI limited to conditional logic?
It really seems there should be options in the UI to control decimal precision when using aggregation. Any updated ideas on how to achieve this?
Comment #28
chankongching CreditAttribution: chankongching commented#16 is good enough
Comment #30
sarguna raj M CreditAttribution: sarguna raj M as a volunteer and commentedHi,
Tried #16 but its not working, the value returned as 0. Since the views uses the order total value (Sum and Average). Any update on this?
Comment #33
ramil g CreditAttribution: ramil g at The University of British Columbia commentedHey all, I created a patch for this.
Can everyone test it and let me know if you find any side-effects. With this patch, you don't need to put anything in your template file or implement any hooks. Aggregated fields will have the same settings that non-aggregated decimal fields have, just like how it works in Drupal 7 right now.
It also fixes the bug of the 'Group column' and 'Group column (additional)' sections missing from a field's Aggregation settings
Comment #34
ramil g CreditAttribution: ramil g at The University of British Columbia commentedComment #35
ramil g CreditAttribution: ramil g at The University of British Columbia commentedI overlooked something with patch #33. The 'thousand marker' settings was being duplicated. Fixed with this patch.
Comment #36
ranjith_kumar_k_u CreditAttribution: ranjith_kumar_k_u at Zyxware Technologies commentedComment #37
ramil g CreditAttribution: ramil g at The University of British Columbia commentedThanks for fixing the whitespace issue @ranjith_kumar_k_u but there's actually a problem with the patch, which I only found out after I saved my view. I'll see if I can find a different way to fix this.
Edit:
Actually I think the patch works. I've been testing on two different sites, one was a vanilla install of drupal 9(using 9.5.x-dev) and the other one, a real site, with some custom modules. I found out that it was one of the custom modules that had caused the issue. It seems to be working fine with the vanilla drupal 9 site. I would love to get others' feedback though.
Comment #38
ramil g CreditAttribution: ramil g at The University of British Columbia commentedIgnore my previous patches. This problem seems to have originated from this commit: lets use annotated handlers in even more places
See attached image. In get_aggregation_info(), somebody pasted the field handler where it should've fallen through to the original handler. It seems like a copy/paste error.
This patch corrects that error, removing the field handler from sum, avg, min, max, and stddev_pop
Comment #39
joelpittet#2735997-38: Decimal separator and decimals settings ignored when aggregating decimal fields While testing with @ramil g we thought through the possible regressions and this may need a CR for those that have implemented #2735997-23: Decimal separator and decimals settings ignored when aggregating decimal fields, but the twig template workaround should continue to work.
This also fixes a bug that we found that when you choose aggregation, the "Aggregation settings" lose the EntityField additional fields and yield a PHP 8.1 Warning (though notice in PHP 7) because it expects the "Group column" and " Group columns (additional)" there but they hide after any other option than "Group results together" is chosen.
Comment #40
quietone CreditAttribution: quietone at PreviousNext commentedLovely to see older bugs getting fixed!
This issue summary is out of date and does not document the proposed change here. Adding tag. I've added some steps to reproduce because I had to poke around and figure out how to create the problem.
In #39 @joelpittet explains the testing that was done but this is a UI issue so before and after screen shots should be added to the issue summary.
Since no tests are breaking with the change should one be added to prevent future problems?
Comment #41
joelpittet@quietone, thanks for the review, I've added the proposed change and yes probably could use a regression test.
Comment #42
asad_ahmed CreditAttribution: asad_ahmed at OpenSense Labs commentedComment #43
asad_ahmed CreditAttribution: asad_ahmed at OpenSense Labs commentedComment #44
asad_ahmed CreditAttribution: asad_ahmed at OpenSense Labs commentedI can apply the patch successfully and add before and after screenshots. Please review the screenshots.
Comment #46
joelpittet@asad_ahmed Can you let us know why there is a comma separator in your screenshot?
Comment #47
Akram KhanCreated patch for updated version 10.1.x
Comment #48
Akram KhanResolve #47
Comment #50
ressa CreditAttribution: ressa at Ardea commentedThanks for sharing a pragmatic Twig solution in #16 @Ben Greenberg. Drupal 10 is now on Twig 3, so the URL is https://twig.symfony.com/doc/3.x/filters/number_format.html, but the syntax is the same.