Introducing the Math Field Drupal Module
COVID19 Response - The Monarch Digital team is standing by to help you with any website needs you have during these difficult times. If you find yourself in need of web services please give us a call or reach out by email.
Love it or hate it, math is everywhere. Sometimes math will even find it's way into your Drupal projects. One of our engineering clients required dynamically evaluated complex math expressions in an entity form based on other fields. The module most closely met these requirements was Computed Field (https://www.drupal.org/project/computed_field). Unfortunately, there were several issues issues that ruled out the Computed Field module.
Computed Field Module Evaluation
- The Computed Field module requires the PHP Filter module. This makes computed fields very powerful but also allows arbitrary php code and stores the php in your database. If it is not immediately obvious why this is dangerous, see https://www.drupal.org/node/1203886 and http://andrewdunkle.com/2013/php-module-is-bad-to-the-bone.html
- Our client needed to evaluate math expressions dynamically on entity add/edit forms. Computed Field will only run the calculation when the form is submitted or when the entity is viewed.
- The Computed Field module requires knowledge of PHP. The administrators on our project were not developers and did not know PHP.
- The Computed Field module requires the PHP Filter module! No, this isn't a broken record. The PHP Filter module is just a really bad idea. *shudder*
Computed Field was out and we needed an alternative. To meet the requirements, Monarch developed and contributed the Math Field module (https://www.drupal.org/project/mathfield
Math Field Module as a Solution
Math Field defines a new field type that calculates math expressions and dynamically updates the result to entity forms. Math Field supports all the primary math operations including addition + (addition), - (subtraction), * (multiplication), / (division), and ^ (exponents) as well as constants 'pi' and 'e'. In addition to the basic operations, Math Field supports many advanced functions including:
sin(), sinh(), arcsin(), asin(), arcsinh(), asinh(), cos(), cosh(), arccos(), acos(), arccosh(), acosh(), tan(), tanh(), arctan(), atan(), arctanh(), atanh(), pow(), exp(), sqrt(), abs(), ln(), log(), time(), ceil(), floor(), min(), max(), round()
For a full list of available functions see https://www.drupal.org/node/2484093
You can install Math Field by downloading a release from the project page or using drush (drush dl mathfield). Once Math Field is installed, you can add a new Math Expression field to a content type or other fieldable entity (users, taxonomy terms, etc.). Math Field provides two widget types, Text field and Read-only.
Select the Text field widget if you want to allow users to override the computed result. Otherwise, choose Read-only. In the field settings, enter the math expression to evaluate. For example: 2 + 2 or sqrt(25).
The following additional field settings apply to the math expression result.
- Decimal marker: The character to mark the decimal in the result. Options are Decimal Point or Comma.
- Precision: The total number of digits to store in the database, including to the right of the decimal.
- Scale: The number of digits displayed to the right of the decimal.
After the field is saved, you will see the result of the expression on the add/edit form for your content type or entity.
Field tokens may may be added to the expression using tokens with the format [$field_name:$delta:$column] where $field_name is the machine name of the source field (eg. field_a), $delta is the position of a multivalue field ($delta starts at 0), and $column is the name of the column to use for multicolumn fields. $delta and/or $column may be omitted for single value and column fields respectively.
On the add/edit form, the math expression will be evaluated as soon as the dependent fields are populated. The result of the expression will be saved to the database.
Math Field is an excellent alternative to Computed Field when you need dynamically evaluated math expressions on your forms and don't want to subject your project to the horrors of PHP Filter.
Have any questions about the Math Field module?
Please post your comments or questions below in the comments section!