number_format(): Passing null to parameter #1 ($num) of type float is deprecated in Drupal\Core\Field\Plugin\Field\FieldFormatter\DecimalFormatter->numberFormat()

Created on 29 June 2024, 3 days ago

Problem/Motivation

\Drupal\Core\Field\Plugin\Field\FieldFormatter\NumericFormatterBase::viewElements() calls:

$this->numberFormat($item->value);

Since the item may belong to a field that is not required, it is completely valid for $item->value to be NULL.

\Drupal\Core\Field\Plugin\Field\FieldFormatter\DecimalFormatter::numberFormat() pases this value directly to PHP's number_format():

  protected function numberFormat($number) {
    return number_format($number, $this->getSetting('scale'), $this->getSetting('decimal_separator'), $this->getSetting('thousand_separator'));
  }

This triggers the following notice:

Deprecated function: number_format(): Passing null to parameter #1 ($num) of type float is deprecated in Drupal\Core\Field\Plugin\Field\FieldFormatter\DecimalFormatter->numberFormat() (line 68 of core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/DecimalFormatter.php).

Steps to reproduce

  1. Add an optional decimal field to an entity
  2. Do not configure the decimal field to be displayed on the form when creating the entity
  3. Configure the decimal field to be displayed when viewing the entity
  4. Create an entity
  5. Visit the entity's view page

Proposed resolution

Check for NULL either directly in NumericFormatterBase or in DecimalFormatter->numberFormat().

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component
NumberΒ  β†’

Last updated 3 days ago

No maintainer
Created by

πŸ‡©πŸ‡ͺGermany tstoeckler Essen, Germany

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.69.0 2024