Entity display plugin "Rendered Entity", that is used on content edit forms, shows the old entity reference after the reference has been updated.
To reproduce:
- add entity reference field to any content type
- in form display settings, set the entity reference field to use Entity Browser widget
- in Entity Browser widget settings, select Entity display plugin "Rendered Entity"
- create a node with an entity reference filled (ref_1). Save
- edit the same node, remove previous referenced entity (ref_1) and add a new reference to a different entity (ref_2). Save.
- go to or reload the edit form again and note that once the new entity placeholder renders within the form, it shows the old referenced entity (ref_1) and not the one that you added last (ref_2)
The issue stems from incomplete cache keys/tags in /entity_browser/src/Plugin/EntityBrowser/FieldWidgetDisplay/RenderedEntity.php. Patch attached.
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | rendered-entity-cache-keys-tags-3123876-6.patch | 850 bytes | berdir |
| #2 | rendered-entity-cache-keys-tags-3123876-2.patch | 785 bytes | oksana-c |
Comments
Comment #2
oksana-c commentedComment #3
oksana-c commentedComment #4
oksana-c commentedComment #5
berdirI'm not sure I fully understand, but those are not valid cache tags and $build should already contain all the necessary cache tags by default.
Comment #6
berdirI did not run into a similar problem. I think the issue is with entities that don't use render caching themself like paragraphs library items.
The correct fix is IMO to only add the extra cache key if there are already cache keys or it is cached _only_ by this key.
Comment #7
berdirComment #8
bpizzillo commentedI just ran into this today with some (many) reference fields to media entities using the Rendered Entity plugin. The patch in #6 seems to fix the problem. What I saw, stepping through the debugger, was that the key was getting set before it ever got handed off to the renderer, and since the key was just 'entity_browser', the media item for the first field was reused for all the other fields. Will test it some more, but it seems good.
Comment #9
berdir> was that the key was getting set before it ever got handed off to the renderer
The cache key has to be set before the renderer. This happens with entity types that have render caching disabled, so their view builder doesn't set a #cache key, and then entity browser sets them all to the same one. media entities have render caching enabled by default, but it's something that can be disabled, which might be done in your case because they're not reused or might depend on the parent entity or so.
Comment #10
bpizzillo commented@Berdir,
Thanks for the note! I was about to argue that we don't disable caching and I stepped through our code and found that we do. We have a bug that errantly cleared $build['#cache'] in a hook_entity_build_defaults_alter while trying to add in a cache tag. So yay for this bug uncovering the underlying issue.
Comment #11
remydenton commentedThe patch in #6 worked great for us and the solution makes sense to me (i.e. adding an 'entity_browser' cache key when no other keys are present will result in overly aggressive caching). We're using paragraphs, so that part of the theory checks out as well.
Comment #12
tichris59 commentedI confirm patch #6 works perfectly, It would be great to add this fix to next release.
Comment #13
azinck commentedAnother vote of confidence for #6: it fixed my issue with Paragraphs Library.
Comment #14
berdirCommitted.