Field SQL storage does not correctly detect value changes with leading zeros

Created on 13 September 2021, over 3 years ago
Updated 16 April 2024, 8 months ago

Problem/Motivation

When the config field_sql_storage_skip_writing_unchanged_fields is enabled and a fieldable enttiy is saved, the field SQL storage will perform a check to see if any field values have changed. If it detects that nothing was changed, the SQL write query is skipped. This comparison is done in _field_sql_storage_write_compare(). Currently, there is a bug in this function due to PHP's type juggling when using == or (in this case) !=: In PHP, '01' == '001' is TRUE.
However, when updating a field value, one may need to specify leading zeroes. Changes to that field value should not be omitted just because of PHP's type juggling.

The same bug seems to be active in Drupal 8 & 9 at the moment, see this issue .

<!--break-->

Steps to reproduce

  1. Have an entity (e.g. node) with a field (e.g. textfield).
  2. Add an field value containing only numbers, e.g. 0012.
  3. Save the entity; everything should work as expected.
  4. Edit the entity again, prepend one or more zeros the previously added value (e.g. updating it to 00012), and save again.
  5. Inspect the field value: it'll still be 0012 instead of the exacted 00012.

Proposed resolution

Recursively check nested arrays, use `===` for non-array values.

🐛 Bug report
Status

Needs work

Version

7.0 ⚰️

Component
Field 

Last updated 1 day ago

Created by

🇦🇹Austria Bird-Kid

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024