@@ -4,8 +4,6 @@ SonataAdminBundle integration
4
4
If we take our example with member that has ` gender ` .
5
5
And let's say we want to build a SonataAdmin for this model.
6
6
7
- ## The admin class
8
-
9
7
``` php
10
8
<?php
11
9
@@ -16,72 +14,68 @@ use Sonata\AdminBundle\Datagrid\DatagridMapper;
16
14
use Sonata\AdminBundle\Datagrid\ListMapper;
17
15
use Sonata\AdminBundle\Form\FormMapper;
18
16
use Sonata\AdminBundle\Show\ShowMapper;
17
+ use Sonata\AdminBundle\Templating\TemplateRegistry;
18
+ use Sonata\DoctrineORMAdminBundle\Filter\ChoiceFilter; // for ORM entities only
19
+ use Yokai\EnumBundle\EnumRegistry;
19
20
use Yokai\EnumBundle\Form\Type\EnumType;
20
21
21
22
class MemberAdmin extends AbstractAdmin
22
23
{
24
+ /**
25
+ * @var EnumRegistry
26
+ */
27
+ private $enums;
28
+
29
+ public function __construct(EnumRegistry $enums, $code, $class, $baseControllerName = null)
30
+ {
31
+ $this->enums = $enums;
32
+ parent::__construct($code, $class, $baseControllerName);
33
+ }
34
+
23
35
protected function configureListFields(ListMapper $list): void
24
36
{
25
37
$list
26
- ->add('gender', null, [
27
- 'template' => 'admin/list_gender.html.twig',
38
+ ->add('gender', TemplateRegistry::TYPE_CHOICE, [
39
+ 'choices' => $this->enums->get(GenderEnum::class)->getChoices(),
40
+ 'catalog' => false, // enum is self translated
28
41
])
29
- //...
30
42
;
31
43
}
32
44
33
45
protected function configureDatagridFilters(DatagridMapper $filter): void
34
46
{
35
47
$filter
36
- ->add('gender', 'doctrine_orm_choice' , [
48
+ ->add('gender', ChoiceFilter::class , [
37
49
'field_type' => EnumType::class,
38
50
'field_options' => [
39
51
'enum' => GenderEnum::class,
40
52
],
41
53
])
42
- //...
43
54
;
44
55
}
45
56
46
57
protected function configureFormFields(FormMapper $form): void
47
58
{
48
59
$form
49
- // Let the bundle guess the form type for you (requires that you configured the validation)
60
+ // the bundle guess the form type for you, if you configured validation
50
61
->add('gender')
51
- //...
52
62
;
53
63
}
54
64
55
65
protected function configureShowFields(ShowMapper $form): void
56
66
{
57
67
$form
58
- ->add('gender', null, [
59
- 'template' => 'admin/show_gender.html.twig',
68
+ ->add('gender', TemplateRegistry::TYPE_CHOICE, [
69
+ 'choices' => $this->enums->get(GenderEnum::class)->getChoices(),
70
+ 'catalog' => false, // enum is self translated
60
71
])
61
- //...
62
72
;
63
73
}
64
74
}
65
75
```
66
76
67
- ## The list templates
68
-
69
- ``` twig
70
- {# admin/list_gender.html.twig #}
71
- {% extends admin.getTemplate('base_list_field') %}
72
-
73
- {% block field %}
74
- {{ value|enum_label('App\\Enum\\GenderEnum') }}
75
- {% endblock %}
76
- ```
77
-
78
- ## The show templates
79
-
80
- ``` twig
81
- {# admin/show_gender.html.twig #}
82
- {% extends admin.getTemplate('base_show_field') %}
83
-
84
- {% block field %}
85
- {{ value|enum_label('App\\Enum\\GenderEnum') }}
86
- {% endblock %}
87
- ```
77
+ There you go, your admin has now enum integrated :
78
+ - list choice filter : [ documentation] ( https://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/filter_field_definition.html )
79
+ - list datagrid column : [ documentation] ( https://sonata-project.org/bundles/admin/3-x/doc/getting_started/the_list_view.html )
80
+ - form choice field : [ documentation] ( https://sonata-project.org/bundles/admin/3-x/doc/getting_started/the_form_view.html )
81
+ - show field : [ documentation] ( https://sonata-project.org/bundles/admin/3-x/doc/getting_started/the_form_view.html )
0 commit comments