Browse Source

Added option to inverse background for boolean fields (#4604)

Xymanek 7 năm trước cách đây
mục cha
commit
e947b56868

+ 10 - 2
Resources/doc/reference/action_list.rst

@@ -142,8 +142,10 @@ Available types and associated options
 | array     |                | Displays an array                                                     |
 +-----------+----------------+-----------------------------------------------------------------------+
 | boolean   | ajax_hidden    | Yes/No; ajax_hidden allows to hide list field during an AJAX context. |
-+-----------+----------------+-----------------------------------------------------------------------+
-| boolean   | editable       | Yes/No; editable allows to edit directly from the list if authorized. |
++           +----------------+-----------------------------------------------------------------------+
+|           | editable       | Yes/No; editable allows to edit directly from the list if authorized. |
++           +----------------+-----------------------------------------------------------------------+
+|           | inverse        | Yes/No; reverses the background color (green for false, red for true) |
 +-----------+----------------+-----------------------------------------------------------------------+
 | choice    | choices        | Possible choices                                                      |
 +           +----------------+-----------------------------------------------------------------------+
@@ -192,6 +194,12 @@ Available types and associated options
 
 If you have the SonataDoctrineORMAdminBundle installed, you have access to more field types, see `SonataDoctrineORMAdminBundle Documentation <https://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/list_field_definition.html>`_.
 
+.. note::
+
+    It is better to prefer non negative notions when possible for boolean
+    values so use the ``inverse`` option if you really cannot find a good enough
+    antonym for the name you have.
+
 Customizing the query used to generate the list
 -----------------------------------------------
 

+ 26 - 0
Resources/views/CRUD/display_boolean.html.twig

@@ -0,0 +1,26 @@
+{#
+
+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.
+
+#}
+
+{%- spaceless %}
+    {% if value %}
+        {% set text = 'label_type_yes'|trans({}, 'SonataAdminBundle') %}
+    {% else %}
+        {% set text = 'label_type_no'|trans({}, 'SonataAdminBundle') %}
+    {% endif %}
+
+    {% if field_description.options.inverse|default(false) ? not value : value %}
+        {% set class = 'label-success' %}
+    {% else %}
+        {% set class = 'label-danger' %}
+    {% endif %}
+
+    <span class="label {{ class }}">{{ text }}</span>
+{% endspaceless -%}

+ 1 - 7
Resources/views/CRUD/list_boolean.html.twig

@@ -24,11 +24,5 @@ file that was distributed with this source code.
 {% endif %}
 
 {% block field %}
-    {% spaceless %}
-        {% if value %}
-            <span class="label label-success">{%- trans from 'SonataAdminBundle' %}label_type_yes{% endtrans -%}</span>
-        {% else %}
-            <span class="label label-danger">{%- trans from 'SonataAdminBundle' %}label_type_no{% endtrans -%}</span>
-        {% endif %}
-    {% endspaceless %}
+    {%- include 'SonataAdminBundle:CRUD:display_boolean.html.twig' -%}
 {% endblock %}

+ 1 - 7
Resources/views/CRUD/show_boolean.html.twig

@@ -12,11 +12,5 @@ file that was distributed with this source code.
 {% extends 'SonataAdminBundle:CRUD:base_show_field.html.twig' %}
 
 {% block field %}
-{% spaceless %}
-    {% if value %}
-        <span class="label label-success">{%- trans from 'SonataAdminBundle' %}label_type_yes{% endtrans -%}</span>
-    {% else %}
-        <span class="label label-danger">{%- trans from 'SonataAdminBundle' %}label_type_no{% endtrans -%}</span>
-    {% endif %}
-{% endspaceless %}
+    {%- include 'SonataAdminBundle:CRUD:display_boolean.html.twig' -%}
 {% endblock %}

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

@@ -1470,7 +1470,19 @@ EOT
                 true,
                 array(),
             ),
+            array(
+                '<th>Data</th> <td><span class="label label-danger">yes</span></td>',
+                'boolean',
+                true,
+                array('inverse' => true),
+            ),
             array('<th>Data</th> <td><span class="label label-danger">no</span></td>', 'boolean', false, array()),
+            array(
+                '<th>Data</th> <td><span class="label label-success">no</span></td>',
+                'boolean',
+                false,
+                array('inverse' => true),
+            ),
             array(
                 '<th>Data</th> <td> Delete </td>',
                 'trans',