From c6eb4d3ac02a9a8873b14b3010ff542065a43b7d Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Sat, 15 Jan 2022 09:32:35 +0100 Subject: [PATCH] Add support for displaying enums --- docs/reference/field_types.rst | 1 + .../FieldDescriptionInterface.php | 1 + .../views/CRUD/display_enum.html.twig | 14 +++++++++ src/Resources/views/CRUD/list_enum.html.twig | 16 ++++++++++ src/Resources/views/CRUD/show_enum.html.twig | 16 ++++++++++ src/Templating/TemplateRegistryInterface.php | 2 ++ tests/Fixtures/Enum/Suit.php | 21 ++++++++++++++ .../Extension/RenderElementExtensionTest.php | 29 +++++++++++++++++-- 8 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 src/Resources/views/CRUD/display_enum.html.twig create mode 100644 src/Resources/views/CRUD/list_enum.html.twig create mode 100644 src/Resources/views/CRUD/show_enum.html.twig create mode 100644 tests/Fixtures/Enum/Suit.php diff --git a/docs/reference/field_types.rst b/docs/reference/field_types.rst index 58ddd168248..c07fabf70d5 100644 --- a/docs/reference/field_types.rst +++ b/docs/reference/field_types.rst @@ -16,6 +16,7 @@ Fieldtype Description ``FieldDescriptionInterface::TYPE_DATETIME`` display a formatted date and time. Accepts the options ``format`` and ``timezone`` ``FieldDescriptionInterface::TYPE_STRING`` display a text ``FieldDescriptionInterface::TYPE_EMAIL`` display a mailto link. Accepts the options ``as_string``, ``subject`` and ``body`` +``FieldDescriptionInterface::TYPE_ENUM`` display the name of a backed enum ``FieldDescriptionInterface::TYPE_TEXTAREA`` display a textarea ``FieldDescriptionInterface::TYPE_TRANS`` translate the value with a provided ``catalogue`` (translation domain) and ``format`` (sprintf format) option ``FieldDescriptionInterface::TYPE_FLOAT`` display a number diff --git a/src/FieldDescription/FieldDescriptionInterface.php b/src/FieldDescription/FieldDescriptionInterface.php index a5b1f7a39e9..0f920a082f3 100644 --- a/src/FieldDescription/FieldDescriptionInterface.php +++ b/src/FieldDescription/FieldDescriptionInterface.php @@ -44,6 +44,7 @@ interface FieldDescriptionInterface public const TYPE_DATETIME = 'datetime'; public const TYPE_TEXTAREA = 'textarea'; public const TYPE_EMAIL = 'email'; + public const TYPE_ENUM = 'enum'; public const TYPE_TRANS = 'trans'; public const TYPE_STRING = 'string'; public const TYPE_INTEGER = 'integer'; diff --git a/src/Resources/views/CRUD/display_enum.html.twig b/src/Resources/views/CRUD/display_enum.html.twig new file mode 100644 index 00000000000..0a2a92062f2 --- /dev/null +++ b/src/Resources/views/CRUD/display_enum.html.twig @@ -0,0 +1,14 @@ +{# + +This file is part of the Sonata package. + +(c) Thomas Rabaix + +For the full copyright and license information, please view the LICENSE +file that was distributed with this source code. + +#} + +{%- apply spaceless %} + {{ value.name }} +{% endapply -%} diff --git a/src/Resources/views/CRUD/list_enum.html.twig b/src/Resources/views/CRUD/list_enum.html.twig new file mode 100644 index 00000000000..e312a1ac9c8 --- /dev/null +++ b/src/Resources/views/CRUD/list_enum.html.twig @@ -0,0 +1,16 @@ +{# + +This file is part of the Sonata package. + +(c) Thomas Rabaix + +For the full copyright and license information, please view the LICENSE +file that was distributed with this source code. + +#} + +{% extends get_admin_template('base_list_field', admin.code) %} + +{% block field %} + {%- include '@SonataAdmin/CRUD/display_enum.html.twig' with { value: value } only -%} +{% endblock %} diff --git a/src/Resources/views/CRUD/show_enum.html.twig b/src/Resources/views/CRUD/show_enum.html.twig new file mode 100644 index 00000000000..12f23300277 --- /dev/null +++ b/src/Resources/views/CRUD/show_enum.html.twig @@ -0,0 +1,16 @@ +{# + +This file is part of the Sonata package. + +(c) Thomas Rabaix + +For the full copyright and license information, please view the LICENSE +file that was distributed with this source code. + +#} + +{% extends '@SonataAdmin/CRUD/base_show_field.html.twig' %} + +{% block field %} + {%- include '@SonataAdmin/CRUD/display_enum.html.twig' with { value: value } only -%} +{% endblock %} diff --git a/src/Templating/TemplateRegistryInterface.php b/src/Templating/TemplateRegistryInterface.php index 984bc76287e..f534ff52b0e 100644 --- a/src/Templating/TemplateRegistryInterface.php +++ b/src/Templating/TemplateRegistryInterface.php @@ -30,6 +30,7 @@ interface TemplateRegistryInterface FieldDescriptionInterface::TYPE_TIME => '@SonataAdmin/CRUD/show_time.html.twig', FieldDescriptionInterface::TYPE_DATETIME => '@SonataAdmin/CRUD/show_datetime.html.twig', FieldDescriptionInterface::TYPE_EMAIL => '@SonataAdmin/CRUD/show_email.html.twig', + FieldDescriptionInterface::TYPE_ENUM => '@SonataAdmin/CRUD/show_enum.html.twig', FieldDescriptionInterface::TYPE_TRANS => '@SonataAdmin/CRUD/show_trans.html.twig', FieldDescriptionInterface::TYPE_STRING => '@SonataAdmin/CRUD/base_show_field.html.twig', FieldDescriptionInterface::TYPE_INTEGER => '@SonataAdmin/CRUD/base_show_field.html.twig', @@ -56,6 +57,7 @@ interface TemplateRegistryInterface FieldDescriptionInterface::TYPE_DATETIME => '@SonataAdmin/CRUD/list_datetime.html.twig', FieldDescriptionInterface::TYPE_TEXTAREA => '@SonataAdmin/CRUD/list_string.html.twig', FieldDescriptionInterface::TYPE_EMAIL => '@SonataAdmin/CRUD/list_email.html.twig', + FieldDescriptionInterface::TYPE_ENUM => '@SonataAdmin/CRUD/list_enum.html.twig', FieldDescriptionInterface::TYPE_TRANS => '@SonataAdmin/CRUD/list_trans.html.twig', FieldDescriptionInterface::TYPE_STRING => '@SonataAdmin/CRUD/list_string.html.twig', FieldDescriptionInterface::TYPE_INTEGER => '@SonataAdmin/CRUD/list_string.html.twig', diff --git a/tests/Fixtures/Enum/Suit.php b/tests/Fixtures/Enum/Suit.php new file mode 100644 index 00000000000..c71c44681fb --- /dev/null +++ b/tests/Fixtures/Enum/Suit.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\AdminBundle\Tests\Fixtures\Enum; + +enum Suit: string { + case Hearts = 'H'; + case Diamonds = 'D'; + case Clubs = 'C'; + case Spades = 'S'; +} diff --git a/tests/Twig/Extension/RenderElementExtensionTest.php b/tests/Twig/Extension/RenderElementExtensionTest.php index 3b9a89e2ff6..3d48fcec56a 100644 --- a/tests/Twig/Extension/RenderElementExtensionTest.php +++ b/tests/Twig/Extension/RenderElementExtensionTest.php @@ -21,6 +21,7 @@ use Sonata\AdminBundle\Templating\MutableTemplateRegistryInterface; use Sonata\AdminBundle\Templating\TemplateRegistryInterface; use Sonata\AdminBundle\Tests\Fixtures\Entity\FooToString; +use Sonata\AdminBundle\Tests\Fixtures\Enum\Suit; use Sonata\AdminBundle\Tests\Fixtures\StubFilesystemLoader; use Sonata\AdminBundle\Twig\Extension\RenderElementExtension; use Sonata\AdminBundle\Twig\Extension\XEditableExtension; @@ -516,7 +517,7 @@ public function testRenderRelationElementWithClosure(): void */ public function getRenderListElementTests(): array { - return [ + $elements = [ [ ' Example ', FieldDescriptionInterface::TYPE_STRING, @@ -1496,6 +1497,18 @@ class="x-editable" ], ], ]; + + // TODO: Remove the "if" check when dropping support of PHP < 8.1 and add the case to the list + if (\PHP_VERSION_ID >= 80100) { + $elements[] = [ + ' Hearts ', + FieldDescriptionInterface::TYPE_ENUM, + Suit::Hearts, + [], + ]; + } + + return $elements; } /** @@ -1503,7 +1516,7 @@ class="x-editable" */ public function getRenderViewElementTests(): array { - return [ + $elements = [ ['Data Example', FieldDescriptionInterface::TYPE_STRING, 'Example', ['safe' => false]], ['Data Example', FieldDescriptionInterface::TYPE_STRING, 'Example', ['safe' => false]], ['Data Example', FieldDescriptionInterface::TYPE_TEXTAREA, 'Example', ['safe' => false]], @@ -2000,6 +2013,18 @@ class="sonata-readmore" ], ], ]; + + // TODO: Remove the "if" check when dropping support of PHP < 8.1 and add the case to the list + if (\PHP_VERSION_ID >= 80100) { + $elements[] = [ + 'Data Hearts', + FieldDescriptionInterface::TYPE_ENUM, + Suit::Hearts, + [], + ]; + } + + return $elements; } /**