Based on https://www.drupal.org/project/views_cumulative_field, this module allows you to perform simple math expressions on two Views fields.

I created this because Drupal 8 Ctools does not currently have the Global Math Expression field that it had in Drupal 7.

Note: Twig can do math, but it's better to do math with PHP. This field is especially useful for site-builders who want to avoid Twig or who have a module like Charts that responds better to this module than a field rewrite.

Instructions

After you enable this module (you may need to run cron/clear caches):

  1. Create/edit your view.
  2. Add fields as values on which you will perform your operation.
  3. Add the "Global: Simple Math Field" field (created by this module).
  4. Select the fields, and then add their tokens (e.g. @field_one) into the text area. An example operation could be '(@field_one / @field_two) * 100'.

That's it!

Versions

8.x-2.x

This version allows users to add multiple fields and operations into the same formula. It makes use of the "Eval-math" library. There is currently no upgrade path from 1.x to 2.x, but it's relatively easy to re-configure existing Views Simple Math Fields once you have updated (edit the view, configure the field, save). Would love someone to contribute an upgrade path! Here's what the 2.x field looks like:

Shows the new Views Simple Math Field in Views

3.0.x

This is nearly identical to 8.x-2.x, except it now relies on a fork of the "Eval-math" library to accommodate PHP 8+.

Contributing

If you are interested in helping out with this module, please submit patches or contact me directly.

Project information

Releases