Math in rewrite result in Views

Last updated on
15 April 2024

This documentation needs work. See "Help improve this page" in the sidebar.

The Views enables to use Twig for calculations with fields in Rewrite result. Below is an example created by @frankdesign in the #2544670 issue.

Views Global Custom Text field allows Twig coding (as does rewriting results in any field). And Twig itself allows pretty much any type of calculation.

So to create complex calculations:

  1. Add your fields to the view as normal
  2. Put a tick in Exclude from Display
  3. In the field Style Settings remove tick from Add default classes and put a tick in each of Customize field htmlCustomise label HTML and Wrapper HTML element and change the dropdown for all to -None- (this reduces the field result to just the content)
  4. Add a Global Custom Text Field.

In this field, if you try to perform calculations on the values of above fields, you will either get an error or the result will be 1 or 0. This is because Drupal auto converts Views results to ViewsRenderPipelineMarkup objects. So the first thing you need to add in the Global Custom Text Field is a new variable for each of your fields and add |trim to the end of the field value (this converts the object back into a string) e.g.

{% set price = field_price|trim %}
{% set amount = field_amount|trim %}

Now you can perform calculations on the new variables.

{{ price * amount }}

Obviously you can perform more complex calculations e.g.

{{ ((((price * interest) * year) + price) / (year * 12))|number_format(2, '.', ',') }}

number_format(2, '.', ',') at the end of the result converts the result to a integer with 2 decimal places and uses a comma for a thousand separator.

Using raw values

An alternative way, instead of using the "trim" filter, is to use raw database values for calculation. Note that this __value token is only available in the field in the view, not in a Custom text field.

An alternative way, instead of using the "trim" filter, is to use raw database values for calculation. Below is the answer of @4uk4 from StackExchange:

Use raw database values for calculation. Most times rendered output doesn't work, because it contains strings or render arrays, not numbers.

I think you are referring to field rewriting in the Views fields UI, then open REPLACEMENTS PATTERN and you'll find this information:

You must add some additional fields to this display before using this field. These fields may be marked as Exclude from display if you prefer. Note that due to rendering order, you cannot use fields that come after this field; if you need a field not listed here, rearrange your fields.

The following replacement tokens are available for this field. Note that due to rendering order, you cannot use fields that come after this field; if you need a field not listed here, rearrange your fields.

  • {{ field_number }} == Number
  • {{ field_number__value }} == Raw value

So you need to add __value to each field name.

References
1. https://www.drupal.org/project/ctools/issues/2544670#comment-12275432
2. https://drupal.stackexchange.com/a/252811

Help improve this page

Page status: Needs work

You can: