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\DoctrineORMAdminBundle\Builder; |
13
|
|
|
|
14
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
15
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
16
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionInterface; |
17
|
|
|
use Sonata\AdminBundle\Builder\FormContractorInterface; |
18
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
19
|
|
|
|
20
|
|
|
class FormContractor implements FormContractorInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @deprecated since version 3.0.4, to be removed in 4.0 |
24
|
|
|
* |
25
|
|
|
* @var FormFactoryInterface |
26
|
|
|
*/ |
27
|
|
|
protected $fieldFactory; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var FormFactoryInterface |
31
|
|
|
*/ |
32
|
|
|
protected $formFactory; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param FormFactoryInterface $formFactory |
36
|
|
|
*/ |
37
|
|
|
public function __construct(FormFactoryInterface $formFactory) |
38
|
|
|
{ |
39
|
|
|
$this->formFactory = $formFactory; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription) |
46
|
|
|
{ |
47
|
|
|
if ($admin->getModelManager()->hasMetadata($admin->getClass())) { |
48
|
|
|
$metadata = $admin->getModelManager()->getMetadata($admin->getClass()); |
49
|
|
|
|
50
|
|
|
// set the default field mapping |
51
|
|
|
if (isset($metadata->fieldMappings[$fieldDescription->getName()])) { |
52
|
|
|
$fieldDescription->setFieldMapping($metadata->fieldMappings[$fieldDescription->getName()]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// set the default association mapping |
56
|
|
|
if (isset($metadata->associationMappings[$fieldDescription->getName()])) { |
57
|
|
|
$fieldDescription->setAssociationMapping($metadata->associationMappings[$fieldDescription->getName()]); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (!$fieldDescription->getType()) { |
62
|
|
|
throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin))); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$fieldDescription->setAdmin($admin); |
66
|
|
|
$fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'standard')); |
67
|
|
|
|
68
|
|
|
if (in_array($fieldDescription->getMappingType(), array(ClassMetadataInfo::ONE_TO_MANY, ClassMetadataInfo::MANY_TO_MANY, ClassMetadataInfo::MANY_TO_ONE, ClassMetadataInfo::ONE_TO_ONE))) { |
69
|
|
|
$admin->attachAdminClass($fieldDescription); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return FormFactoryInterface |
75
|
|
|
*/ |
76
|
|
|
public function getFormFactory() |
77
|
|
|
{ |
78
|
|
|
return $this->formFactory; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function getFormBuilder($name, array $options = array()) |
85
|
|
|
{ |
86
|
|
|
// NEXT_MAJOR: Remove this line when drop Symfony <2.8 support |
87
|
|
|
$formType = method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix') |
88
|
|
|
? 'Symfony\Component\Form\Extension\Core\Type\FormType' : 'form'; |
89
|
|
|
|
90
|
|
|
return $this->getFormFactory()->createNamedBuilder($name, $formType, null, $options); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription) |
97
|
|
|
{ |
98
|
|
|
$options = array(); |
99
|
|
|
$options['sonata_field_description'] = $fieldDescription; |
100
|
|
|
|
101
|
|
|
if (in_array($type, array('sonata_type_model', 'sonata_type_model_list', 'sonata_type_model_hidden', 'sonata_type_model_autocomplete'))) { |
102
|
|
|
if ($fieldDescription->getOption('edit') == 'list') { |
103
|
|
|
throw new \LogicException('The ``sonata_type_model`` type does not accept an ``edit`` option anymore, please review the UPGRADE-2.1.md file from the SonataAdminBundle'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$options['class'] = $fieldDescription->getTargetEntity(); |
107
|
|
|
$options['model_manager'] = $fieldDescription->getAdmin()->getModelManager(); |
108
|
|
|
|
109
|
|
|
if ($type == 'sonata_type_model_autocomplete') { |
110
|
|
|
if (!$fieldDescription->getAssociationAdmin()) { |
111
|
|
|
throw new \RuntimeException(sprintf('The current field `%s` is not linked to an admin. Please create one for the target entity: `%s`', $fieldDescription->getName(), $fieldDescription->getTargetEntity())); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} elseif ($type == 'sonata_type_admin') { |
115
|
|
|
if (!$fieldDescription->getAssociationAdmin()) { |
116
|
|
|
throw new \RuntimeException(sprintf('The current field `%s` is not linked to an admin. Please create one for the target entity : `%s`', $fieldDescription->getName(), $fieldDescription->getTargetEntity())); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if (!in_array($fieldDescription->getMappingType(), array(ClassMetadataInfo::ONE_TO_ONE, ClassMetadataInfo::MANY_TO_ONE))) { |
120
|
|
|
throw new \RuntimeException(sprintf('You are trying to add `sonata_type_admin` field `%s` which is not One-To-One or Many-To-One. Maybe you want `sonata_model_list` instead?', $fieldDescription->getName())); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// set sensitive default value to have a component working fine out of the box |
124
|
|
|
$options['btn_add'] = false; |
125
|
|
|
$options['delete'] = false; |
126
|
|
|
|
127
|
|
|
$options['data_class'] = $fieldDescription->getAssociationAdmin()->getClass(); |
128
|
|
|
$fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'admin')); |
129
|
|
|
} elseif ($type == 'sonata_type_collection') { |
130
|
|
|
if (!$fieldDescription->getAssociationAdmin()) { |
131
|
|
|
throw new \RuntimeException(sprintf('The current field `%s` is not linked to an admin. Please create one for the target entity : `%s`', $fieldDescription->getName(), $fieldDescription->getTargetEntity())); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$options['type'] = 'sonata_type_admin'; |
135
|
|
|
$options['modifiable'] = true; |
136
|
|
|
$options['type_options'] = array( |
137
|
|
|
'sonata_field_description' => $fieldDescription, |
138
|
|
|
'data_class' => $fieldDescription->getAssociationAdmin()->getClass(), |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $options; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|