diff --git a/core/core.services.yml b/core/core.services.yml index 0a69541..cb91abf 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1452,6 +1452,7 @@ services: calls: - [setUrlGenerator, ['@url_generator']] - [setThemeManager, ['@theme.manager']] + - [setDateFormatter, ['@date.formatter']] # @todo Figure out what to do about debugging functions. # @see https://www.drupal.org/node/1804998 twig.extension.debug: diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php index 735a434..e2b45ae 100644 --- a/core/lib/Drupal/Core/Template/TwigExtension.php +++ b/core/lib/Drupal/Core/Template/TwigExtension.php @@ -15,6 +15,7 @@ use Drupal\Component\Utility\Html; use Drupal\Component\Utility\SafeMarkup; use Drupal\Component\Utility\SafeStringInterface; +use Drupal\Core\Datetime\DateFormatter; use Drupal\Core\Render\RenderableInterface; use Drupal\Core\Render\RendererInterface; use Drupal\Core\Routing\UrlGeneratorInterface; @@ -52,6 +53,13 @@ class TwigExtension extends \Twig_Extension { protected $themeManager; /** + * The date formatter. + * + * @var \Drupal\Core\Datetime\DateFormatter + */ + protected $dateFormatter; + + /** * Constructs \Drupal\Core\Template\TwigExtension. * * @param \Drupal\Core\Render\RendererInterface $renderer @@ -103,6 +111,19 @@ public function setThemeManager(ThemeManagerInterface $theme_manager) { } /** + * 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} */ public function getFunctions() { @@ -152,6 +173,7 @@ public function getFilters() { new \Twig_SimpleFilter('clean_id', '\Drupal\Component\Utility\Html::getId'), // This filter will render a renderable array to use the string results. new \Twig_SimpleFilter('render', array($this, 'renderVar')), + new \Twig_SimpleFilter('format_date', array($this->dateFormatter, 'format')), ); } diff --git a/core/modules/locale/locale.pages.inc b/core/modules/locale/locale.pages.inc index c0e2b65..83bdebb 100644 --- a/core/modules/locale/locale.pages.inc +++ b/core/modules/locale/locale.pages.inc @@ -52,18 +52,8 @@ function locale_translation_manual_status() { * @see \Drupal\locale\Form\TranslationStatusForm */ function template_preprocess_locale_translation_update_info(array &$variables) { - // Build output for available updates. - if (isset($variables['updates'])) { - $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; - } - } + foreach ($variables['updates'] as $update) { + $variables['modules'][] = $update['name']; } } diff --git a/core/modules/locale/templates/locale-translation-update-info.html.twig b/core/modules/locale/templates/locale-translation-update-info.html.twig index b3f81e1..a66ee54 100644 --- a/core/modules/locale/templates/locale-translation-update-info.html.twig +++ b/core/modules/locale/templates/locale-translation-update-info.html.twig @@ -7,7 +7,7 @@ * * Available variables: * - 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. * * @see template_preprocess_locale_translation_update_info() @@ -34,7 +34,7 @@ {% if available_updates %} {% endif %} diff --git a/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php b/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php index 3e78e91..d01641b 100644 --- a/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php +++ b/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php @@ -10,6 +10,7 @@ use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Render\RenderableInterface; use Drupal\Core\Render\RendererInterface; +use Drupal\Core\Template\Loader\StringLoader; use Drupal\Core\Template\TwigEnvironment; use Drupal\Core\Template\TwigExtension; use Drupal\Tests\UnitTestCase; @@ -104,6 +105,27 @@ public function testActiveTheme() { } /** + * 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. * * @covers ::escapeFilter