[6.x] Fix localized dates leaking into later template usages - #15224
Open
duncanmcclean wants to merge 1 commit into
Open
[6.x] Fix localized dates leaking into later template usages#15224duncanmcclean wants to merge 1 commit into
duncanmcclean wants to merge 1 commit into
Conversation
Carbon is mutable and a dated entry's date is a single memoized instance, so calling `setTimezone()` in the `Localize` middleware's toString format (or in `CoreModifiers::carbon()` when `localize_dates_in_modifiers` is enabled) leaked the localized timezone into every later usage of the same date within the request. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request fixes an issue where date modifiers would ignore
localize_dates_in_modifiers => falseand output dates in the display timezone — but only when the same date had already been output plainly earlier in the template.This was happening because Carbon instances are mutable, and a dated entry's
dateis a single memoized instance shared by every usage in the template. TheLocalizemiddleware'stoStringFormatcallback calledsetTimezone()on it directly, so the first plain{{ date }}output shifted the shared instance into the display timezone and every later modifier saw the shifted date. (Date fieldtype values weren't affected, since augmentation creates a fresh Carbon instance on each access — which is why the behaviour seemed inconsistent between fields.)This PR fixes it by copying the date before localizing it, both in the middleware's
toStringFormatcallback and inCoreModifiers::carbon(), which had the same mutation in the other direction whenlocalize_dates_in_modifiersis enabled.Fixes #13869