Completed
Pull Request — master (#175)
by
unknown
08:49
created

FormContractor::getDefaultOptions()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 34
rs 5.3846
cc 8
eloc 22
nc 7
nop 2
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\DoctrineMongoDBAdminBundle\Builder;
13
14
use Sonata\AdminBundle\Admin\AdminInterface;
15
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
16
use Sonata\AdminBundle\Builder\AbstractFormContractor;
17
18
class FormContractor extends AbstractFormContractor
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
24
    {
25
        if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
26
            $metadata = $admin->getModelManager()->getMetadata($admin->getClass());
27
28
            // set the default field mapping
29
            if (isset($metadata->fieldMappings[$fieldDescription->getName()])) {
30
                $fieldDescription->setFieldMapping($metadata->fieldMappings[$fieldDescription->getName()]);
31
            }
32
33
            // set the default association mapping
34
            if (isset($metadata->associationMappings[$fieldDescription->getName()])) {
35
                $fieldDescription->setAssociationMapping($metadata->associationMappings[$fieldDescription->getName()]);
36
            }
37
        }
38
39
        if (!$fieldDescription->getType()) {
40
            throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
41
        }
42
43
        $fieldDescription->setAdmin($admin);
44
        $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'standard'));
45
46
        if ($fieldDescription->describesAssociation()) {
47
            $admin->attachAdminClass($fieldDescription);
48
        }
49
    }
50
}
51