Commit b0f2f838 authored by alexpott's avatar alexpott

Issue #2571561 by lauriii, Cottser, joelpittet, pjonckiere: Add Twig filter for date formatting

parent 8afa4070
...@@ -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:
......
...@@ -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')),
); );
} }
......
...@@ -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;
}
}
} }
} }
......
...@@ -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 %}
......
...@@ -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.
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment