Completed
Pull Request — 3.1 (#348)
by Piotr
01:23
created

Person::initForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace FSi\FixturesBundle\Admin;
13
14
use FSi\Bundle\AdminBundle\Doctrine\Admin\CRUDElement;
15
use FSi\Component\DataGrid\DataGridFactoryInterface;
16
use FSi\Component\DataGrid\DataGridInterface;
17
use FSi\Component\DataSource\DataSourceFactoryInterface;
18
use FSi\Component\DataSource\DataSourceInterface;
19
use FSi\FixturesBundle\Entity;
20
use Symfony\Component\Form\Extension\Core\Type\FormType;
21
use Symfony\Component\Form\Extension\Core\Type\TextType;
22
use Symfony\Component\Form\FormFactoryInterface;
23
use Symfony\Component\Form\FormInterface;
24
25
class Person extends CRUDElement
26
{
27
    public function getId(): string
28
    {
29
        return 'person';
30
    }
31
32
    public function getClassName(): string
33
    {
34
        return Entity\Person::class;
35
    }
36
37
    public function createDataGrid(): DataGridInterface
38
    {
39
        $datagrid = $this->initDataGrid($this->datagridFactory);
40
41
        if (!$datagrid->hasColumnType('batch')) {
42
            $datagrid->addColumn('batch', 'batch', [
43
                'actions' => [
44
                    'delete' => [
45
                        'route_name' => 'fsi_admin_batch',
46
                        'additional_parameters' => ['element' => $this->getId()],
47
                        'label' => 'crud.list.batch.delete'
48
                    ]
49
                ],
50
                'display_order' => -1000
51
            ]);
52
        }
53
54
        return $datagrid;
55
    }
56
57
    protected function initDataGrid(DataGridFactoryInterface $factory): DataGridInterface
58
    {
59
        $datagrid = $factory->createDataGrid($this->getId());
60
61
        $datagrid->addColumn('email', 'text', [
62
            'label' => 'admin.email'
63
        ]);
64
65
        $datagrid->addColumn('actions', 'action', [
66
            'label' => 'admin.news.list.actions',
67
            'field_mapping' => ['id'],
68
            'actions' => [
69
                'edit' => [
70
                    'route_name' => 'fsi_admin_form',
71
                    'additional_parameters' => ['element' => $this->getId()],
72
                    'parameters_field_mapping' => ['id' => 'id']
73
                ]
74
            ]
75
        ]);
76
77
        return $datagrid;
78
    }
79
80
    protected function initDataSource(DataSourceFactoryInterface $factory): DataSourceInterface
81
    {
82
        return $factory->createDataSource(
83
            'doctrine-orm',
84
            ['entity' => $this->getClassName()],
85
            $this->getId()
86
        );
87
    }
88
89
    protected function initForm(FormFactoryInterface $factory, $data = null): FormInterface
90
    {
91
        $builder = $factory->createNamedBuilder(
92
            'person',
93
            FormType::class,
94
            $data,
95
            ['data_class' => $this->getClassName()]
96
        );
97
98
        $builder->add(
99
            'email',
100
            TextType::class,
101
            ['label' => 'admin.email']
102
        );
103
104
        return $builder->getForm();
105
    }
106
}
107