Completed
Pull Request — master (#485)
by Maximilian
01:47
created

AssociationAdmin::configureFormFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Symfony CMF package.
5
 *
6
 * (c) 2011-2017 Symfony CMF
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\DoctrinePHPCRAdminBundle\Tests\Resources\Admin;
13
14
use Doctrine\Bundle\PHPCRBundle\Form\DataTransformer\DocumentToPathTransformer;
15
use Doctrine\Common\Persistence\ManagerRegistry;
16
use Sonata\AdminBundle\Datagrid\DatagridMapper;
17
use Sonata\AdminBundle\Datagrid\ListMapper;
18
use Sonata\AdminBundle\Form\FormMapper;
19
use Sonata\DoctrinePHPCRAdminBundle\Admin\Admin;
20
use Sonata\DoctrinePHPCRAdminBundle\Filter\NodeNameFilter;
21
use Sonata\DoctrinePHPCRAdminBundle\Filter\StringFilter;
22
use Sonata\DoctrinePHPCRAdminBundle\Tests\Resources\Document\Content;
23
use Symfony\Cmf\Bundle\TreeBrowserBundle\Form\Type\TreeSelectType;
24
use Symfony\Component\Form\Extension\Core\Type\TextType;
25
26
/**
27
 * @author Maximilian Berghoff <[email protected]>
28
 */
29
class AssociationAdmin extends Admin
30
{
31
    /**
32
     * @var ManagerRegistry
33
     */
34
    private $managerRegistry;
35
36
    public function setManagerRegistry(ManagerRegistry $managerRegistry)
37
    {
38
        $this->managerRegistry = $managerRegistry;
39
    }
40
41
    public function getExportFormats()
42
    {
43
        return array();
44
    }
45
46
    protected function configureListFields(ListMapper $listMapper)
47
    {
48
        $listMapper
49
            ->addIdentifier('id')
50
            ->add('title')
51
        ;
52
    }
53
54
    protected function configureFormFields(FormMapper $formMapper)
55
    {
56
        $formMapper
57
            ->with('form.group_general')
58
                ->add('name', TextType::class)
59
                ->add('title', TextType::class)
60
                ->add('parentDocument', TreeSelectType::class, [
61
                    'widget' => 'browser',
62
                    'root_node' => $this->getRootPath(),
63
                ])
64
            ->end()
65
        ;
66
67
        $formMapper->getFormBuilder()->get('parentDocument')->addModelTransformer(new DocumentToPathTransformer(
68
            $this->managerRegistry->getManagerForClass($this->getClass())
69
        ));
70
    }
71
72
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
73
    {
74
        $datagridMapper
75
            ->add('title', StringFilter::class)
76
            ->add('name', NodeNameFilter::class)
77
        ;
78
    }
79
80
    public function toString($object)
81
    {
82
        return $object instanceof Content && $object->getTitle()
83
            ? $object->getTitle()
84
            : $this->trans('link_add', array(), 'SonataAdminBundle')
85
            ;
86
    }
87
}
88