2 types of content, A and B.
A refers to B via a entity reference field, with the Entity Browser widget. It's configured to show the edit and remove buttons.
Both A and B have another entity references field, to files.

When I'm in the edit form of A and press the Edit button on B, a modal is opened with the edit form of B. However, not the files of B show up in the modal, but the files of A.

The cause of this is EntityReferenceBrowserWidget::getEntitiesByTargetId() using the unfiltered $form_state->getUserInput(), which contains the values of A's edit form, which are submitted when opening the modal via AJAX. The code in question was added in #2764889: Entity Browser widget loses selected images in inline entity form. A code comment already mentions that this needs to be refactored, "// TODO Figure out how to avoid using raw user input." and there seems to be a ticket for that as well, #2825890: Refactor and clean EntityReferenceBrowserWidget::formElementEntities(), but I was not able to find a bug report yet about the wrong field values being used in the modal.

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

Cyberwolf created an issue. See original summary.

tarasich’s picture

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

bramdriesen’s picture

Version: 8.x-1.1 » 8.x-2.x-dev

bramdriesen’s picture

Status: Active » Needs review
benstallings’s picture

Status: Needs review » Reviewed & tested by the community

Claude Code says:

This is a clean, minimal guard. The distinction between getUserInput() (raw POST, always present) and getValues() (processed, only present after this form's submit handlers run) is the right signal to check.

One edge case: getValues() could technically be empty on a legitimate submit if the form has no elements that produce values (unlikely here, but possible in theory). In practice, the entity reference widget's form always has values when submitted, so this is safe.

Verdict: Good fix. Simple, well-commented, addresses the root cause. Good to merge.

anybody’s picture

Issue tags: +Needs tests

I guess it would make sense to have a test for this case, so it doesn't break again in the future?

benstallings’s picture

Assigned: Unassigned » benstallings
bramdriesen’s picture

Tests look good to me :-)

anybody’s picture

Thanks @BramDriesen! I'll merge it then!

anybody’s picture

Assigned: benstallings » Unassigned
Status: Reviewed & tested by the community » Fixed

Thank you @benstallings!!

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.

anybody’s picture

@BramDriesen btw if you're able to help to review more of the issues here. that would be great to get a lot of bugs fixed!

  • anybody committed 684e5ef7 on 8.x-2.x authored by bramdriesen
    Issue #2905068: Entity reference field values from the referencing...
bramdriesen’s picture

I'll have a look!

Status: Fixed » Closed (fixed)

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