Browse Source

Merge pull request #1657 from tiagojsag/datagrid_choice

List and Show choice type
Thomas 11 years ago
parent
commit
f2356eaa52

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

@@ -17,6 +17,7 @@ There are many field types that can be used in the list action or show action :
 * decimal: display a number
 * currency: display a number with a provided ``currency`` option
 * percent: display a percentage
+* choice: uses the given value as index for the ``choices`` array and displays (and optionally translates) the matching value
 
 .. note::
 

+ 23 - 0
Resources/views/CRUD/list_choice.html.twig

@@ -0,0 +1,23 @@
+{#
+
+This file is part of the Sonata package.
+
+(c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+
+For the full copyright and license information, please view the LICENSE
+file that was distributed with this source code.
+
+#}
+
+{% extends admin.getTemplate('base_list_field') %}
+
+{% block field %}
+    {% if field_description.options.choices and value in field_description.options.choices|keys %}
+        {% if field_description.options.catalogue is not defined %}
+            {% set value = field_description.options.choices[value] %}
+        {% else %}
+            {% set value = field_description.options.choices[value]|trans({}, field_description.options.catalogue) %}
+        {% endif %}
+    {% endif %}
+    {{ parent() }}
+{% endblock %}

+ 27 - 0
Resources/views/CRUD/show_choice.html.twig

@@ -0,0 +1,27 @@
+{#
+
+This file is part of the Sonata package.
+
+(c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+
+For the full copyright and license information, please view the LICENSE
+file that was distributed with this source code.
+
+#}
+{% extends 'SonataAdminBundle:CRUD:base_show_field.html.twig' %}
+
+{% block field%}
+    {% if field_description.options.choices and value in field_description.options.choices|keys %}
+        {% if field_description.options.catalogue is not defined %}
+            {% set value = field_description.options.choices[value] %}
+        {% else %}
+            {% set value = field_description.options.choices[value]|trans({}, field_description.options.catalogue) %}
+        {% endif %}
+    {% endif %}
+
+    {% if field_description.options.safe %}
+        {{ value|raw }}
+    {% else %}
+        {{ value }}
+    {% endif %}
+{% endblock %}

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

@@ -198,6 +198,8 @@ class SonataAdminExtensionTest extends \PHPUnit_Framework_TestCase
                         return 'SonataAdminBundle:CRUD:list_currency.html.twig';
                     case 'percent':
                         return 'SonataAdminBundle:CRUD:list_percent.html.twig';
+                    case 'choice':
+                        return 'SonataAdminBundle:CRUD:list_choice.html.twig';
                     case 'array':
                         return 'SonataAdminBundle:CRUD:list_array.html.twig';
                     case 'trans':
@@ -300,6 +302,8 @@ class SonataAdminExtensionTest extends \PHPUnit_Framework_TestCase
                         return 'SonataAdminBundle:CRUD:show_currency.html.twig';
                     case 'percent':
                         return 'SonataAdminBundle:CRUD:show_percent.html.twig';
+                    case 'choice':
+                        return 'SonataAdminBundle:CRUD:show_choice.html.twig';
                     case 'array':
                         return 'SonataAdminBundle:CRUD:show_array.html.twig';
                     case 'trans':

+ 1 - 0
composer.json

@@ -37,6 +37,7 @@
     },
     "require-dev": {
         "jms/translation-bundle": "*@dev",
+        "symfony/yaml": "~2.2",
         "sonata-project/intl-bundle": "~2.1"
     },
     "suggest": {