Problem/Motivation
Drupal\commerce\Hook\CommerceHooks::fieldWidgetSingleElementFormAlter() calls:
if (!str_starts_with($field_definition->getTargetEntityTypeId(), 'commerce_')) {BaseFieldDefinition::getTargetEntityTypeId() returns ?string. When code creates a synthetic BaseFieldDefinition that is not attached to an entity type - for example, Drupal Canvas's component-instance form builds field widgets for editing SDC props without a real entity behind them - the return value is NULL. PHP 8.1+ raises:
Deprecated function: str_starts_with(): Passing null to parameter #1 ($haystack) of type string is deprecated in Drupal\commerce\Hook\CommerceHooks->fieldWidgetSingleElementFormAlter() (line 134 of .../commerce/src/Hook/CommerceHooks.php)
Steps to reproduce
- Install Drupal Canvas (Experience Builder).
- Register a custom SDC with any prop that Canvas maps to a base-field-backed widget in its editor form.
- Open the Canvas editor and click a component instance to edit its props.
- Watchdog fills with the deprecation on every component-instance form render.
Proposed resolution
Null-coalesce the return value before comparison:
if (!str_starts_with($field_definition->getTargetEntityTypeId() ?? '', 'commerce_')) {An empty string cannot start with commerce_, so the existing early-return semantics are preserved and no functional behavior changes.
Remaining tasks
- Review and merge the one-line fix.
User interface changes
None.
API changes
None.
Data model changes
None.
Issue fork commerce-3607722
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
rsnydComment #6
jsacksick commentedComment #8
f0ns commentedJust noticed the same issue, this MR/patch fixed it for me.
Thank you!