Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 299228

FieldType DecimalItem may fail with a TypeError in preSave

$
0
0

Problem/Motivation

I wonder about strict types in the context of \Drupal\Core\Field\Plugin\Field\FieldType\DecimalItem::preSave. It calls

round($this->value, $this->getSetting('scale'))

which throws a TypeError exception on PHP 8.3 and later if $this->value happens to be a string. This can happen easily if a module calls ->setValue($value) and where $value may be a string, like e.g. received from a custom form or an external API.

Steps to reproduce

  • Create a decimal field with the name field_decimal on a node entity type
  • Write some PHP custom code like this and see that failing:
    $node = Node::load(1);
    $node->set('field_decimal', '2.5');
    $node->save();
    

Similar code can be found in contrib and custom code all over the place, still it is theoretical and I can't provide any more realistic examples.

Proposed resolution

Type casting the value before being passed to the round method.


Viewing all articles
Browse latest Browse all 299228

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>