Fraction and decimal properties return 0/1 when numerator/denominator are null

Created on 21 August 2023, 10 months ago

Problem/Motivation

This module provides two computed properties on Fraction field items: fraction (returns a Fraction object) and decimal (returns an auto-calculated decimal representation of the fraction).

When the fraction's numerator and/or denominator are NULL, however, which is the case on a newly created entity with no value in the fraction field, these fraction and decimal properties are incorrectly computed to show a value of "0". This is because they pass the NULL numerator/denominator values into the Fraction() class constructor, which defaults to a numerator of 0 and a denominator of 1.

Steps to reproduce

  1. Create an entity bundle with a Fraction field (eg: called fraction_field_test that is not required.
  2. Create an entity of that bundle through the Drupal UI, but do not enter any value in the Fraction field.
  3. Load the entity in PHP and examine the output of $entity->get('fraction_field_test')->fraction and $entity->get('fraction_field_test')->decimal. They should be NULL, but instead they are 0.

Proposed resolution

Check to see if the numerator/denominator are null. If they are, set the property to NULL as well.

Remaining tasks

  • Automated test.
  • Fix.

User interface changes

None.

API changes

None.

Data model changes

None.

πŸ› Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States m.stenta

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024