-
Notifications
You must be signed in to change notification settings - Fork 0
/
ggroup_role_mapper.module
175 lines (158 loc) · 6.08 KB
/
ggroup_role_mapper.module
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
<?php
/**
* @file
* Enables Subgroup role mapper functionality.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\group\Entity\GroupType;
/**
* Implements hook_form_alter().
*/
function ggroup_role_mapper_form_group_relationship_type_add_form_alter (
&$form,
FormStateInterface $form_state,
$form_id
) {
$route_matcher = \Drupal::service('current_route_match');
$plugin_id = $route_matcher->getParameter('plugin_id');
if (strpos($plugin_id, 'ggroup:') === FALSE) {
return;
}
$subgroup_type_id = str_replace('ggroup:', '', $plugin_id);
$group_type_id = $route_matcher->getParameter('group_type');
_ggroup_role_mapper_mapping_form($form, $plugin_id, $group_type_id, $subgroup_type_id);
}
/**
* Implements hook_form_alter().
*/
function ggroup_role_mapper_form_group_relationship_type_edit_form_alter (
&$form,
\Drupal\Core\Form\FormStateInterface $form_state,
$form_id
) {
$route_matcher = \Drupal::service('current_route_match');
$group_relationship_type = $route_matcher->getParameter('group_relationship_type');
$plugin_id = $group_relationship_type->getPluginId();
if (strpos($plugin_id, 'ggroup:') === FALSE) {
return;
}
$group_type_id = $group_relationship_type->getGroupTypeId();
$subgroup_type_id = $group_relationship_type->getPlugin()->getRelationType()->getEntityBundle();
_ggroup_role_mapper_mapping_form($form, $plugin_id, $group_type_id, $subgroup_type_id);
}
/**
* Adds mapping settings between parent and subgroups.
*
* @param array $form
* Form array.
* @param string $plugin_id
* Plugin ID.
* @param string $group_type_id
* Group type id.
* @param $subgroup_type_id
* Group type id.
*/
function _ggroup_role_mapper_mapping_form(&$form, $plugin_id, $group_type_id, $subgroup_type_id) {
// We handle only subgroup plugins.
$group_types = GroupType::loadMultiple();
$group_type = $group_types[$group_type_id];
$sub_group_type = $group_types[$subgroup_type_id];
$collection = \Drupal::service('group_relation_type.manager')->getInstalled($group_type);
$configuration = $collection->getConfiguration();
// We create form field to map parent roles to child roles, and map child
// roles to parent roles. This allows for permissions/membership to
// propagate up/down.
$parent_roles = $group_type->getRoles();
$parent_options = [];
foreach ($parent_roles as $role_id => $role) {
$parent_options[$role_id] = $role->label() . ' (' . $role_id . ')';
}
$child_roles = $sub_group_type->getRoles();
$child_options = [];
foreach ($child_roles as $role_id => $role) {
$child_options[$role_id] = $role->label() . ' (' . $role_id . ')';
}
$group_types_options = [];
foreach ($group_types as $group_type) {
foreach ($group_type->getRoles() as $role_id => $role) {
$group_types_options[$group_type->label()][$role_id] = $role->label() . ' (' . $role_id . ')';
}
}
$none_string = t('- None -');
$container_with_tree_options = [
'#type' => 'container',
'#tree' => TRUE,
];
$form['parent_mapping'] = [
'#type' => 'fieldset',
'#title' => t('Map group roles to subgroup roles to allow group membership and permissions to be inherited by the subgroup.'),
];
foreach ($parent_options as $role_id => $role_name) {
$parent_container_id = "role_container_{$role_id}";
$form['parent_mapping'][$parent_container_id] = [
'#type' => 'fieldset',
'#title' => $role_name,
];
$form['parent_mapping'][$parent_container_id]['parent_role_mapping'] = $container_with_tree_options;
$form['parent_mapping'][$parent_container_id]['parent_role_mapping'][$role_id] = [
'#type' => 'select',
'#title' => t('Parent target role'),
'#options' => $child_options,
'#empty_option' => $none_string,
'#default_value' => $configuration[$plugin_id]['parent_role_mapping'][$role_id] ?? NULL,
];
$form['parent_mapping'][$parent_container_id]['parent_target_roles'] = $container_with_tree_options;
$form['parent_mapping'][$parent_container_id]['parent_target_roles'][$role_id] = [
'#type' => 'select',
'#title' => 'Target roles',
'#options' => $group_types_options,
'#empty_option' => $none_string,
'#multiple' => TRUE,
'#default_value' => $configuration[$plugin_id]['parent_target_roles'][$role_id] ?? [],
'#states' => [
'visible' => [
':input[name="parent_role_mapping[' . $role_id . ']"]' => ['!value' => ''],
],
'disabled' => [
':input[name="parent_role_mapping[' . $role_id . ']"]' => ['value' => ''],
],
],
];
}
$form['child_mapping'] = [
'#type' => 'fieldset',
'#title' => t('Map subgroup roles to group roles to allow subgroup membership and permissions to be propagated to the group.'),
];
foreach ($child_options as $role_id => $role_name) {
$child_container_id = "child_container_{$role_id}";
$form['child_mapping'][$child_container_id] = [
'#type' => 'fieldset',
'#title' => $role_name,
];
$form['child_mapping'][$child_container_id]['child_role_mapping'] = $container_with_tree_options;
$form['child_mapping'][$child_container_id]['child_role_mapping'][$role_id] = [
'#type' => 'select',
'#title' => 'Child target role',
'#options' => $parent_options,
'#empty_option' => $none_string,
'#default_value' => $configuration[$plugin_id]['child_role_mapping'][$role_id] ?? NULL,
];
$form['child_mapping'][$child_container_id]['child_target_roles'] = $container_with_tree_options;
$form['child_mapping'][$child_container_id]['child_target_roles'][$role_id] = [
'#type' => 'select',
'#title' => t('Target roles'),
'#options' => $group_types_options,
'#empty_option' => $none_string,
'#multiple' => TRUE,
'#default_value' => $configuration[$plugin_id]['child_target_roles'][$role_id] ?? [],
'#states' => [
'visible' => [
':input[name="child_role_mapping[' . $role_id . ']"]' => ['!value' => ''],
],
'disabled' => [
':input[name="child_role_mapping[' . $role_id . ']"]' => ['value' => ''],
],
],
];
}
}