Problem / Motivation

On PHP 8.1+, the following deprecation notice fires during search results rendering and other code paths involving indexed entity reference fields:

Deprecated function: str_contains(): Passing null to parameter #1 ($haystack)
of type string is deprecated in
Drupal\Core\Entity\EntityTypeManager->getDefinition()
(line 161 of core/lib/Drupal/Core/Entity/EntityTypeManager.php).

The root cause is that EntityDataDefinition::getEntityTypeId() can legitimately return NULL when no EntityType constraint is set on the definition (e.g. for entity reference properties whose target type is not fully specified). Several places in Search API pass that return value directly to EntityTypeManager::getDefinition() without a null guard, triggering the deprecation.

EntityTypeManager::getDefinition() calls str_contains($entity_type_id, ':') at line 161 before any null check, so even passing exception_on_invalid: FALSE does not prevent the notice.

Affected Call Sites

Three locations in Search API pass a potentially-null entity type ID to getDefinition():

  1. src/Plugin/views/field/SearchApiFieldTrait.php — getPropertyValues() — triggered during Views search results rendering when a result row contains an entity reference field whose typed data target definition has no EntityType constraint
  2. src/Utility/FieldsHelper.php — isContentEntityType() — called by getNestedProperties(), which is invoked during index field traversal and dependency calculation
  3. src/Plugin/search_api/datasource/ContentEntity.php — getPropertyPathDependencies() — called during config dependency resolution for datasource fields

Steps to Reproduce

  1. Run Drupal on PHP 8.1 or higher with error reporting set to E_ALL
  2. Create a Search API index that includes an entity reference field whose target entity type is not fully constrained (e.g. a generic dynamic entity reference, or a processor-generated property)
  3. Build a View on that index that renders results including that field
  4. Load the View page — the deprecation fires on every page load that renders search results

Alternatively, trigger FieldsHelper::getNestedProperties() or ContentEntity::getPropertyPathDependencies() against such an index (e.g. via Drush, indexing, or cron).

Proposed Resolution

Add null guards at all three call sites so getDefinition() is only called when $entity_type_id is a non-empty string:

  • SearchApiFieldTrait::getPropertyValues(): Wrap the getDefinition() call with if ($entity_type_id && ...), using exception_on_invalid: FALSE since the result is used only for a static-cache optimization
  • FieldsHelper::isContentEntityType(): Return FALSE early when $entity_type_id is falsy — the cleanest fix as it protects all callers of isContentEntityType()
  • ContentEntity::getPropertyPathDependencies(): Extract getEntityTypeId() into a local variable and wrap the getDefinition() call with if ($entity_type_id)

Patch covering all three sites is attached.

Issue fork search_api-3619092

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

blade_ukraine created an issue. See original summary.

blade_ukraine’s picture

Status: Active » Needs review

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

aren33k’s picture

Status: Needs review » Reviewed & tested by the community

@blade_ukraine, I had the same issue, your patch resolves the issue, I have pushed your patch to the git repo.