Problem/Motivation
This is a narrow follow-up from #2706883. That older issue is closed, and the latest guidance there was to open a fresh issue/MR for the current 8.x-1.14 behavior.
EntityReferenceRevisionsItem::onChange('entity') keeps target_id and target_revision_id in sync. The target_id write already handles an unset/new target through the computed entity reference property:
$target_id = $property->isTargetNew() ? NULL : $property->getTargetIdentifier();
The sibling target_revision_id write still calls getRevisionId() unconditionally:
$property->getValue()->getRevisionId()
If the computed entity property is NULL, that fatals with:
Call to a member function getRevisionId() on null
Minimal trigger:
$item->set('entity', NULL);Real-world trigger: Drupal core DefaultContent can currently pass NULL into an entity_reference_revisions field after an unresolved dependency lookup. I filed the DefaultContent/Graph-side issue separately as #3595546. Core should fail earlier, but ERR can still avoid turning a NULL clear into a fatal.
Steps to reproduce
- Create an entity_reference_revisions field pointing at a revisionable target entity.
- Populate one field item with a saved target entity.
- Call
$item->set('entity', NULL).
Expected: target_id and target_revision_id are both cleared.
Actual: target_revision_id sync calls getRevisionId() on NULL and fatals.
Proposed resolution
Mirror the existing target_id behavior for target_revision_id:
$target_revision_id = $property->getValue()?->getRevisionId();
Then write that value to target_revision_id.
Tests
Add kernel coverage to EntityReferenceRevisionsSaveTest:
- create an ERR field,
- assert target_id and target_revision_id are populated from a saved target,
- call
$item->set('entity', NULL), - assert both target properties are NULL.
Prepared with AI assistance.
Issue fork entity_reference_revisions-3600795
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
alex ua commented