Skip to content

Commit

Permalink
Add support for displaying enums
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu committed Jan 15, 2022
1 parent 6c649e0 commit c6eb4d3
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/reference/field_types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/FieldDescription/FieldDescriptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
14 changes: 14 additions & 0 deletions src/Resources/views/CRUD/display_enum.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{#
This file is part of the Sonata package.
(c) Thomas Rabaix <[email protected]>
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
#}

{%- apply spaceless %}
{{ value.name }}
{% endapply -%}
16 changes: 16 additions & 0 deletions src/Resources/views/CRUD/list_enum.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{#
This file is part of the Sonata package.
(c) Thomas Rabaix <[email protected]>
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 %}
16 changes: 16 additions & 0 deletions src/Resources/views/CRUD/show_enum.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{#
This file is part of the Sonata package.
(c) Thomas Rabaix <[email protected]>
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 %}
2 changes: 2 additions & 0 deletions src/Templating/TemplateRegistryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down
21 changes: 21 additions & 0 deletions tests/Fixtures/Enum/Suit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* 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';
}
29 changes: 27 additions & 2 deletions tests/Twig/Extension/RenderElementExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -516,7 +517,7 @@ public function testRenderRelationElementWithClosure(): void
*/
public function getRenderListElementTests(): array
{
return [
$elements = [
[
'<td class="sonata-ba-list-field sonata-ba-list-field-string" objectId="12345"> Example </td>',
FieldDescriptionInterface::TYPE_STRING,
Expand Down Expand Up @@ -1496,14 +1497,26 @@ 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[] = [
'<td class="sonata-ba-list-field sonata-ba-list-field-enum" objectId="12345"> Hearts </td>',
FieldDescriptionInterface::TYPE_ENUM,
Suit::Hearts,
[],
];
}

return $elements;
}

/**
* @phpstan-return array<array{string, string, mixed, array<string, mixed>}>
*/
public function getRenderViewElementTests(): array
{
return [
$elements = [
['<th>Data</th> <td>Example</td>', FieldDescriptionInterface::TYPE_STRING, 'Example', ['safe' => false]],
['<th>Data</th> <td>Example</td>', FieldDescriptionInterface::TYPE_STRING, 'Example', ['safe' => false]],
['<th>Data</th> <td>Example</td>', FieldDescriptionInterface::TYPE_TEXTAREA, 'Example', ['safe' => false]],
Expand Down Expand Up @@ -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[] = [
'<th>Data</th> <td>Hearts</td>',
FieldDescriptionInterface::TYPE_ENUM,
Suit::Hearts,
[],
];
}

return $elements;
}

/**
Expand Down

0 comments on commit c6eb4d3

Please sign in to comment.