In Views it is possible to rewrite the output result of a field.
If choosing:
'Override the output of this field with custom text', I can read leading text:
'You may include HTML or Twig. You may enter data from this view as per the "Replacement patterns" below.'
What I need is some simple math, like a division of the actual field. In Twig documentation I have found that both division / and floored division // among other math function is possible in twig. Trying this in Views I do not get the right result.
I have an integer field {{field_ute}} that have 140 as output. When trying maths on it I get these results:
{{field_ute}}=140
{{field_ute/2}}=0.5
{{field_ute//2}}=0
{{field_ute+10}}=11
The math operation is done but the field value are always set to 1 regardless of what the value really are.
What have I done wrong?
Comments
Comment #2
chrotto CreditAttribution: chrotto commentedComment #3
cilefen CreditAttribution: cilefen as a volunteer commentedComment #4
javjaff CreditAttribution: javjaff commentedI had the same problem. I'd tried many different variants and filters in "override the output" option.
{{ field_ute|number_format }} seems to be equal 1 (field_ute is the example here).
I got the same results by overriding default views template for fields using {{ field_ute.content }} inside a code.
{{ field_ute.content }} is displayed correctly, but it doesn't want to be used in math.
Comment #5
geek-merlinCrosslinking: #2544670: Where is the math field in D8
Comment #6
dawehnerI would argue its a duplicate of what @alex.rutz linked to. Thank you for finding this url!
Comment #7
mahtab_alam CreditAttribution: mahtab_alam commented{% set name = field_ute | trim %}
{{ name*4+8}}
After that you can do any complex calculation.
Comment #8
brooke_heaton CreditAttribution: brooke_heaton as a volunteer commentedJust to add, you can also use twig in views field rewrites. I'm using it to do a calculation based on a field delta.
{{ delta+1 }}
Genius. <3 that Twig.
Comment #9
varalakshmi kuppusamy CreditAttribution: varalakshmi kuppusamy as a volunteer commentedI have done multiplication operation in views. its works for me
{% set wgt = weight | trim %}
{% set qty = quantity | trim %}
{{ wgt*qty }}
Comment #10
MahmoudSayed96Thanks @varalakshmi-kuppusamy
Comment #11
CProfessionals CreditAttribution: CProfessionals commentedJust as an added note. Set the thousands marker to any field value to "none" otherwise it runs operations like the value is a string (I think) and makes the value 0.