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
/
ShowBuilder.php
142 lines (119 loc) · 4.66 KB
/
ShowBuilder.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
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/*
* 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\Builder;
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
use Sonata\AdminBundle\Builder\ShowBuilderInterface;
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
class ShowBuilder implements ShowBuilderInterface
{
/**
* @var TypeGuesserInterface
*/
protected $guesser;
/**
* @var array
*/
protected $templates;
/**
* @param TypeGuesserInterface $guesser
* @param array $templates Indexed by field type
*/
public function __construct(TypeGuesserInterface $guesser, array $templates)
{
$this->guesser = $guesser;
$this->templates = $templates;
}
/**
* {@inheritdoc}
*/
public function getBaseList(array $options = [])
{
return new FieldDescriptionCollection();
}
/**
* {@inheritdoc}
*/
public function addField(FieldDescriptionCollection $list, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
{
if ($type == null) {
$guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
$fieldDescription->setType($guessType->getType());
} else {
$fieldDescription->setType($type);
}
$this->fixFieldDescription($admin, $fieldDescription);
$admin->addShowFieldDescription($fieldDescription->getName(), $fieldDescription);
$list->add($fieldDescription);
}
/**
* The method defines the correct default settings for the provided FieldDescription.
*
* {@inheritdoc}
*
* @throws \RuntimeException if the $fieldDescription does not have a type
*/
public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
{
$fieldDescription->setAdmin($admin);
$metadata = null;
if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
/** @var ClassMetadata $metadata */
$metadata = $admin->getModelManager()->getMetadata($admin->getClass());
// set the default field mapping
if (isset($metadata->mappings[$fieldDescription->getName()])) {
$fieldDescription->setFieldMapping($metadata->mappings[$fieldDescription->getName()]);
}
// set the default association mapping
if ($metadata->hasAssociation($fieldDescription->getName())) {
$fieldDescription->setAssociationMapping($metadata->getAssociation($fieldDescription->getName()));
}
}
if (!$fieldDescription->getType()) {
throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
}
$fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
$fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName()));
if (!$fieldDescription->getTemplate()) {
$fieldDescription->setTemplate($this->getTemplate($fieldDescription->getType()));
if ($fieldDescription->getMappingType() == ClassMetadata::MANY_TO_ONE) {
$fieldDescription->setTemplate('SonataAdminBundle:CRUD/Association:show_many_to_one.html.twig');
}
if ($fieldDescription->getMappingType() == ClassMetadata::MANY_TO_MANY) {
$fieldDescription->setTemplate('SonataAdminBundle:CRUD/Association:show_many_to_many.html.twig');
}
}
$mappingTypes = [
ClassMetadata::MANY_TO_ONE,
ClassMetadata::MANY_TO_MANY,
'children',
'child',
'parent',
'referrers',
];
if ($metadata && $metadata->hasAssociation($fieldDescription->getName()) && in_array($fieldDescription->getMappingType(), $mappingTypes)) {
$admin->attachAdminClass($fieldDescription);
}
}
/**
* @param string $type
*
* @return string|null The template if found
*/
private function getTemplate($type)
{
if (!isset($this->templates[$type])) {
return;
}
return $this->templates[$type];
}
}