Skip to content

Commit 4de3710

Browse files
authored
Merge pull request #33 from yokai-php/sonata-admin-doc-update
Updated sonata admin doc to use choice field types
2 parents b8c72a6 + 0821ed6 commit 4de3710

File tree

1 file changed

+27
-33
lines changed

1 file changed

+27
-33
lines changed

doc/sonata-admin.md

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ SonataAdminBundle integration
44
If we take our example with member that has `gender`.
55
And let's say we want to build a SonataAdmin for this model.
66

7-
## The admin class
8-
97
```php
108
<?php
119

@@ -16,72 +14,68 @@ use Sonata\AdminBundle\Datagrid\DatagridMapper;
1614
use Sonata\AdminBundle\Datagrid\ListMapper;
1715
use Sonata\AdminBundle\Form\FormMapper;
1816
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;
1920
use Yokai\EnumBundle\Form\Type\EnumType;
2021

2122
class MemberAdmin extends AbstractAdmin
2223
{
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+
2335
protected function configureListFields(ListMapper $list): void
2436
{
2537
$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
2841
])
29-
//...
3042
;
3143
}
3244

3345
protected function configureDatagridFilters(DatagridMapper $filter): void
3446
{
3547
$filter
36-
->add('gender', 'doctrine_orm_choice', [
48+
->add('gender', ChoiceFilter::class, [
3749
'field_type' => EnumType::class,
3850
'field_options' => [
3951
'enum' => GenderEnum::class,
4052
],
4153
])
42-
//...
4354
;
4455
}
4556

4657
protected function configureFormFields(FormMapper $form): void
4758
{
4859
$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
5061
->add('gender')
51-
//...
5262
;
5363
}
5464

5565
protected function configureShowFields(ShowMapper $form): void
5666
{
5767
$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
6071
])
61-
//...
6272
;
6373
}
6474
}
6575
```
6676

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

Comments
 (0)