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

  1. Install Drupal Canvas (Experience Builder).
  2. Register a custom SDC with any prop that Canvas maps to a base-field-backed widget in its editor form.
  3. Open the Canvas editor and click a component instance to edit its props.
  4. 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

Command icon 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

rsnyd created an issue. See original summary.

rsnyd’s picture

Status: Active » Needs review

jsacksick made their first commit to this issue’s fork.

  • jsacksick committed ebdc87d4 on 3.x authored by rsnyd
    fix: #3607722 Deprecated: str_starts_with(null) in CommerceHooks::...
jsacksick’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

f0ns’s picture

Just noticed the same issue, this MR/patch fixed it for me.

Thank you!