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

Reference: https://www.drupal.org/core/beta-changes
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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Cottser created an issue. See original summary.

joelpittet’s picture

lauriii’s picture

Assigned: Unassigned » lauriii
lauriii’s picture

Assigned: lauriii » Unassigned
Status: Active » Needs review
FileSize
3.43 KB
star-szr’s picture

Status: Needs review » Needs work
+++ b/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php
@@ -104,6 +104,27 @@ public function testActiveTheme() {
+    $loader = new \Twig_Loader_String();

Use \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!

lauriii’s picture

Status: Needs work » Needs review
FileSize
3.73 KB
926 bytes

Thanks for the review @Cottser!

joelpittet’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

Changed 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

star-szr’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
5.74 KB
2.01 KB

Here's a usage, this shows how this can help us to simplify preprocess in some cases.

Status: Needs review » Needs work

The last submitted patch, 8: add_twig_filter_for-2571561-8.patch, failed testing.

joelpittet’s picture

Status: Needs work » Needs review
FileSize
1.33 KB
6.27 KB

We missed a couple things. This should resolve those things. Like having a usecase to show how this fixes unnecessary preprocess! +1 to that.

Status: Needs review » Needs work

The last submitted patch, 10: add_twig_filter_for-2571561-10.patch, failed testing.

joelpittet’s picture

Status: Needs work » Needs review

Random CI fail.

lauriii’s picture

Status: Needs review » Reviewed & tested by the community

The changes @Cottser and @joelpittet made after #7 looks sane.

Anonymous’s picture

Issue summary: View changes
FileSize
42.38 KB

I did some manual testing using an inline template render array, and this works as expected.

So RTBC+1 from me.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

This 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!

  • alexpott committed b0f2f83 on 8.0.x
    Issue #2571561 by lauriii, Cottser, joelpittet, pjonckiere: Add Twig...

Status: Fixed » Closed (fixed)

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