1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\AdminBundle\Datagrid; |
15
|
|
|
|
16
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
17
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionCollection; |
18
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionInterface; |
19
|
|
|
use Sonata\AdminBundle\Builder\ListBuilderInterface; |
20
|
|
|
use Sonata\AdminBundle\Mapper\BaseMapper; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* This class is used to simulate the Form API. |
24
|
|
|
* |
25
|
|
|
* @author Thomas Rabaix <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class ListMapper extends BaseMapper |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var FieldDescriptionCollection |
31
|
|
|
*/ |
32
|
|
|
protected $list; |
33
|
|
|
|
34
|
|
|
public function __construct( |
35
|
|
|
ListBuilderInterface $listBuilder, |
36
|
|
|
FieldDescriptionCollection $list, |
37
|
|
|
AdminInterface $admin |
38
|
|
|
) { |
39
|
|
|
parent::__construct($listBuilder, $admin); |
40
|
|
|
$this->list = $list; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $name |
45
|
|
|
* @param string|null $type |
46
|
|
|
* |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
|
|
public function addIdentifier($name, $type = null, array $fieldDescriptionOptions = []) |
50
|
|
|
{ |
51
|
|
|
$fieldDescriptionOptions['identifier'] = true; |
52
|
|
|
|
53
|
|
|
if (!isset($fieldDescriptionOptions['route']['name'])) { |
54
|
|
|
$routeName = ($this->admin->hasAccess('edit') && $this->admin->hasRoute('edit')) ? 'edit' : 'show'; |
55
|
|
|
$fieldDescriptionOptions['route']['name'] = $routeName; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (!isset($fieldDescriptionOptions['route']['parameters'])) { |
59
|
|
|
$fieldDescriptionOptions['route']['parameters'] = []; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this->add($name, $type, $fieldDescriptionOptions); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $name |
67
|
|
|
* @param string|null $type |
68
|
|
|
* |
69
|
|
|
* @throws \RuntimeException |
70
|
|
|
* |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function add($name, $type = null, array $fieldDescriptionOptions = []) |
74
|
|
|
{ |
75
|
|
|
// Ensure batch and action pseudo-fields are tagged as virtual |
76
|
|
|
if (\in_array($type, ['actions', 'batch', 'select'], true)) { |
77
|
|
|
$fieldDescriptionOptions['virtual_field'] = true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (\array_key_exists('identifier', $fieldDescriptionOptions) && !\is_bool($fieldDescriptionOptions['identifier'])) { |
81
|
|
|
throw new \InvalidArgumentException(sprintf('Value for "identifier" option must be boolean, %s given.', \gettype($fieldDescriptionOptions['identifier']))); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if ($name instanceof FieldDescriptionInterface) { |
85
|
|
|
$fieldDescription = $name; |
86
|
|
|
$fieldDescription->mergeOptions($fieldDescriptionOptions); |
87
|
|
|
} elseif (\is_string($name)) { |
88
|
|
|
if ($this->admin->hasListFieldDescription($name)) { |
89
|
|
|
throw new \RuntimeException(sprintf( |
90
|
|
|
'Duplicate field name "%s" in list mapper. Names should be unique.', |
91
|
|
|
$name |
92
|
|
|
)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance( |
96
|
|
|
$this->admin->getClass(), |
97
|
|
|
$name, |
98
|
|
|
$fieldDescriptionOptions |
99
|
|
|
); |
100
|
|
|
} else { |
101
|
|
|
throw new \RuntimeException( |
102
|
|
|
'Unknown field name in list mapper. ' |
103
|
|
|
.'Field name should be either of FieldDescriptionInterface interface or string.' |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (null === $fieldDescription->getLabel()) { |
108
|
|
|
$fieldDescription->setOption( |
109
|
|
|
'label', |
110
|
|
|
$this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'list', 'label') |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (isset($fieldDescriptionOptions['header_style'])) { |
115
|
|
|
@trigger_error( |
|
|
|
|
116
|
|
|
'The "header_style" option is deprecated, please, use "header_class" option instead.', |
117
|
|
|
E_USER_DEPRECATED |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (!isset($fieldDescriptionOptions['role']) || $this->admin->isGranted($fieldDescriptionOptions['role'])) { |
122
|
|
|
// add the field with the FormBuilder |
123
|
|
|
$this->builder->addField($this->list, $type, $fieldDescription, $this->admin); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function get($name) |
130
|
|
|
{ |
131
|
|
|
return $this->list->get($name); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function has($key) |
135
|
|
|
{ |
136
|
|
|
return $this->list->has($key); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function remove($key) |
140
|
|
|
{ |
141
|
|
|
$this->admin->removeListFieldDescription($key); |
142
|
|
|
$this->list->remove($key); |
143
|
|
|
|
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
final public function keys() |
148
|
|
|
{ |
149
|
|
|
return array_keys($this->list->getElements()); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function reorder(array $keys) |
153
|
|
|
{ |
154
|
|
|
$this->list->reorder($keys); |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: