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():
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 noEntityTypeconstraintsrc/Utility/FieldsHelper.php—isContentEntityType()— called bygetNestedProperties(), which is invoked during index field traversal and dependency calculationsrc/Plugin/search_api/datasource/ContentEntity.php—getPropertyPathDependencies()— called during config dependency resolution for datasource fields
Steps to Reproduce
- Run Drupal on PHP 8.1 or higher with error reporting set to
E_ALL - 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)
- Build a View on that index that renders results including that field
- 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 thegetDefinition()call withif ($entity_type_id && ...), usingexception_on_invalid: FALSEsince the result is used only for a static-cache optimizationFieldsHelper::isContentEntityType(): ReturnFALSEearly when$entity_type_idis falsy — the cleanest fix as it protects all callers ofisContentEntityType()ContentEntity::getPropertyPathDependencies(): ExtractgetEntityTypeId()into a local variable and wrap thegetDefinition()call withif ($entity_type_id)
Patch covering all three sites is attached.
| Comment | File | Size | Author |
|---|---|---|---|
| search_api-null-entity-type-getDefinition.patch | 2.59 KB | blade_ukraine |
Issue fork search_api-3619092
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 #2
blade_ukraine commentedComment #5
aren33k commented@blade_ukraine, I had the same issue, your patch resolves the issue, I have pushed your patch to the git repo.