Repository navigation
[6.x] Format dates in generated fields in the site’s locale - #20467
Open
brandonkelly wants to merge 2 commits into
Open
brandonkelly wants to merge 2 commits into
brandonkelly wants to merge 2 commits into
Conversation
Generated field templates printed dates with PHP’s default string conversion: Carbon values as Y-m-d H:i:s, native date attributes as ISO 8601, and plain DateTime values threw an error. Dates referenced by a generated field template now print the way element cards show them, in the element’s site language, while filters still get a date. Amp-Thread-ID: https://ampcode.com/threads/T-01a1b7ff-b71a-7140-bea4-22eb1d71ca29 Co-authored-by: Amp <amp@ampcode.com>
[ci skip] Amp-Thread-ID: https://ampcode.com/threads/T-01a1b7ff-b71a-7140-bea4-22eb1d71ca29 Co-authored-by: Amp <amp@ampcode.com>
📚 Storybook previews@craftcms/ui — open Storybook No changed components detected in this Storybook. resources/js — open Storybook No changed components detected in this Storybook. |
This branch has not been deployed
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.
Description
Generated field templates printed dates however PHP stringified them, so cards showed raw values:
en-USsite){startDate}(Date field, loaded from the DB)2026-10-09 10:00:0010/9/2026, 10:00 AM PDT{startDate}(Date field set from code as aDateTime)Object of class DateTime could not be converted to string10/9/2026, 10:00 AM PDT{postDate}2026-03-04T13:06:00+00:003/4/2026, 5:06 AM{startDate|date('Y')}20262026Generated field values are stored when an element is saved and differ by site, so they’re now formatted in the element’s site language, the same way entry title formats are rendered.
ElementWrites::completeGeneratedFields() foreach site element foreach generated field - renderObjectTemplate(template, element) + I18N::withLocale(site language) + renderObjectTemplate(template, element, generatedFieldDateVariables(element, template))generatedFieldDateVariables()passes each date attribute or custom field that the template references as aCarboncopy whose string form matches the date’s card preview:getPreviewHtml(), so date-only fields print only the date and time zones show the way they do in cardspostDateuse the formatter’s short date and timeObject template variables take precedence over the element’s own values for
{foo}shorthand. Filters such as|datestill receive a date. Full Twig like{{ object.startDate }}still prints the element’s own value.Stored values change the next time their elements are saved. Run
php artisan craft:resave:allto update existing values.Evidence
SaveElementTest› “formats dates that generated fields print in the site’s locale” fails on6.x:Object of class DateTime could not be converted to string.After: It passes. A
desite with the template{startDate} | {day} | {postDate} | {startDate|date("Y")} | [{endDate}]stores09.10.2026, 10:00 PDT | 28.02.2027 | 04.03.2026, 05:06 | 2026 | [].composer ci: Pint, Rector, PHPStan, and the core suite pass (12,425 tests).tests-adapterhas 3 failures (Legacy\ConfigCompatibilityTest,Legacy\Markdown\LegacyMarkdownTest) that also fail on unchanged6.xand pass when run alone.