Problem/Motivation

When serializing DrupalDateTime object the following exception is thrown:

LogicException: The database connection is not serializable. This probably means you are serializing an object that has an indirect reference to the database connection. Adjust your code so that is not necessary. Alternatively, look at DependencySerializationTrait as a temporary solution. in Drupal\Core\Database\Connection->__sleep() (line 1574 of web/core/lib/Drupal/Core/Database/Connection.php).

This is caused by formatTranslationCache property of the object. If there is another language selected than English, this property will contain references to stringTranslation service that it its turn can have database service references if you use locale module.

Steps to reproduce

PHP version: 7.3.24
Web Server: Apache
OS: Centos 7

1. Install Drupal 8.9.x
1a. Add language other than "English".
1b. Make sure that there are translations of months, days of the weeks in the database.
2. Install Commerce module 2.21
3. Install Commerce Shipping module.
4. Use delivery_date property of ShippingRate class. I am not aware of any contrib module that does it, but my custom shipping method provides rates that depend on date. The instance of this class is added to ShippingRates widget that is displayed on checkout form.
5. Install Commerce Promotion or Commerce GiftCard module, so that it is possible to run ajax callbacks on checkout for.
6. Get to the step where you can select the shipping rate and use the language other than English.
7. Try to add coupon/card code.
8. Observe the exception described in the "Problem Motivation".
9. Change to English language, try to do the same, everything should work as expected.

I guess there are easier steps to reproduce, like creating a form with ajax callback that has DrupalDateTime object for any form element.

Proposed resolution

Implement __sleep() method in DrupalDateTime class like:

  public function __sleep() {
    return ['langcode', 'dateTimeObject'];
  }

Remaining tasks

Write tests, make sure that no other properties of the class are needed in serialized version of the instance.

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

Not needed.

Issue fork drupal-3187004

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

a.dmitriiev created an issue. See original summary.

a.dmitriiev’s picture

Issue summary: View changes
larowlan’s picture

Status: Active » Needs work
Issue tags: +Needs tests, +Bug Smash Initiative

Thanks, patch looks straightforward, just need a test

mohit_aghera made their first commit to this issue’s fork.

mohit_aghera’s picture

Issue tags: -Needs tests
mohit_aghera’s picture

Status: Needs work » Needs review
larowlan’s picture

Looking good, would be good to somehow see a failing test patch

matroskeen’s picture

Status: Needs review » Needs work

I was recently dealing with serializing issues in migration tests, so I have thrown in my 2 cents.
Also, I'm +1 to upload a test-only patch and moving to NW to address remaining items.

matroskeen’s picture

For some reason, GitLab didn't send me a notification about your comment @mohit_rocks...
I have just one more question in gitlab ;)

I'm also wondering why the target branch is 8.9.x. Shouldn't it be 9.2.x?

a.dmitriiev’s picture

Re-rolled patch for 9.2.x

neclimdul’s picture

Couldn't figure out what was going on in the merge request. 1000s of changes and commits so maybe it needs to rebased or something.

If I understand the problem correctly, this is just on the property caching localisation. Looking at the patch, could we avoid a lot of complexity with the trait and wakeup stuff and just empty the cache during serialization and assume it can(and should) be rebuilt after serialization? The rest shouldn't really matter.

a.dmitriiev’s picture

Status: Needs work » Needs review

I created another MR to target to 9.3.x branch.

In my particular case the DrupalDateTime object was used in ajax form and the form cache was broken because of date object serialization, because localization has database service.

a.dmitriiev’s picture

Version: 8.9.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

kristen pol’s picture

Thanks for the MR and patch. If I understand correctly, the patch is for 8.9 and the MR is for 9.4.

I looked at the patch in #11 and noticed a couple minor nitpicks:

  1. +++ b/core/lib/Drupal/Core/Datetime/DrupalDateTime.php
    @@ -21,6 +22,10 @@
    +    __wakeup as defaultWakeup;
    +    __sleep as defaultSleep;
    

    Nitpick: Ordering doesn't match function ordering (__sleep then __wakeup) but I see this is the same as core/modules/forum/src/ForumManager.php so I guess it should stay.

  2. +++ b/core/tests/Drupal/Tests/Core/Datetime/DrupalDateTimeTest.php
    @@ -239,4 +239,23 @@ public function testGetPhpDateTime() {
    +   * Test to avoid serialization of formatTranslationCache
    

    Period is missing.

The MR has a gazillion changes so I'm not sure what to do there: https://git.drupalcode.org/project/drupal/-/merge_requests/112.diff

ranjith_kumar_k_u’s picture

StatusFileSize
new2.19 KB
new522 bytes

Just fixed CS error

a.dmitriiev’s picture

Kristen, the MR 122 is already closed because it is outdated. The new MR is here https://git.drupalcode.org/project/drupal/-/merge_requests/1340

kristen pol’s picture

Status: Needs review » Needs work

Moving back to needs work based on feedback above.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

recrit made their first commit to this issue’s fork.

recrit changed the visibility of the branch 11.x to hidden.

recrit changed the visibility of the branch 3187004-drupal9-rebased to hidden.

recrit’s picture

recrit’s picture

Status: Needs work » Needs review

I created a new MR 6334 based on 11.x. The following have been address.

- #18 code cleanup - DONE..

- Regarding MR1340 comment "Can we replace this by just settings the property definition to protected $formatTranslationCache = [];" - DONE

- Regarding MR1340 comment "This class really doesn't need any of the service detection magic. Lets just do this and get rid of the trait." - This does need the DependencySerializationTrait for the service "string_translation" stored in protected $stringTranslation.

recrit’s picture

StatusFileSize
new2.27 KB

attaching a static patch for composer builds.

smustgrave’s picture

Left some nitpicky stuff on the MR.

recrit’s picture

@smustgrave All MR comments have been resolved

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

All feedback appears to be addressed!

larowlan’s picture

Issue credits

  • larowlan committed 6567f4f0 on 10.2.x
    Issue #3187004 by recrit, a.dmitriiev, mohit_aghera, ranjith_kumar_k_u,...

  • larowlan committed 6db12006 on 11.x
    Issue #3187004 by recrit, a.dmitriiev, mohit_aghera, ranjith_kumar_k_u,...
larowlan’s picture

Version: 11.x-dev » 10.2.x-dev
Status: Reviewed & tested by the community » Fixed

Committed to 11.x and backported to 10.2.x - thanks!

Status: Fixed » Closed (fixed)

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