Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
If you have a date variable in Twig you can't format it using date formats you've created in Drupal. Twig has a date filter but it does not hook up to Drupal things.
Beta phase evaluation
Issue category | Task, it's an addition as part of #2168231: Twig Functions needed in templates |
---|---|
Issue priority | Normal for now. |
Disruption | No disruption |
Proposed resolution
Add a new filter to do this.
Before
In preprocess or the render array variables.
// Functional service wrapper from D7.
format_date($timestamp, 'html_date');
// Date Formatter Service
\Drupal::service('date.formatter')->format($timestamp, 'html_date');
After
{{ timestamp|format_date('html_date') }}
Also note that Twig has a date filter & function already, but this doesn't work with the named formats defined in the Drupal UI or system
http://twig.sensiolabs.org/doc/filters/date.html
{{ post.published_at|date("m/d/Y") }}
Remaining tasks
TBD
User interface changes
n/a
API changes
API addition, ability to use Drupal date formats from Twig templates.
Data model changes
None.
Comment | File | Size | Author |
---|---|---|---|
#14 | Screen Shot 2015-09-25 at 10.59.51.png | 42.38 KB | Anonymous (not verified) |
#10 | add_twig_filter_for-2571561-10.patch | 6.27 KB | joelpittet |
#10 | interdiff.txt | 1.33 KB | joelpittet |
#8 | interdiff.txt | 2.01 KB | star-szr |
#8 | add_twig_filter_for-2571561-8.patch | 5.74 KB | star-szr |
Comments
Comment #2
joelpittetComment #3
lauriiiComment #4
lauriiiCreated follow-up for this: #2571633: Remove TwigExtension setter methods - use constructor instead
Comment #5
star-szrUse \Drupal\Core\Template\Loader\StringLoader instead here, \Twig_Loader_String() is going away. See #2568181: [META] Update to Twig 2.x in Drupal 9.
That's all I found, thanks @lauriii!
Comment #6
lauriiiThanks for the review @Cottser!
Comment #7
joelpittetChanged the filter name to match the old drupal function name. date_format back to format_date.
This has tests and seems to be a nice solution to have this in the drupal twig extension, may allow for less preprocessing.
Thank you @lauriii and @Cottser
Comment #8
star-szrHere's a usage, this shows how this can help us to simplify preprocess in some cases.
Comment #10
joelpittetWe missed a couple things. This should resolve those things. Like having a usecase to show how this fixes unnecessary preprocess! +1 to that.
Comment #12
joelpittetRandom CI fail.
Comment #13
lauriiiThe changes @Cottser and @joelpittet made after #7 looks sane.
Comment #14
Anonymous (not verified) CreditAttribution: Anonymous at XIO commentedI did some manual testing using an inline template render array, and this works as expected.
So RTBC+1 from me.
Comment #15
alexpottThis makes lots of sense. I'm committing this under the committer discretion clause in the beta policy. Committed b0f2f83 and pushed to 8.0.x. Thanks!