-
Notifications
You must be signed in to change notification settings - Fork 1
/
OverviewBuilderManager.php
191 lines (155 loc) · 6.53 KB
/
OverviewBuilderManager.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
namespace Drupal\wmentity_overview\OverviewBuilder;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Utility\Error;
use Drupal\wmentity_overview\Annotation\OverviewBuilder;
use Drupal\wmentity_overview\FilterStorage\FilterStorageManager;
use Drupal\wmentity_overview\Plugin\Factory\OverviewBuilderPluginFactory;
/**
* @method OverviewBuilderInterface createInstance($plugin_id, array $configuration = [])
*/
class OverviewBuilderManager extends DefaultPluginManager
{
/** @var EntityTypeManagerInterface */
protected $entityTypeManager;
/** @var FilterStorageManager */
protected $filterStorageManager;
public function __construct(
\Traversable $namespaces,
CacheBackendInterface $cacheBackend,
ModuleHandlerInterface $moduleHandler,
EntityTypeManagerInterface $entityTypeManager,
FilterStorageManager $filterStorageManager
) {
parent::__construct(
'',
$namespaces,
$moduleHandler,
OverviewBuilderInterface::class,
OverviewBuilder::class
);
$this->alterInfo('wmentity_overview_builder_info');
$this->setCacheBackend($cacheBackend, 'wmentity_overview_builders');
$this->factory = new OverviewBuilderPluginFactory($this, OverviewBuilderInterface::class);
$this->entityTypeManager = $entityTypeManager;
$this->filterStorageManager = $filterStorageManager;
}
/** @return OverviewBuilder[] */
public function getAlternatives(OverviewBuilder $definition): array
{
$filterStorage = $this->filterStorageManager->createInstance($definition->getFilterStorageId());
$alternatives = $this->getAlternativesByFilters($definition, $filterStorage->getAll());
$this->moduleHandler->alter('entity_overview_alternatives', $alternatives, $definition);
return $alternatives;
}
/** @return OverviewBuilder[] */
public function getAlternativesByFilters(OverviewBuilder $definition, array $filters): array
{
$alternatives = [];
$possibleAlternatives = array_diff_key(
$this->getDefinitionsByEntityType($definition->getEntityTypeId()),
[$definition->getId() => $definition]
);
foreach ($possibleAlternatives as $alternative) {
$missingFilters = array_diff_assoc($alternative->getFilters(), $filters);
if (empty($missingFilters)) {
$alternatives[$alternative->getId()] = $alternative;
}
}
return $alternatives;
}
/** @return OverviewBuilder[] */
public function getDefinitionsByEntityType(string $entityTypeId): array
{
return array_reduce(
$this->getDefinitions(),
static function (array $definitions, $definition) use ($entityTypeId) {
$definition = new OverviewBuilder($definition);
if ($definition->getEntityTypeId() !== $entityTypeId) {
return $definitions;
}
if (!$definition->isOverride()) {
return $definitions;
}
$definitions[$definition->getId()] = $definition;
return $definitions;
},
[]
);
}
public function getDefinitionsByRouteName(?string $type = null): array
{
return array_reduce(
$this->getDefinitions(),
static function (array $definitions, $definition) use ($type) {
$definition = new OverviewBuilder($definition);
$routeName = $definition->getRouteName();
if (!$routeName) {
return $definitions;
}
if ($type && $definition->getType() !== $type) {
return $definitions;
}
$definitions[$definition->getRouteName()][$definition->getId()] = $definition;
return $definitions;
},
[]
);
}
protected function findDefinitions(): array
{
$definitions = parent::findDefinitions();
foreach ($definitions as &$definition) {
$annotation = new OverviewBuilder($definition);
if (!$annotation->getId()) {
throw new \RuntimeException(sprintf("Overview builder with class '%s' is missing the id property.", $annotation->getClass()));
}
if (!$annotation->getEntityTypeId()) {
throw new \RuntimeException(sprintf("Overview builder with id '%s' is missing the entity_type property.", $annotation->getId() ?? ''));
}
if ($annotation->isOverride()) {
try {
$definition['route_name'] = $this->guessRouteName($definition);
} catch (\Exception $e) {
// Catch the exception to prevent site install from failing
// when the entity type in question is not yet installed.
Error::logException(\Drupal::logger('wmentity_overview'), $e);
}
}
$definition['type'] = $annotation->getType();
$definition['id'] = $annotation->getId();
}
return $definitions;
}
protected function guessRouteName(array $definition): ?string
{
$entityType = $this->entityTypeManager->getDefinition($definition['entity_type'], false);
if (!$entityType) {
throw new \RuntimeException(sprintf(
'Entity type with id \'%s\' as referenced in EntityOverview with id \'%s\' does not exist.',
$definition['entity_type'],
$definition['id']
));
}
if ($entityType->hasLinkTemplate('collection')) {
return sprintf('entity.%s.collection', $entityType->id());
}
$map = [
'node' => 'system.admin_content',
'taxonomy_term' => 'entity.taxonomy_vocabulary.overview_form',
];
if (isset($map[$entityType->id()])) {
return $map[$entityType->id()];
}
if ($entityType->getProvider() === 'eck') {
return sprintf('eck.entity.%s.list', $entityType->id());
}
throw new \RuntimeException(sprintf(
'Could not determine route name of EntityOverview with id \'%s\'. Please add a route_name parameter to the annotation.',
$definition['id']
));
}
}