Browse Source

Merge pull request #3257 from lightglitch/list_datetime_timezone

Add support for timezone on datetime list format
Sullivan SENECHAL 9 years ago
parent
commit
475a1b5439

+ 12 - 1
Resources/doc/reference/field_types.rst

@@ -9,7 +9,7 @@ There are many field types that can be used in the list action or show action :
 * **array**: display value from an array
 * **array**: display value from an array
 * **boolean**: display a green or red picture dependant on the boolean value
 * **boolean**: display a green or red picture dependant on the boolean value
 * **date**: display a formatted date. Accepts an optional ``format`` parameter
 * **date**: display a formatted date. Accepts an optional ``format`` parameter
-* **datetime**: display a formatted date and time. Accepts an optional ``format`` parameter
+* **datetime**: display a formatted date and time. Accepts an optional ``format`` and ``timezone`` parameter
 * **text**: display a text
 * **text**: display a text
 * **textarea**: display a textarea
 * **textarea**: display a textarea
 * **trans**: translate the value with a provided ``catalogue`` option
 * **trans**: translate the value with a provided ``catalogue`` option
@@ -34,6 +34,17 @@ This is currently limited to scalar types (text, integer, url...).
 
 
     In ``date`` and ``datetime`` field types, ``format`` pattern must match twig's
     In ``date`` and ``datetime`` field types, ``format`` pattern must match twig's
     ``date`` filter specification, available at: `http://twig.sensiolabs.org/doc/filters/date.html <http://twig.sensiolabs.org/doc/filters/date.html>`_
     ``date`` filter specification, available at: `http://twig.sensiolabs.org/doc/filters/date.html <http://twig.sensiolabs.org/doc/filters/date.html>`_
+
+    In ``datetime`` field types, ``timezone`` syntax must match twig's
+    ``date`` filter specification, available at: `http://twig.sensiolabs.org/doc/filters/date.html <http://twig.sensiolabs.org/doc/filters/date.html>`_
+    and php timezone list: `https://php.net/manual/en/timezones.php <https://php.net/manual/en/timezones.php>`_
+    You can use in lists what `view-timezone <http://symfony.com/doc/current/reference/forms/types/datetime.html#view-timezone>`_ allows on forms,
+    a way to render the date in the user timezone.
+.. code-block:: php
+
+    // Store date in UTC but show in the end user timezone
+    $listMapper->add('date', null, array('format' => 'Y-m-d H:i', 'timezone' => 'America/New_York'));
+
     
     
 More types might be provided based on the persistency layer defined. Please refer to their
 More types might be provided based on the persistency layer defined. Please refer to their
 related documentations.
 related documentations.

+ 4 - 1
Resources/views/CRUD/list_datetime.html.twig

@@ -15,7 +15,10 @@ file that was distributed with this source code.
     {%- if value is empty -%}
     {%- if value is empty -%}
         &nbsp;
         &nbsp;
     {%- elseif field_description.options.format is defined -%}
     {%- elseif field_description.options.format is defined -%}
-        {{ value|date(field_description.options.format) }}
+        {% set timezone = field_description.options.timezone is defined ? field_description.options.timezone : null %}
+        {{ value|date(field_description.options.format, timezone) }}
+    {%- elseif field_description.options.timezone is defined -%}
+        {{ value|date(null, field_description.options.timezone) }}
     {%- else -%}
     {%- else -%}
         {{ value|date }}
         {{ value|date }}
     {%- endif -%}
     {%- endif -%}

+ 3 - 0
Tests/Twig/Extension/SonataAdminExtensionTest.php

@@ -261,9 +261,12 @@ class SonataAdminExtensionTest extends \PHPUnit_Framework_TestCase
             array('<td class="sonata-ba-list-field sonata-ba-list-field-textarea" objectId="12345"> Example </td>', 'textarea', 'Example', array()),
             array('<td class="sonata-ba-list-field sonata-ba-list-field-textarea" objectId="12345"> Example </td>', 'textarea', 'Example', array()),
             array('<td class="sonata-ba-list-field sonata-ba-list-field-textarea" objectId="12345"> </td>', 'textarea', null, array()),
             array('<td class="sonata-ba-list-field sonata-ba-list-field-textarea" objectId="12345"> </td>', 'textarea', null, array()),
             array('<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345"> December 24, 2013 10:11 </td>', 'datetime', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')), array()),
             array('<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345"> December 24, 2013 10:11 </td>', 'datetime', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')), array()),
+            array('<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345"> December 24, 2013 18:11 </td>', 'datetime', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('UTC')), array('timezone' => 'Asia/Hong_Kong')),
             array('<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345"> &nbsp; </td>', 'datetime', null, array()),
             array('<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345"> &nbsp; </td>', 'datetime', null, array()),
             array('<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345"> 24.12.2013 10:11:12 </td>', 'datetime', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')), array('format' => 'd.m.Y H:i:s')),
             array('<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345"> 24.12.2013 10:11:12 </td>', 'datetime', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')), array('format' => 'd.m.Y H:i:s')),
             array('<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345"> &nbsp; </td>', 'datetime', null, array('format' => 'd.m.Y H:i:s')),
             array('<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345"> &nbsp; </td>', 'datetime', null, array('format' => 'd.m.Y H:i:s')),
+            array('<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345"> 24.12.2013 18:11:12 </td>', 'datetime', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('UTC')), array('format' => 'd.m.Y H:i:s', 'timezone' => 'Asia/Hong_Kong')),
+            array('<td class="sonata-ba-list-field sonata-ba-list-field-datetime" objectId="12345"> &nbsp; </td>', 'datetime', null, array('format' => 'd.m.Y H:i:s', 'timezone' => 'Asia/Hong_Kong')),
             array('<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="12345"> December 24, 2013 </td>', 'date', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')), array()),
             array('<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="12345"> December 24, 2013 </td>', 'date', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')), array()),
             array('<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="12345"> &nbsp; </td>', 'date', null, array()),
             array('<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="12345"> &nbsp; </td>', 'date', null, array()),
             array('<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="12345"> 24.12.2013 </td>', 'date', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')), array('format' => 'd.m.Y')),
             array('<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="12345"> 24.12.2013 </td>', 'date', new \DateTime('2013-12-24 10:11:12', new \DateTimeZone('Europe/London')), array('format' => 'd.m.Y')),