Skip to content

Make field item stub properties nullable - #1098

Open
hanming5 wants to merge 1 commit into
mglaman:mainfrom
hanming5:1078-nullable-field-item-stubs
Open

hanming5 wants to merge 1 commit into
mglaman:mainfrom
hanming5:1078-nullable-field-item-stubs

Conversation

@hanming5

@hanming5 hanming5 commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1078

Stored field item properties can be NULL, for example on items from appendItem() or on unsaved entities, and core's isEmpty() implementations check for NULL. This PR makes the stored properties listed in the issue nullable, matching the PathItem, TextItemBase and TelephoneItem stubs.

Stub Property Before After
StringItemBase value string ?string
EmailItem value string ?string
DecimalItem value string ?string
IntegerItem value int ?int
FloatItem value float ?float
BooleanItem value int ?int
TimestampItem value int ?int
LanguageItem value string ?string
LinkItem uri, title string ?string
FileItem description string ?string
FileItem display bool ?bool

Subclasses pick up the change through inheritance: StringItem, StringLongItem, UriItem, UuidItem and PasswordItem from StringItemBase, and ChangedItem and CreatedItem from TimestampItem. The matching assertions in tests/src/Type/data/field-types.php now include |null.

I left out the "Needs a closer look" items (LanguageItem::$language, LinkItem::$options, PasswordItem::$pre_hashed, MapItem::$value), since they weren't confirmed. I can cover them in a follow-up.

Effect on the Drupal core baseline check

The "Drupal core HEAD baseline check" job reports 7 errors on this PR. Four of them come from DecimalItem::preSave() (core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DecimalItem.php:113), which calls round($this->value, ...). Core's baseline has two entries for that line, and their messages include the old type:

  • Parameter #1 $num of function round expects float|int, string given.
  • Property ...\DecimalItem::$value (string) does not accept float.

With this change, the same two errors are reported with string|null in the message. The old entries no longer match, so the job reports both errors plus two unmatched baseline entries. Core would need to regenerate those two entries. The other three errors are unmatched baseline entries that also fail on main.

Stored field item properties are NULL on items from appendItem() and
on unsaved entities. Core's isEmpty() implementations check for NULL,
so the stubs now declare these properties as nullable, matching the
PathItem, TextItemBase and TelephoneItem stubs.

- StringItemBase, EmailItem, DecimalItem, LanguageItem: ?string $value
- IntegerItem, BooleanItem, TimestampItem: ?int $value
- FloatItem: ?float $value
- LinkItem: ?string $uri, ?string $title
- FileItem: ?string $description, ?bool $display

Fixes mglaman#1078
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Field item stubs should allow null for stored properties

1 participant