diff --git a/core/lib/Drupal/Core/StringTranslation/StringTranslationTrait.php b/core/lib/Drupal/Core/StringTranslation/StringTranslationTrait.php index c435f79..34877ec 100644 --- a/core/lib/Drupal/Core/StringTranslation/StringTranslationTrait.php +++ b/core/lib/Drupal/Core/StringTranslation/StringTranslationTrait.php @@ -100,11 +100,9 @@ protected function getNumberOfPlurals($langcode = NULL) { * The string translation service. */ protected function getStringTranslation() { - if (!$this->stringTranslation) { - $this->stringTranslation = \Drupal::service('string_translation'); - } - - return $this->stringTranslation; + // @todo Make this trait use DependencySerializationTrait to ensure that + // $this->stringTranslation can be serialized. + return $this->stringTranslation ?: \Drupal::service('string_translation'); } /** diff --git a/core/modules/locale/tests/src/Kernel/EntityTypeSerializationTest.php b/core/modules/locale/tests/src/Kernel/EntityTypeSerializationTest.php new file mode 100644 index 0000000..66db456 --- /dev/null +++ b/core/modules/locale/tests/src/Kernel/EntityTypeSerializationTest.php @@ -0,0 +1,41 @@ +container + ->get('entity_type.manager') + ->getDefinition('entity_test'); + + // Because the entity_test entity type does not explicitly define its plural + // label, this will result in a call to getStringTranslation(), which will + // implicitly cause $entity_type to reference the string_translation service + // and therefore become impossible to serialize due to the implicit + // dependency on the database connection (via locale's translator). + $entity_type->getPluralLabel(); + serialize($entity_type); + } + +}