GroupMenuConfigOverrides.php in Group Menu 8
File
src/GroupMenuConfigOverrides.php
View source
<?php
namespace Drupal\groupmenu;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryOverrideInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\group\Entity\GroupContentType;
class GroupMenuConfigOverrides implements ConfigFactoryOverrideInterface {
protected $baseStorage;
protected $currentUser;
protected $cache;
protected $configurations;
protected $groupTypes;
protected $userGroupMenuIds;
protected $overrides;
public function __construct(StorageInterface $storage, AccountInterface $current_user, CacheBackendInterface $cache) {
$this->baseStorage = $storage;
$this->currentUser = $current_user;
$this->cache = $cache;
}
public function loadOverrides($names) {
$overrides = [];
$node_type_names = array_filter($names, function ($name) {
return strpos($name, 'node.type') === 0;
});
if (!empty($node_type_names)) {
foreach ($node_type_names as $node_type_name) {
if (isset($this->overrides[$node_type_name])) {
$overrides[$node_type_name] = $this->overrides[$node_type_name];
}
else {
$current_config = $this
->getConfig($node_type_name);
$group_types = $this
->getEnabledGroupMenuTypesByNodeType($current_config['type']);
if ($group_types && ($menus = $this
->getUserGroupMenuIdsByGroupTypes($group_types))) {
$overrides[$node_type_name] = [
'third_party_settings' => [
'menu_ui' => [
'available_menus' => array_merge($current_config['third_party_settings']['menu_ui']['available_menus'], $menus),
],
],
];
$this->overrides[$node_type_name] = $overrides[$node_type_name];
}
}
}
}
return $overrides;
}
protected function getEnabledGroupMenuTypesByNodeType($node_type) {
if (isset($this->groupTypes[$node_type])) {
return $this->groupTypes[$node_type];
}
$cid = 'groupmenu:group_menu_types:' . $node_type;
$persistent_cache = $this->cache
->get($cid);
if ($persistent_cache && $persistent_cache->valid) {
$this->groupTypes[$node_type] = $persistent_cache->data;
return $this->groupTypes[$node_type];
}
$plugin_id = 'group_node:' . $node_type;
$group_content_types = GroupContentType::loadByContentPluginId($plugin_id);
$this->groupTypes[$node_type] = [];
foreach ($group_content_types as $group_content_type) {
if (!empty($group_content_type
->getContentPlugin()
->getConfiguration()['node_form_group_menu'])) {
$this->groupTypes[$node_type][$group_content_type
->getGroupType()
->id()] = $group_content_type
->getGroupType()
->id();
}
}
$this->cache
->set($cid, $this->groupTypes[$node_type]);
return $this->groupTypes[$node_type];
}
protected function getUserGroupMenuIdsByGroupTypes(array $group_types) {
$group_types_cid = md5(implode('-', $group_types));
if (isset($this->userGroupMenuIds[$this->currentUser
->id()][$group_types_cid])) {
return $this->userGroupMenuIds[$this->currentUser
->id()][$group_types_cid];
}
$cid = 'groupmenu:user_group_menu_ids:' . $this->currentUser
->id() . ':' . $group_types_cid;
$persistent_cache = $this->cache
->get($cid);
if ($persistent_cache && $persistent_cache->valid) {
$this->userGroupMenuIds[$this->currentUser
->id()][$group_types_cid] = $persistent_cache->data;
return $this->userGroupMenuIds[$this->currentUser
->id()][$group_types_cid];
}
$entity_type_manager = \Drupal::service('entity_type.manager');
$plugin_id = 'group_menu:menu';
$group_content_types = $entity_type_manager
->getStorage('group_content_type')
->loadByProperties([
'content_plugin' => $plugin_id,
'group_type' => array_keys($group_types),
]);
if (empty($group_content_types)) {
return [];
}
$group_contents = $entity_type_manager
->getStorage('group_content')
->loadByProperties([
'type' => array_keys($group_content_types),
]);
$this->userGroupMenuIds[$this->currentUser
->id()][$group_types_cid] = [];
foreach ($group_contents as $group_content) {
if ($group_content && $group_content
->getGroup()
->hasPermission("update {$plugin_id} entity", $this->currentUser) && ($entity = $group_content
->getEntity())) {
$this->userGroupMenuIds[$this->currentUser
->id()][$group_types_cid][] = $entity
->id();
}
}
$this->cache
->set($cid, $this->userGroupMenuIds[$this->currentUser
->id()][$group_types_cid]);
return $this->userGroupMenuIds[$this->currentUser
->id()][$group_types_cid];
}
protected function getConfig($config_name) {
if (!isset($this->configurations[$config_name])) {
$this->configurations[$config_name] = $this->baseStorage
->read($config_name);
}
return $this->configurations[$config_name];
}
public function getCacheSuffix() {
return 'GroupMenuConfigOverrides';
}
public function getCacheableMetadata($name) {
return new CacheableMetadata();
}
public function createConfigObject($name, $collection = StorageInterface::DEFAULT_COLLECTION) {
return NULL;
}
}