forked from sonata-project/SonataDoctrineORMAdminBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormContractor.php
130 lines (108 loc) · 5.58 KB
/
FormContractor.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
121
122
123
124
125
126
127
128
129
130
<?php
/*
* This file is part of the Sonata package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\DoctrineORMAdminBundle\Builder;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
use Sonata\AdminBundle\Builder\FormContractorInterface;
use Symfony\Component\Form\FormFactoryInterface;
class FormContractor implements FormContractorInterface
{
protected $fieldFactory;
/**
* @param \Symfony\Component\Form\FormFactoryInterface $formFactory
*/
public function __construct(FormFactoryInterface $formFactory)
{
$this->formFactory = $formFactory;
}
/**
* {@inheritdoc}
*/
public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
{
if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
$metadata = $admin->getModelManager()->getMetadata($admin->getClass());
// set the default field mapping
if (isset($metadata->fieldMappings[$fieldDescription->getName()])) {
$fieldDescription->setFieldMapping($metadata->fieldMappings[$fieldDescription->getName()]);
}
// set the default association mapping
if (isset($metadata->associationMappings[$fieldDescription->getName()])) {
$fieldDescription->setAssociationMapping($metadata->associationMappings[$fieldDescription->getName()]);
}
}
if (!$fieldDescription->getType()) {
throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
}
$fieldDescription->setAdmin($admin);
$fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'standard'));
if (in_array($fieldDescription->getMappingType(), array(ClassMetadataInfo::ONE_TO_MANY, ClassMetadataInfo::MANY_TO_MANY, ClassMetadataInfo::MANY_TO_ONE, ClassMetadataInfo::ONE_TO_ONE))) {
$admin->attachAdminClass($fieldDescription);
}
}
/**
* @return \Symfony\Component\Form\FormFactoryInterface
*/
public function getFormFactory()
{
return $this->formFactory;
}
/**
* {@inheritdoc}
*/
public function getFormBuilder($name, array $options = array())
{
return $this->getFormFactory()->createNamedBuilder($name, 'form', null, $options);
}
/**
* {@inheritdoc}
*/
public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription)
{
$options = array();
$options['sonata_field_description'] = $fieldDescription;
if (in_array($type, array('sonata_type_model', 'sonata_type_model_list', 'sonata_type_model_hidden', 'sonata_type_model_autocomplete'))) {
if ($fieldDescription->getOption('edit') == 'list') {
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');
}
$options['class'] = $fieldDescription->getTargetEntity();
$options['model_manager'] = $fieldDescription->getAdmin()->getModelManager();
if ($type == 'sonata_type_model_autocomplete') {
if (!$fieldDescription->getAssociationAdmin()) {
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()));
}
}
} elseif ($type == 'sonata_type_admin') {
if (!$fieldDescription->getAssociationAdmin()) {
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()));
}
if (!in_array($fieldDescription->getMappingType(), array(ClassMetadataInfo::ONE_TO_ONE, ClassMetadataInfo::MANY_TO_ONE))) {
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()));
}
// set sensitive default value to have a component working fine out of the box
$options['btn_add'] = false;
$options['delete'] = false;
$options['data_class'] = $fieldDescription->getAssociationAdmin()->getClass();
$fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'admin'));
} elseif ($type == 'sonata_type_collection') {
if (!$fieldDescription->getAssociationAdmin()) {
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()));
}
$options['type'] = 'sonata_type_admin';
$options['modifiable'] = true;
$options['type_options'] = array(
'sonata_field_description' => $fieldDescription,
'data_class' => $fieldDescription->getAssociationAdmin()->getClass(),
);
}
return $options;
}
}