Problem/Motivation
This surfaced after migrating a project to PHP 8.5 on Dynamic Entity Reference fields . Suddenly I've noticed this error message:
Deprecated function: Using null as an array offset is deprecated, use an empty string instead in Drupal\token\TokenEntityMapper->getTokenTypeForEntityType() (line 72 of modules/contrib/token/src/TokenEntityMapper.php)
Tracking the error I see it happens in \Drupal\token\Hook\TokenTokenInfoHooks::fieldTokenInfoAlter(), lines ~693-694
$referenced_entity_type = $property_definition->getTargetDefinition()->getEntityTypeId();
$referenced_token_type = $this->tokenEntityMapper->getTokenTypeForEntityType($referenced_entity_type);
The problem seems to be that $referenced_entity_type is computed to NULL. But the Dynamic Entity Reference field is a special kind of fish. Even it extends EntityReferenceItem the 'entity' field property doesn't set a target entity data definition because the target entity type is unknown and is assigned dynamically (see \Drupal\dynamic_entity_reference\Plugin\DataType\DynamicEntityReference).
Steps to reproduce
Clear the cache and visit Status report
Proposed resolution
Only create the token if $referenced_entity_type is not NULL.
Remaining tasks
None.
User interface changes
None.
API changes
None.
Data model changes
None.
Issue fork token-3581883
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
claudiu.cristeaThe fix is straightforward. Do we need tests?
Comment #4
claudiu.cristeaComment #5
claudiu.cristeaComment #6
claudiu.cristeaAfter investigations on DER side I've found out that all we need is this change. I'm going to close the related issue.
This is a simple defensive fix and I think it doesn't need testing.
Comment #7
andypostLooks good to go
Comment #9
berdirMerged.