Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 294160

Provide an API to temporarily associate data with an entity

$
0
0

Problem/Motivation

In Drupal 7 it was very common to temporarily put data on an entity by using an arbitrary property to hold the data. This was abusing the fact that entities were simple objects:

$entity->myproperty = 'my data';

This was intended to keep track of some temporary state until the end of the page request.

Unfortunately this practice is completely unreliable; whenever static caches are cleared, or an unchanged entity is loaded from the database, a new entity will be instantiated and the temporary data is lost.

I see this practice continuing in the wild in D8 code, even though we can now solve keeping track of temporary state by writing a custom service to hold the data. Writing custom services might be overkill though in many cases, and with the lack of an officially supported alternative we will see the continued abuse of properties on the entity.

Proposed resolution

Provide new methods:

  • EntityInterface::setTemporaryData($key, $value, $persist_on_reload = TRUE)
  • EntityInterface::getTemporaryData($key)
  • EntityInterface::clearTemporaryData($key)

Alongside this we could maybe deprecate the setting of arbitrary properties on entities, so that in D9 or beyond we can disable this completely. This could be as simple as a few lines of documentation on the EntityInterface, or even a deprecated warning being emitted whenever an unsupported property is accessed.

Remaining tasks

Discuss potential pitfalls.

User interface changes

None.

API changes

New methods added.

Data model changes

None.


Viewing all articles
Browse latest Browse all 294160

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>