Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
D
drupal
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Insights
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Locked Files
Drupal.org issue queue
Drupal.org issue queue
Security & Compliance
Security & Compliance
Dependency List
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
project
drupal
Commits
b0f2f838
Commit
b0f2f838
authored
Sep 27, 2015
by
alexpott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue
#2571561
by lauriii, Cottser, joelpittet, pjonckiere: Add Twig filter for date formatting
parent
8afa4070
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
53 additions
and
18 deletions
+53
-18
core/core.services.yml
core/core.services.yml
+1
-0
core/lib/Drupal/Core/Template/TwigExtension.php
core/lib/Drupal/Core/Template/TwigExtension.php
+22
-0
core/modules/locale/locale.pages.inc
core/modules/locale/locale.pages.inc
+2
-12
core/modules/locale/templates/locale-translation-update-info.html.twig
...locale/templates/locale-translation-update-info.html.twig
+6
-6
core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php
core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php
+22
-0
No files found.
core/core.services.yml
View file @
b0f2f838
...
@@ -1450,6 +1450,7 @@ services:
...
@@ -1450,6 +1450,7 @@ services:
calls
:
calls
:
-
[
setUrlGenerator
,
[
'
@url_generator'
]]
-
[
setUrlGenerator
,
[
'
@url_generator'
]]
-
[
setThemeManager
,
[
'
@theme.manager'
]]
-
[
setThemeManager
,
[
'
@theme.manager'
]]
-
[
setDateFormatter
,
[
'
@date.formatter'
]]
# @todo Figure out what to do about debugging functions.
# @todo Figure out what to do about debugging functions.
# @see https://www.drupal.org/node/1804998
# @see https://www.drupal.org/node/1804998
twig.extension.debug
:
twig.extension.debug
:
...
...
core/lib/Drupal/Core/Template/TwigExtension.php
View file @
b0f2f838
...
@@ -15,6 +15,7 @@
...
@@ -15,6 +15,7 @@
use
Drupal\Component\Utility\Html
;
use
Drupal\Component\Utility\Html
;
use
Drupal\Component\Utility\SafeMarkup
;
use
Drupal\Component\Utility\SafeMarkup
;
use
Drupal\Component\Utility\SafeStringInterface
;
use
Drupal\Component\Utility\SafeStringInterface
;
use
Drupal\Core\Datetime\DateFormatter
;
use
Drupal\Core\Render\RenderableInterface
;
use
Drupal\Core\Render\RenderableInterface
;
use
Drupal\Core\Render\RendererInterface
;
use
Drupal\Core\Render\RendererInterface
;
use
Drupal\Core\Routing\UrlGeneratorInterface
;
use
Drupal\Core\Routing\UrlGeneratorInterface
;
...
@@ -51,6 +52,13 @@ class TwigExtension extends \Twig_Extension {
...
@@ -51,6 +52,13 @@ class TwigExtension extends \Twig_Extension {
*/
*/
protected
$themeManager
;
protected
$themeManager
;
/**
* The date formatter.
*
* @var \Drupal\Core\Datetime\DateFormatter
*/
protected
$dateFormatter
;
/**
/**
* Constructs \Drupal\Core\Template\TwigExtension.
* Constructs \Drupal\Core\Template\TwigExtension.
*
*
...
@@ -102,6 +110,19 @@ public function setThemeManager(ThemeManagerInterface $theme_manager) {
...
@@ -102,6 +110,19 @@ public function setThemeManager(ThemeManagerInterface $theme_manager) {
return
$this
;
return
$this
;
}
}
/**
* Sets the date formatter.
*
* @param \Drupal\Core\Datetime\DateFormatter $date_formatter
* The date formatter.
*
* @return $this
*/
public
function
setDateFormatter
(
DateFormatter
$date_formatter
)
{
$this
->
dateFormatter
=
$date_formatter
;
return
$this
;
}
/**
/**
* {@inheritdoc}
* {@inheritdoc}
*/
*/
...
@@ -152,6 +173,7 @@ public function getFilters() {
...
@@ -152,6 +173,7 @@ public function getFilters() {
new
\Twig_SimpleFilter
(
'clean_id'
,
'\Drupal\Component\Utility\Html::getId'
),
new
\Twig_SimpleFilter
(
'clean_id'
,
'\Drupal\Component\Utility\Html::getId'
),
// This filter will render a renderable array to use the string results.
// This filter will render a renderable array to use the string results.
new
\Twig_SimpleFilter
(
'render'
,
array
(
$this
,
'renderVar'
)),
new
\Twig_SimpleFilter
(
'render'
,
array
(
$this
,
'renderVar'
)),
new
\Twig_SimpleFilter
(
'format_date'
,
array
(
$this
->
dateFormatter
,
'format'
)),
);
);
}
}
...
...
core/modules/locale/locale.pages.inc
View file @
b0f2f838
...
@@ -52,18 +52,8 @@ function locale_translation_manual_status() {
...
@@ -52,18 +52,8 @@ function locale_translation_manual_status() {
* @see \Drupal\locale\Form\TranslationStatusForm
* @see \Drupal\locale\Form\TranslationStatusForm
*/
*/
function
template_preprocess_locale_translation_update_info
(
array
&
$variables
)
{
function
template_preprocess_locale_translation_update_info
(
array
&
$variables
)
{
// Build output for available updates.
foreach
(
$variables
[
'updates'
]
as
$update
)
{
if
(
isset
(
$variables
[
'updates'
]))
{
$variables
[
'modules'
][]
=
$update
[
'name'
];
$variables
[
'available_updates'
]
=
[];
if
(
$variables
[
'updates'
])
{
foreach
(
$variables
[
'updates'
]
as
$update
)
{
$variables
[
'modules'
][]
=
$update
[
'name'
];
// Format date for Twig template.
$release
=
$update
;
$release
[
'date'
]
=
\Drupal
::
service
(
'date.formatter'
)
->
format
(
$update
[
'timestamp'
],
'html_date'
);
$variables
[
'available_updates'
][]
=
$release
;
}
}
}
}
}
}
...
...
core/modules/locale/templates/locale-translation-update-info.html.twig
View file @
b0f2f838
...
@@ -7,7 +7,7 @@
...
@@ -7,7 +7,7 @@
*
*
* Available variables:
* Available variables:
* - modules: A list of modules names that have available translation updates.
* - modules: A list of modules names that have available translation updates.
* -
available_
updates: A list of available translation updates.
* - updates: A list of available translation updates.
* - not_found: A list of modules missing translation updates.
* - not_found: A list of modules missing translation updates.
*
*
* @see template_preprocess_locale_translation_update_info()
* @see template_preprocess_locale_translation_update_info()
...
@@ -29,12 +29,12 @@
...
@@ -29,12 +29,12 @@
{%
-
endtrans
-
%}
{%
-
endtrans
-
%}
</span>
</span>
{%
endif
%}
{%
endif
%}
{%
if
available_
updates
or
not_found
%}
{%
if
updates
or
not_found
%}
<div
class=
"locale-translation-update__details"
>
<div
class=
"locale-translation-update__details"
>
{%
if
available_
updates
%}
{%
if
updates
%}
<ul>
<ul>
{%
for
update
in
available_
updates
%}
{%
for
update
in
updates
%}
<li>
{{
update.name
}}
(
{{
update.
date
}}
)
</li>
<li>
{{
update.name
}}
(
{{
update.
timestamp
|
format_date
(
'html_date'
)
}}
)
</li>
{%
endfor
%}
{%
endfor
%}
</ul>
</ul>
{%
endif
%}
{%
endif
%}
...
@@ -43,7 +43,7 @@
...
@@ -43,7 +43,7 @@
Prefix the missing updates list if there is an available updates lists
Prefix the missing updates list if there is an available updates lists
before it.
before it.
#}
#}
{%
if
available_
updates
%}
{%
if
updates
%}
{{
'Missing translations for:'
|
t
}}
{{
'Missing translations for:'
|
t
}}
{%
endif
%}
{%
endif
%}
{%
if
not_found
%}
{%
if
not_found
%}
...
...
core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php
View file @
b0f2f838
...
@@ -10,6 +10,7 @@
...
@@ -10,6 +10,7 @@
use
Drupal\Component\Utility\SafeMarkup
;
use
Drupal\Component\Utility\SafeMarkup
;
use
Drupal\Core\Render\RenderableInterface
;
use
Drupal\Core\Render\RenderableInterface
;
use
Drupal\Core\Render\RendererInterface
;
use
Drupal\Core\Render\RendererInterface
;
use
Drupal\Core\Template\Loader\StringLoader
;
use
Drupal\Core\Template\TwigEnvironment
;
use
Drupal\Core\Template\TwigEnvironment
;
use
Drupal\Core\Template\TwigExtension
;
use
Drupal\Core\Template\TwigExtension
;
use
Drupal\Tests\UnitTestCase
;
use
Drupal\Tests\UnitTestCase
;
...
@@ -103,6 +104,27 @@ public function testActiveTheme() {
...
@@ -103,6 +104,27 @@ public function testActiveTheme() {
$this
->
assertEquals
(
'test_theme'
,
$result
);
$this
->
assertEquals
(
'test_theme'
,
$result
);
}
}
/**
* Tests the format_date filter.
*/
public
function
testFormatDate
()
{
$date_formatter
=
$this
->
getMockBuilder
(
'\Drupal\Core\Datetime\DateFormatter'
)
->
disableOriginalConstructor
()
->
getMock
();
$date_formatter
->
expects
(
$this
->
exactly
(
2
))
->
method
(
'format'
)
->
willReturn
(
'1978-11-19'
);
$renderer
=
$this
->
getMock
(
'\Drupal\Core\Render\RendererInterface'
);
$extension
=
new
TwigExtension
(
$renderer
);
$extension
->
setDateFormatter
(
$date_formatter
);
$loader
=
new
StringLoader
();
$twig
=
new
\Twig_Environment
(
$loader
);
$twig
->
addExtension
(
$extension
);
$result
=
$twig
->
render
(
'{{ time|format_date("html_date") }}'
);
$this
->
assertEquals
(
$date_formatter
->
format
(
'html_date'
),
$result
);
}
/**
/**
* Tests the escaping of objects implementing SafeStringInterface.
* Tests the escaping of objects implementing SafeStringInterface.
*
*
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment