-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGroupTypeManager.php
More file actions
404 lines (348 loc) · 12.1 KB
/
GroupTypeManager.php
File metadata and controls
404 lines (348 loc) · 12.1 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php
namespace Drupal\og;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteBuilderInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\og\Event\GroupCreationEvent;
use Drupal\og\Event\GroupCreationEventInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* A manager to keep track of which entity type/bundles are OG group enabled.
*/
class GroupTypeManager implements GroupTypeManagerInterface {
/**
* The key used to identify the cached version of the group relation map.
*/
const GROUP_RELATION_MAP_CACHE_KEY = 'og.group_manager.group_relation_map';
/**
* The OG settings configuration key.
*
* @var string
*/
const SETTINGS_CONFIG_KEY = 'og.settings';
/**
* The OG group settings config key.
*
* @var string
*/
const GROUPS_CONFIG_KEY = 'groups';
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The service providing information about bundles.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected $entityTypeBundleInfo;
/**
* The event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* The cache backend.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cache;
/**
* The OG permission manager.
*
* @var \Drupal\og\PermissionManagerInterface
*/
protected $permissionManager;
/**
* A map of entity types and bundles.
*
* Do not access this property directly, use $this->getGroupMap() instead.
*
* @var array
*/
protected $groupMap;
/**
* A map of group and group content relations.
*
* Do not access this property directly, use $this->getGroupRelationMap()
* instead.
*
* @var array
* An associative array representing group and group content relations.
*
* This mapping is in the following format:
* @code
* [
* 'group_entity_type_id' => [
* 'group_bundle_id' => [
* 'group_content_entity_type_id' => [
* 'group_content_bundle_id',
* ],
* ],
* ],
* ]
* @endcode
*/
protected $groupRelationMap = [];
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The OG role manager.
*
* @var \Drupal\og\OgRoleManagerInterface
*/
protected $ogRoleManager;
/**
* The route builder service.
*
* @var \Drupal\Core\Routing\RouteBuilderInterface
*/
protected $routeBuilder;
/**
* The OG group audience helper.
*
* @var \Drupal\og\OgGroupAudienceHelperInterface
*/
protected $groupAudienceHelper;
/**
* Constructs a GroupTypeManager object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The service providing information about bundles.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache
* The cache backend.
* @param \Drupal\og\PermissionManagerInterface $permission_manager
* The OG permission manager.
* @param \Drupal\og\OgRoleManagerInterface $og_role_manager
* The OG role manager.
* @param \Drupal\Core\Routing\RouteBuilderInterface $route_builder
* The route builder service.
* @param \Drupal\og\OgGroupAudienceHelperInterface $group_audience_helper
* The OG group audience helper.
*/
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EventDispatcherInterface $event_dispatcher, CacheBackendInterface $cache, PermissionManagerInterface $permission_manager, OgRoleManagerInterface $og_role_manager, RouteBuilderInterface $route_builder, OgGroupAudienceHelperInterface $group_audience_helper) {
$this->configFactory = $config_factory;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->eventDispatcher = $event_dispatcher;
$this->cache = $cache;
$this->permissionManager = $permission_manager;
$this->ogRoleManager = $og_role_manager;
$this->routeBuilder = $route_builder;
$this->groupAudienceHelper = $group_audience_helper;
}
/**
* {@inheritdoc}
*/
public function isGroup($entity_type_id, $bundle) {
$group_map = $this->getGroupMap();
return isset($group_map[$entity_type_id]) && in_array($bundle, $group_map[$entity_type_id]);
}
/**
* {@inheritdoc}
*/
public function isGroupContent($entity_type_id, $bundle) {
return $this->groupAudienceHelper->hasGroupAudienceField($entity_type_id, $bundle);
}
/**
* {@inheritdoc}
*/
public function getGroupBundleIdsByEntityType($entity_type_id) {
$group_map = $this->getGroupMap();
return isset($group_map[$entity_type_id]) ? $group_map[$entity_type_id] : [];
}
/**
* {@inheritdoc}
*/
public function getAllGroupBundles($entity_type = NULL) {
$group_map = $this->getGroupMap();
return !empty($group_map[$entity_type]) ? $group_map[$entity_type] : $group_map;
}
/**
* {@inheritdoc}
*/
public function getAllGroupContentBundleIds() {
$bundles = [];
foreach ($this->getGroupRelationMap() as $group_entity_type_id => $group_bundle_ids) {
foreach ($group_bundle_ids as $group_content_entity_type_ids) {
foreach ($group_content_entity_type_ids as $group_content_entity_type_id => $group_content_bundle_ids) {
$bundles[$group_content_entity_type_id] = array_merge(isset($bundles[$group_content_entity_type_id]) ? $bundles[$group_content_entity_type_id] : [], $group_content_bundle_ids);
}
}
}
return $bundles;
}
/**
* {@inheritdoc}
*/
public function getAllGroupContentBundlesByEntityType($entity_type_id) {
$bundles = $this->getAllGroupContentBundleIds();
if (!isset($bundles[$entity_type_id])) {
throw new \InvalidArgumentException("The '$entity_type_id' entity type has no group content bundles.");
}
return $bundles[$entity_type_id];
}
/**
* {@inheritdoc}
*/
public function getGroupBundleIdsByGroupContentBundle($group_content_entity_type_id, $group_content_bundle_id) {
$bundles = [];
foreach ($this->groupAudienceHelper->getAllGroupAudienceFields($group_content_entity_type_id, $group_content_bundle_id) as $field) {
$group_entity_type_id = $field->getSetting('target_type');
$handler_settings = $field->getSetting('handler_settings');
$group_bundle_ids = !empty($handler_settings['target_bundles']) ? $handler_settings['target_bundles'] : [];
// If the group bundles are empty, it means that all bundles are
// referenced.
if (empty($group_bundle_ids)) {
$group_bundle_ids = $this->getGroupMap()[$group_entity_type_id];
}
foreach ($group_bundle_ids as $group_bundle_id) {
$bundles[$group_entity_type_id][$group_bundle_id] = $group_bundle_id;
}
}
return $bundles;
}
/**
* {@inheritdoc}
*/
public function getGroupContentBundleIdsByGroupBundle($group_entity_type_id, $group_bundle_id) {
$group_relation_map = $this->getGroupRelationMap();
if (!is_array($group_relation_map) && property_exists($group_relation_map,'data')) {
$group_relation_map = $group_relation_map->data;
}
return isset($group_relation_map[$group_entity_type_id][$group_bundle_id]) ? $group_relation_map[$group_entity_type_id][$group_bundle_id] : [];
}
/**
* {@inheritdoc}
*/
public function addGroup($entity_type_id, $bundle_id) {
// Throw an error if the entity type is already defined as a group.
if ($this->isGroup($entity_type_id, $bundle_id)) {
throw new \InvalidArgumentException("The '$entity_type_id' of type '$bundle_id' is already a group.");
}
$editable = $this->configFactory->getEditable('og.settings');
$groups = $editable->get('groups');
$groups[$entity_type_id][] = $bundle_id;
// @todo, just key by bundle ID instead?
$groups[$entity_type_id] = array_unique($groups[$entity_type_id]);
$editable->set('groups', $groups);
$editable->save();
// Trigger an event upon the new group creation.
$event = new GroupCreationEvent($entity_type_id, $bundle_id);
$this->eventDispatcher->dispatch(GroupCreationEventInterface::EVENT_NAME, $event);
$this->ogRoleManager->createPerBundleRoles($entity_type_id, $bundle_id);
$this->refreshGroupMap();
// Routes will need to be rebuilt.
$this->routeBuilder->setRebuildNeeded();
}
/**
* {@inheritdoc}
*/
public function removeGroup($entity_type_id, $bundle_id) {
$editable = $this->configFactory->getEditable('og.settings');
$groups = $editable->get('groups');
if (isset($groups[$entity_type_id])) {
$search_key = array_search($bundle_id, $groups[$entity_type_id]);
if ($search_key !== FALSE) {
unset($groups[$entity_type_id][$search_key]);
}
// Clean up entity types that have become empty.
$groups = array_filter($groups);
// Only update and refresh the map if a key was found and unset.
$editable->set('groups', $groups);
$editable->save();
$this->resetGroupMap();
// Routes will need to be rebuilt.
$this->routeBuilder->setRebuildNeeded();
}
}
/**
* {@inheritdoc}
*/
public function reset() {
$this->resetGroupMap();
$this->resetGroupRelationMap();
}
/**
* {@inheritdoc}
*/
public function resetGroupMap() {
$this->groupMap = [];
}
/**
* {@inheritdoc}
*/
public function resetGroupRelationMap() {
$this->groupRelationMap = [];
$this->cache->delete(self::GROUP_RELATION_MAP_CACHE_KEY);
}
/**
* {@inheritdoc}
*/
public function getGroupMap() {
if (empty($this->groupMap)) {
$this->refreshGroupMap();
}
return $this->groupMap;
}
/**
* Returns the group relation map.
*
* @return array
* The group relation map.
*/
protected function getGroupRelationMap() {
if (empty($this->groupRelationMap)) {
$this->refreshGroupRelationMap();
}
return $this->groupRelationMap;
}
/**
* Refreshes the groupMap property with currently configured groups.
*/
protected function refreshGroupMap() {
$group_map = $this->configFactory->get(static::SETTINGS_CONFIG_KEY)->get(static::GROUPS_CONFIG_KEY);
$this->groupMap = !empty($group_map) ? $group_map : [];
}
/**
* Populates the map of relations between group types and group content types.
*/
protected function refreshGroupRelationMap() {
// Retrieve a cached version of the map if it exists.
if ($group_relation_map = $this->cache->get(self::GROUP_RELATION_MAP_CACHE_KEY)) {
$this->groupRelationMap = $group_relation_map;
return;
}
$this->groupRelationMap = [];
$user_bundles = \Drupal::entityTypeManager()->getDefinition('user')->getKey('bundle') ?: ['user'];
foreach ($this->entityTypeBundleInfo->getAllBundleInfo() as $group_content_entity_type_id => $bundles) {
foreach ($bundles as $group_content_bundle_id => $bundle_info) {
if (in_array($group_content_bundle_id, $user_bundles)) {
// User is not a group content per se. Remove it.
continue;
}
foreach ($this->getGroupBundleIdsByGroupContentBundle($group_content_entity_type_id, $group_content_bundle_id) as $group_entity_type_id => $group_bundle_ids) {
foreach ($group_bundle_ids as $group_bundle_id) {
$this->groupRelationMap[$group_entity_type_id][$group_bundle_id][$group_content_entity_type_id][$group_content_bundle_id] = $group_content_bundle_id;
}
}
}
}
// Cache the map.
$this->cache->set(self::GROUP_RELATION_MAP_CACHE_KEY, $this->groupRelationMap);
}
}