Completed
Push — master ( eb83f4...bb238e )
by Maximilian
14s
created

src/Form/Type/TreeModelType.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
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\Form\Type;
13
14
use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList;
15
use Sonata\AdminBundle\Form\DataTransformer\ModelToIdTransformer;
16
use Symfony\Component\Form\AbstractType;
17
use Symfony\Component\Form\FormBuilderInterface;
18
use Symfony\Component\Form\FormInterface;
19
use Symfony\Component\Form\FormView;
20
use Symfony\Component\OptionsResolver\Options;
21
use Symfony\Component\OptionsResolver\OptionsResolver;
22
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
23
24
class TreeModelType extends AbstractType
25
{
26
    /**
27
     * @var array
28
     */
29
    protected $defaults = array();
30
31
    /**
32
     * @param array $defaults
33
     */
34
    public function setDefaults(array $defaults)
35
    {
36
        $this->defaults = $defaults;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function buildForm(FormBuilderInterface $builder, array $options)
43
    {
44
        $builder->addViewTransformer(new ModelToIdTransformer($options['model_manager'], $options['class']), true);
45
        $builder->setAttribute('root_node', $options['root_node']);
46
        $builder->setAttribute('select_root_node', $options['select_root_node']);
47
        $builder->setAttribute('repository_name', $options['repository_name']);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function buildView(FormView $view, FormInterface $form, array $options)
54
    {
55
        $view->vars['root_node'] = $form->getConfig()->getAttribute('root_node');
56
        $view->vars['select_root_node'] = $form->getConfig()->getAttribute('select_root_node');
57
        $view->vars['repository_name'] = $form->getConfig()->getAttribute('repository_name');
58
        $view->vars['routing_defaults'] = $this->defaults;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     *
64
     * @todo Remove when Symfony <2.8 is no longer supported
65
     */
66
    public function setDefaultOptions(OptionsResolverInterface $resolver)
67
    {
68
        $this->configureOptions($resolver);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function configureOptions(OptionsResolver $resolver)
75
    {
76
        $resolver->setDefaults(array(
77
            'template' => 'doctrine_phpcr_odm_tree',
78
            'compound' => false,
79
            'model_manager' => null,
80
            'class' => null,
81
            'property' => null,
82
            'query' => null,
83
            'choices' => null,
84
            'root_node' => '/',
85
            'select_root_node' => false,
86
            'parent' => 'choice',
87
            'repository_name' => 'default',
88
            'preferred_choices' => array(),
89
            'choice_list' => function (Options $options, $previousValue) {
0 ignored issues
show
The parameter $previousValue is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
                return new ModelChoiceList(
91
                    $options['model_manager'],
92
                    $options['class'],
93
                    $options['property'],
94
                    $options['query'],
95
                    $options['choices']
96
                );
97
            },
98
        ));
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     *
104
     * @todo Remove when Symfony <2.8 is no longer supported
105
     */
106
    public function getName()
107
    {
108
        return $this->getBlockPrefix();
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function getBlockPrefix()
115
    {
116
        return 'doctrine_phpcr_odm_tree';
117
    }
118
}
119