$entity->_referringItem conflicts with rendering the same entity multiple times on the same page

Created on 1 September 2022, over 2 years ago
Updated 15 July 2024, 5 months ago

Problem/Motivation

entity reference formatters set a magic, mostly undocumented property _referringItem on entities that are being rendered.

That's a powerful feature for more advanced, context-sensitive control over the render process. For example we use it in combination with paragraph behaviors that allow users to control how entities look in ways that would be complex to handle with just view modes.

Steps to reproduce

Beside caching issues that are tricky but can be handled if you are careful (either disable caching or add keys), we recently realized that this is a problem if you are rendering the same entity multiple times on the same page with different behaviours, due to lazy rendering and render caching.

Basically, you have multiple formatters that all set that property on the same object, but you can only have one of those. And at an unknown time later in the render process, the rendering actually happens, which is when code might access it.

That means the last formatter that happens to set the property wins and that remains set for the rest of the process as entities are statically cached including all their set properties.

A similar problem also applies to $entity->view, but that's a different issue for a different day.

Proposed resolution

As a workaround in our project, we cloned the entity objects as part of \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase::getEntitiesToView(), that helps in our case.

A better solution might be to put it in the prepared render array, then it has a clear scope but it's obviously an API change. But in scope of 📌 [meta] Deprecate __get/__set() on ContentEntityBase Postponed , that would need to move away anyway.

It's still challenging to handle with enabled render caching, see the related issue for that.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
Entity 

Last updated about 17 hours ago

Created by

🇨🇭Switzerland berdir Switzerland

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • First commit to issue fork.
  • Status changed to Needs work 5 months ago
  • 🇧🇪Belgium tim-diels Belgium 🇧🇪

    From the info Berdir provided, I tried adding this to the base formatter but was not really sure how. So only added this to the entity reference view one and that worked for me.

    If someone could have a look on maybe how to implement this in the base formatter would be great.

  • Pipeline finished with Failed
    5 months ago
    Total: 162s
    #224514
  • Pipeline finished with Success
    5 months ago
    Total: 497s
    #224523
Production build 0.71.5 2024