This repository has been archived by the owner on Jul 22, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
TreeModelType.php
120 lines (108 loc) · 3.41 KB
/
TreeModelType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\DoctrinePHPCRAdminBundle\Form\Type;
use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList;
use Sonata\AdminBundle\Form\DataTransformer\ModelToIdTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class TreeModelType extends AbstractType
{
/**
* @var array
*/
protected $defaults = [];
/**
* @param array $defaults
*/
public function setDefaults(array $defaults)
{
$this->defaults = $defaults;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addViewTransformer(new ModelToIdTransformer($options['model_manager'], $options['class']), true);
$builder->setAttribute('root_node', $options['root_node']);
$builder->setAttribute('select_root_node', $options['select_root_node']);
$builder->setAttribute('repository_name', $options['repository_name']);
}
/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['root_node'] = $form->getConfig()->getAttribute('root_node');
$view->vars['select_root_node'] = $form->getConfig()->getAttribute('select_root_node');
$view->vars['repository_name'] = $form->getConfig()->getAttribute('repository_name');
$view->vars['routing_defaults'] = $this->defaults;
}
/**
* {@inheritdoc}
*
* @todo Remove when Symfony <2.8 is no longer supported
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$this->configureOptions($resolver);
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'template' => 'doctrine_phpcr_odm_tree',
'compound' => false,
'model_manager' => null,
'class' => null,
'property' => null,
'query' => null,
'choices' => null,
'root_node' => '/',
'select_root_node' => false,
'parent' => 'choice',
'repository_name' => 'default',
'preferred_choices' => [],
'choice_list' => function (Options $options, $previousValue) {
return new ModelChoiceList(
$options['model_manager'],
$options['class'],
$options['property'],
$options['query'],
$options['choices']
);
},
]);
}
/**
* {@inheritdoc}
*
* @todo Remove when Symfony <2.8 is no longer supported
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'doctrine_phpcr_odm_tree';
}
}