Drupal is a registered trademark of Dries Buytaert
Protected Pages 3.0.0 Major update available for module protected_pages (3.0.0). Commerce Core 3.3.8 Minor update available for module commerce (3.3.8). Search API HTML Element Filter 1.0.7 Minor update available for module search_api_html_element_filter (1.0.7). Layout Builder Reorder 2.0.1 Minor update available for module layout_builder_reorder (2.0.1). Ban 1.1.0 Minor update available for module ban (1.1.0). Field Formatter Range 2.0.0 Major update available for module field_formatter_range (2.0.0). Field Formatter Range 8.x-1.8 Minor update available for module field_formatter_range (8.x-1.8). UI Patterns (SDC in Drupal UI) 2.0.18 Minor update available for module ui_patterns (2.0.18). Search API Solr 4.3.11 Module search_api_solr updated after 14 months of inactivity (4.3.11). Provus Mega Menu Module provus_mega_menu crossed 1,000 active installs.

This module provides a text field formatter that can compute simple mathematical expressions using basic operators and parentheses. It displays the expression and its calculated result, with the result appearing on hover.

Math Field (Mathematical Expression Field Formatter)

A simple lexer and parser that can compute simple mathematical operations using the most basic operators `+, -, *, /` and can correctly handle parentheses `()` and floating point operations (decimal numbers). It can NOT currently handle negative numbers or unary operations.

It is implemented as a Drupal 8 service module and provides a text field formatter.

The formatter currently displays errors inline.

Requirements

* Drupal 8.x version

Installation

Download, install and enable as you normally install a Drupal 8 module hosted on drupal.org

composer require drupal/math_field
drush en math_field

Usage

After enabling the module create a `text field` in your content type and choose `Math field formatter` as a formatter at admin/structure/types/manage/CONTENT_TYPE/display

The formatter will display both the expression and the result (or error message):
(1 + 2) * 4 = 12

The result will be revealed on hover with a delayed CSS animation.

Features

1. The Parser needs to be able to compute simple mathematical operations using the most basic operators (+, -, *, /) without using eval().
For example: “10 + 20 - 30 + 15 * 5” should return 75.
2. Make sure you take care of operator precedence using infix notation.
3. Provide a field formatter plugin in a Drupal 8 module that uses this Service.
4. Deliver the work as a Drupal 8 site with the custom module in a git repository.
5. Provide a simple unit test with a data provider (@dataProvider) that tests the tokenization (lexing) and parsing of a few different computations.
6. Provide a GraphQL field formatter so that the field can be consumed on the frontend via GraphQL. (Under development)
7. Add a frontend component in a template language (Twig, React) of your choice that will animate the calculation in the frontend, i.e. you first show the entire formula and on-hover, the text transitions into the computed outcome.

Submission

You need to make sure that by the end of the assessment everything needed is handed in as a Drupal repository. Besides implementing the requirements, the Drupal repository needs to specify a README document with all steps needed to install and run your code.

Inspiration

  1. Initial development was done by Giorgos Kontopoulos as a test exercise (github).
  2. Shunting-yard algorithm (wikipedia)
  3. University of Cambridge
  4. Rules (D8 module)
  5. Parsing and lexing
  6. https://github.com/vuchkov/math-php
  7. https://github.com/vuchkov/math-parser

Activity

Total releases
2
First release
Jun 2025
Latest release
1 year ago
Releases (12 mo)
0 ▼ from 2
Maintenance
Dormant

Releases

Version Type Release date
2.0.4 Stable Jun 18, 2025
2.0.3 Stable Jun 18, 2025