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

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

claudiu.cristea created an issue. See original summary.

claudiu.cristea’s picture

Status: Active » Needs review

The fix is straightforward. Do we need tests?

claudiu.cristea’s picture

Issue summary: View changes
claudiu.cristea’s picture

Issue summary: View changes
claudiu.cristea’s picture

After 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.

andypost’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to go

berdir’s picture

Status: Reviewed & tested by the community » Fixed

Merged.

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.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.