Field items that are both empty are not considered as equal

Created on 20 March 2024, 9 months ago

Problem/Motivation

I have come across cases where upon order refresh using Drupal Commerce, the price of the order is recalculated. When the order is saved, the total_price field item list on the original entity does not contain any items, while the same field item list on the updated entity contains an item with an array with null for both amount and currency as its value.

Both of these field item lists are considered as empty by the field API (isEmpty method on FieldItemListInterface) because the field type considers the field item in the updated entity as empty. However, the equals method does not take into account the logic as isEmpty and the result is that the two fields are considered not equal.

Regardless of whether the price on the updated entity should be set to an array with NULL values, that's something for Drupal Commerce. However, the fact that equals considers as different two values that are considered both as empty by isEmpty, I would consider it a bug in Drupal core.

Steps to reproduce

1. Load an entity that does not have any field items in a given field.
2. Clone the entity.
3. In the cloned entity set the value of the field to contain an item that is not literally NULL but it is considered as an empty item by its field type.
4. Compare the two field item lists from the two entities using equals.
5. Observe that the result is FALSE.

Proposed resolution

Add a check on the equals method to check if the fields are considered empty by the field type.

Remaining tasks

- Are there any caveats? Any reasons that, in general, we don't want this proposed behavior?
- Write a test.

🐛 Bug report
Status

Active

Version

11.0 🔥

Component
Field 

Last updated 1 day ago

Created by

🇵🇪Peru krystalcode

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024