1

I use a computed field, to calculate and store a floating value for a node. Now I want to work with this value in a twig template. When I access the field value via {{ content.computed_field.0 }}it outputs the correct value, say 0.22. When I try to calculate with this value, i.e. {% set newvalue = content.computed_field.0 * 100 %} the page breaks (just pre-calculation twig content is outputted).

My first guess was that, for some reason, the field value is stored or read as a string (or another type?!). My DB says its a float and I don't know how to check a variable type in twig.

I tried to to use twigs number_format()-function : {{ content.computed_field.0|number_format(2) }}. This transforms the computed field to 1. I have no clue why this happens.
What am I doing wrong? Any tips or a workaround?

I use Drupal 8, Twig 1.25, and Computed Field 8.x-2.0-alpha3

Thank you in advance

1
  • 1
    Why can't you make a math in field preprocess function?
    – David
    Commented Mar 25, 2020 at 15:04

1 Answer 1

1

You could try this, if your using a node template: {{ node.computed_field.value * 100 }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.