EntityType objects cannot be reliably serialized without DependencySerializationTrait

Created on 7 July 2017, almost 7 years ago
Updated 13 September 2023, 10 months ago

Problem/Motivation

When you install the locale module, the core string_translation service will receive locale's translator class, which has an indirect dependency on the database.

If you then try to serialize anything, directly or indirectly, which:

a) uses StringTranslationTrait; and
b) does not use DependencySerializationTrait;

...you may get a "database connection is not serializable" exception, if the consumer of StringTranslationTrait has a reference to the string_translation service, set either explicitly by setStringTranslation() or implicitly by getStringTranslation().

I'm marking this as a major issue because it seems to me that it is squarely an architectural conflict between DependencySerializationTrait and StringTranslationTrait, and may need to be resolved in several different places in core. However, the EntityType class, and its descendants, are maybe the most visible and pervasive manifestation of this problem, so this issue fixes the problem in EntityType.

Original issue report, filed against Lightning:

Steps to reproduce using Lightning:

  1. Install Drupal 8 with fresh copy of lightning profile using composer.
  2. Add entity reference field to reference media entity type to basic page content type (doesn't work for any content type).
  3. Make sure the form widget is Entity Browser with Media Browser selector, the selector shows the Rendered Entity in Current selection area.
  4. Enable Language and Interface translation modules. Optionally (maybe required) add one more language. In our case it is German.
  5. Create a new node/edit existing node.
  6. Try to add/change the media in the newly created field.
  7. Observe fatal error 500 after clicking on "Place" button when it is trying the render the preview of the Media in for Current selection area.

Proposed resolution

Add DependencySerializationTrait to EntityType class.

There are a couple of longer-term solutions which can be discussed and worked on in other issues:

  1. getStringTranslation() should not implicitly set a reference to the string translation manager. To summarize #7 and quote #16:

    Without this patch, that condition could happen essentially by accident if you, or anything you are referencing (explicitly or implicitly) uses StringTranslationTrait without DependencySerializationTrait. With this patch, the only way you will encounter the serialization exception is if you (or a parent class you are extending) have explicitly injected the service. Which means you are in a position to actually fix the problem, either by removing the reference to the service, or by importing DependencySerializationTrait.

  2. Or, once Drupal core requires PHP 7, make StringTranslationTrait use DependencySerializationTrait.

Remaining tasks

Commit the patch.

User interface changes

None.

API changes

None.

Data model changes

None.

πŸ› Bug report
Status

Fixed

Version

8.6 ⚰️

Component
BaseΒ  β†’

Last updated about 1 hour ago

Created by

πŸ‡©πŸ‡ͺGermany a.dmitriiev

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

Comments & Activities

Not all content is available!

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

Production build 0.69.0 2024