-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathColumnConfigurationService.php
More file actions
156 lines (131 loc) · 5 KB
/
ColumnConfigurationService.php
File metadata and controls
156 lines (131 loc) · 5 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
declare(strict_types=1);
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/
namespace Pimcore\Bundle\StudioBackendBundle\Grid\Service;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
use Pimcore\Bundle\StudioBackendBundle\Grid\Column\ClassIdInterface;
use Pimcore\Bundle\StudioBackendBundle\Grid\Column\FolderIdInterface;
use Pimcore\Bundle\StudioBackendBundle\Grid\Event\GridColumnConfigurationEvent;
use Pimcore\Bundle\StudioBackendBundle\Grid\Schema\ColumnConfiguration;
use Pimcore\Bundle\StudioBackendBundle\Grid\Util\ColumnFieldDefinition;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\ElementTypes;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use function in_array;
/**
* @internal
*/
final readonly class ColumnConfigurationService implements ColumnConfigurationServiceInterface
{
public function __construct(
private GridServiceInterface $gridService,
private EventDispatcherInterface $eventDispatcher,
) {
}
/**
* @return ColumnConfiguration[]
*/
public function getAvailableAssetColumnConfiguration(): array
{
$columns = [];
foreach ($this->gridService->getColumnCollectors() as $collector) {
// Only collect supported asset collectors
if (!in_array(ElementTypes::TYPE_ASSET, $collector->supportedElementTypes(), true)) {
continue;
}
// rather use the spread operator instead of array_merge in a loop
$columns = [
...$columns,
...$collector->getColumnConfigurations($this->gridService->getColumnDefinitions()),
];
}
$this->dispatchEventForAllColumns($columns);
return $columns;
}
public function getAvailableDataObjectColumnConfiguration(string $classId, int $folderId): array
{
$columns = [];
foreach ($this->gridService->getColumnCollectors() as $collector) {
// Only collect supported data object collectors
if (!in_array(ElementTypes::TYPE_DATA_OBJECT, $collector->supportedElementTypes(), true)) {
continue;
}
if ($collector instanceof ClassIdInterface) {
$collector->setClassId($classId);
}
if ($collector instanceof FolderIdInterface) {
$collector->setFolderId($folderId);
}
// rather use the spread operator instead of array_merge in a loop
$columns = [
...$columns,
...$collector->getColumnConfigurations($this->gridService->getColumnDefinitions()),
];
}
$this->dispatchEventForAllColumns($columns);
return $columns;
}
/**
* @return ColumnConfiguration[]
*
* @throws InvalidArgumentException
*/
public function getSystemDataObjectColumnConfiguration(): array
{
$systemCollector = $this->gridService->getColumnCollectors()['system.dataobject'];
if (!in_array(ElementTypes::TYPE_DATA_OBJECT, $systemCollector->supportedElementTypes(), true)) {
throw new InvalidArgumentException('collector does not support data objects');
}
return $systemCollector->getColumnConfigurations($this->gridService->getColumnDefinitions());
}
public function buildDataObjectAdapterColumnConfiguration(
ColumnFieldDefinition $definition,
?string $type = null,
?string $key = null,
?array $additionalConfig = null
): ColumnConfiguration {
$config = [];
$fieldDefinition = $definition->getFieldDefinition();
$config['fieldDefinition'] = $fieldDefinition;
if ($key === null) {
$key = $fieldDefinition->getName();
}
if ($additionalConfig) {
$config = array_merge($config, $additionalConfig);
}
return new ColumnConfiguration(
key: $key,
group: $definition->getGroup(),
sortable: true,
editable: !$fieldDefinition->getNoteditable(),
exportable: true,
localizable: $definition->isLocalized(),
locale: null,
type: $type,
frontendType: $fieldDefinition->getFieldType(),
config: $config
);
}
/**
* @param ColumnConfiguration[] $columns
*/
private function dispatchEventForAllColumns(array $columns): void
{
foreach ($columns as $column) {
$this->eventDispatcher->dispatch(
new GridColumnConfigurationEvent($column),
GridColumnConfigurationEvent::EVENT_NAME
);
}
}
}