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.

Files: 
CommentFileSizeAuthor
#14 Screen Shot 2015-09-25 at 10.59.51.png42.38 KBpjonckiere
#10 add_twig_filter_for-2571561-10.patch6.27 KBjoelpittet
PASSED: [[SimpleTest]]: [PHP 5.5 MySQL] 114,360 pass(es). View
#10 interdiff.txt1.33 KBjoelpittet
#8 interdiff.txt2.01 KBCottser
#8 add_twig_filter_for-2571561-8.patch5.74 KBCottser
FAILED: [[SimpleTest]]: [PHP 5.5 MySQL] 113,594 pass(es), 3 fail(s), and 0 exception(s). View
#6 interdiff.txt926 byteslauriii
#6 add_twig_filter_for-2571561-6.patch3.73 KBlauriii
PASSED: [[SimpleTest]]: [PHP 5.5 MySQL] 113,588 pass(es). View
#4 add_twig_filter_for-2571561-4.patch3.43 KBlauriii
PASSED: [[SimpleTest]]: [PHP 5.5 MySQL] 113,609 pass(es). View

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
PASSED: [[SimpleTest]]: [PHP 5.5 MySQL] 113,609 pass(es). View
Cottser’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] Get ready to upgrade to Twig 2.x.

That's all I found, thanks @lauriii!

lauriii’s picture

Status: Needs work » Needs review
FileSize
3.73 KB
PASSED: [[SimpleTest]]: [PHP 5.5 MySQL] 113,588 pass(es). View
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

Cottser’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
5.74 KB
FAILED: [[SimpleTest]]: [PHP 5.5 MySQL] 113,594 pass(es), 3 fail(s), and 0 exception(s). View
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
PASSED: [[SimpleTest]]: [PHP 5.5 MySQL] 114,360 pass(es). View

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.

pjonckiere’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.