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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

oksana-c created an issue. See original summary.

oksana-c’s picture

oksana-c’s picture

Status: Active » Needs review
oksana-c’s picture

Issue summary: View changes
Berdir’s picture

Status: Needs review » Needs work
+++ b/src/Plugin/EntityBrowser/FieldWidgetDisplay/RenderedEntity.php
@@ -76,7 +76,10 @@ class RenderedEntity extends FieldWidgetDisplayBase implements ContainerFactoryP
     $build['#entity_browser_suppress_contextual'] = TRUE;
+    $build['#cache']['keys'][] = $entity->bundle();
+    $build['#cache']['keys'][] = $entity->id();
     $build['#cache']['keys'][] = 'entity_browser';
+    $build['#cache']['tags'][] = $entity->bundle() . ':' . $entity->id();
 
     return $build;
   }

I'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.

Berdir’s picture

Status: Needs work » Needs review
FileSize
850 bytes

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

Berdir’s picture

Title: Entity display plugin "Rendered Entity" is missing cache tags/keys » Entity display plugin "Rendered Entity" should only add cache key if the entity is render cached
bpizzillo’s picture

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

Berdir’s picture

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

bpizzillo’s picture

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

remydenton’s picture

Status: Needs review » Reviewed & tested by the community

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

Tichris59’s picture

I confirm patch #6 works perfectly, It would be great to add this fix to next release.

azinck’s picture

Another vote of confidence for #6: it fixed my issue with Paragraphs Library.

Berdir’s picture

Status: Reviewed & tested by the community » Fixed

Committed.

  • Berdir committed 1a3207d on 8.x-2.x
    Issue #3123876 by oksana-c, Berdir: Entity display plugin "Rendered...

Status: Fixed » Closed (fixed)

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