Problem/Motivation

The entity_reference_autocomplete widget generates a fatal error when autocreating a new composite entity:

Error: Call to a member function getRevisionId() on null in Drupal\entity_reference_revisions\Plugin\Field\FieldWidget\EntityReferenceRevisionsAutocompleteWidget->massageFormValues() (line 32 of modules/entity_reference_revisions/src/Plugin/Field/FieldWidget/EntityReferenceRevisionsAutocompleteWidget.php).

Proposed resolution

Fix autocreation in the entity_reference_autocomplete widget.

Remaining tasks

  1. Write a patch
  2. Review
  3. Commit

User interface changes

Non-existing entities referenced in the entity_reference_autocomplete widget are automatically created.

API changes

None.

Data model changes

None.

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

idebr created an issue. See original summary.

idebr’s picture

- Added a Functional test for the autocomplete widget
- Fixed autocreation of the entity_reference_autocomplete widget

flyke’s picture

The patch could not be applied. And if I manually apply changes from the patch in public function massageFormValues,
I get an error because

$entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($value['target_id']);

is null. So

$entity->getRevisionId()

doesn't work.

Also, if I inspect, the value of

$value['target_id']

it is the string 'previous revision' so it is not a paragraph id or something.

My entire $values array does not seem to have any usable information:
Array ( [0] => Array ( [target_id] => previous revision [_weight] => 0 [_original_delta] => 0 ) [1] => Array ( [target_id] => [_weight] => 1 [_original_delta] => 1 ) )

Applied patch #4 is the code from previous patch #2 for the function massageFormValues, but this one applies to latest dev version of entity_reference_revisions module, with an added

if (!is_null($entity))

check, because that was causing errors on my project. I would urge anyone to create a better patch and test this further as my patch is missing the newly added test from #2

flyke’s picture

benstallings’s picture

Status: Needs review » Reviewed & tested by the community

Claude Code says:

Minor nits

- Extra blank line before the closing } (line between the } blocks) — cosmetic, not worth blocking over.
- The patch is from 2020 — given the age, it's worth verifying it still applies cleanly to 8.x-1.x. Looking at the current file, the code at lines 37–47 matches the patch's "before" context exactly, so it should apply.

Assessment: Good fix, recommend applying. It corrects a real logic-order bug and adds a proper null guard. Both changes are defensive and correct.

I'll make a MR!

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

  • berdir committed a7eaa29d on 8.x-1.x authored by benstallings
    fix: #2955240 entity_reference_autocomplete widget generates error when...
berdir’s picture

Status: Reviewed & tested by the community » Fixed

@benstallings: Thanks for the MR, that *is* the reason this old issue is now merged, so I granted credits for that, but please do not post "Claude Code says" comments. If I'm interested in the output of an LLM, I can run it myself. See the new AGENTS.md in drupal core.

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.