-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathEmbeddedListViewConfigPass.php
More file actions
95 lines (81 loc) · 3.86 KB
/
EmbeddedListViewConfigPass.php
File metadata and controls
95 lines (81 loc) · 3.86 KB
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
<?php
namespace AlterPHP\EasyAdminExtensionBundle\Configuration;
use EasyCorp\Bundle\EasyAdminBundle\Configuration\ConfigPassInterface;
/**
* Initializes the configuration for all the views of each object of type "%s", which is
* needed when some object of type "%s" relies on the default configuration for some view.
*/
class EmbeddedListViewConfigPass implements ConfigPassInterface
{
private $defaultOpenNewTab;
public function __construct($defaultOpenNewTab)
{
$this->defaultOpenNewTab = $defaultOpenNewTab;
}
public function process(array $backendConfig)
{
$backendConfig = $this->processTemplateConfig($backendConfig);
$backendConfig = $this->processSortingConfig($backendConfig);
$backendConfig = $this->processOpenNewTabConfig($backendConfig);
return $backendConfig;
}
/**
* @return array
*/
private function processOpenNewTabConfig(array $backendConfig)
{
foreach (['entities', 'documents'] as $objectTypeRootKey) {
if (isset($backendConfig[$objectTypeRootKey]) && \is_array($backendConfig[$objectTypeRootKey])) {
foreach ($backendConfig[$objectTypeRootKey] as $objectName => $objectConfig) {
if (!isset($objectConfig['embeddedList']['open_new_tab'])) {
$backendConfig[$objectTypeRootKey][$objectName]['embeddedList']['open_new_tab'] = $this->defaultOpenNewTab;
}
}
}
}
return $backendConfig;
}
/**
* @return array
*/
private function processSortingConfig(array $backendConfig)
{
foreach (['entities', 'documents'] as $objectTypeRootKey) {
if (isset($backendConfig[$objectTypeRootKey]) && \is_array($backendConfig[$objectTypeRootKey])) {
foreach ($backendConfig[$objectTypeRootKey] as $objectName => $objectConfig) {
if (
!isset($objectConfig['embeddedList']['sort'])
&& isset($objectConfig['list']['sort'])
) {
$backendConfig[$objectTypeRootKey][$objectName]['embeddedList']['sort'] = $objectConfig['list']['sort'];
} elseif (isset($objectConfig['embeddedList']['sort'])) {
$sortConfig = $objectConfig['embeddedList']['sort'];
if (!\is_string($sortConfig) && !\is_array($sortConfig)) {
throw new \InvalidArgumentException(\sprintf('The "sort" option of the "embeddedList" view of the "%s" object contains an invalid value (it can only be a string or an array).', $objectName));
}
if (\is_string($sortConfig)) {
$sortConfig = ['field' => $sortConfig, 'direction' => 'DESC'];
} else {
$sortConfig = ['field' => $sortConfig[0], 'direction' => \strtoupper($sortConfig[1])];
}
$backendConfig[$objectTypeRootKey][$objectName]['embeddedList']['sort'] = $sortConfig;
}
}
}
}
return $backendConfig;
}
private function processTemplateConfig(array $backendConfig)
{
foreach (['entities', 'documents'] as $objectTypeRootKey) {
if (isset($backendConfig[$objectTypeRootKey]) && \is_array($backendConfig[$objectTypeRootKey])) {
foreach ($backendConfig[$objectTypeRootKey] as $objectName => $objectConfig) {
if (!isset($objectConfig['embeddedList']['template'])) {
$backendConfig[$objectTypeRootKey][$objectName]['embeddedList']['template'] = '@EasyAdminExtension/default/embedded_list.html.twig';
}
}
}
}
return $backendConfig;
}
}