forked from Gizra/og
-
Notifications
You must be signed in to change notification settings - Fork 0
/
og.api.php
439 lines (400 loc) · 11.5 KB
/
og.api.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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
/**
* @file
* Hooks provided by the Organic groups module.
*/
/**
* @addtogroup hooks
* @{
*/
/**
* Add group permissions.
*/
function hook_og_permission() {
return array(
'subscribe' => array(
'title' => t('Subscribe user to group'),
'description' => t("Allow user to be a member of a group (approval required)."),
// Determine to which role to limit the permission. For example the
// "subscribe" can't be assigned only to a non-member, as a member doesn't
// need it.
'roles' => array(OG_ANONYMOUS_ROLE),
// Determine to which roles the permissions will be enabled by default.
'default role' => array(OG_ANONYMOUS_ROLE),
),
);
}
/**
* Alter the organic groups permissions.
*
* @param $perms
* The permissions passed by reference.
*/
function hook_og_permission_alter(&$perms) {
}
/**
* Set a default role that will be used as a global role.
*
* A global role, is a role that is assigned by default to all new groups.
*/
function hook_og_default_roles() {
return array('super admin');
}
/**
* Alter the default roles.
*
* The anonymous and authenticated member roles are not alterable.
*
* @param $roles
* Array with the default roles name.
*/
function hook_og_default_roles_alter(&$roles) {
// Remove a default role.
unset($roles['super admin']);
}
/**
* Allow modules to act upon new group role.
*
* @param $role
* The group role object.
*/
function hook_og_role_insert($role) {
}
/**
* Allow modules to act upon existing group role update.
*
* @param $role
* The group role object.
*/
function hook_og_role_update($role) {
}
/**
* Allow modules to act upon existing group role deletion.
*
* @param $role
* The deleted group role object. The object is actually a dummy, as the data
* is already deleted from the database. However, we pass the object to allow
* implementing modules to properly identify the deleted role.
*/
function hook_og_role_delete($role) {
}
/**
* Allow modules to react upon a role being granted.
*
* @param $entity_type
* The entity type of the group in which a role has been granted.
* @param $gid
* The group id of the group in which a role has been granted.
* @param $uid
* The user id of the user to whom a role has been granted.
* @param $rid
* The OG role id being granted to the user.
*/
function hook_og_role_grant($entity_type, $gid, $uid, $rid) {
}
/**
* Allow modules to react upon a role being revoked.
*
* @param $entity_type
* The entity type of the group in which a role has been revoked.
* @param $gid
* The group id of the group in which a role has been revoked.
* @param $uid
* The user id of the user to whom a role has been revoked.
* @param $rid
* The OG role id being revoked from the user.
*/
function hook_og_role_revoke($entity_type, $gid, $uid, $rid) {
}
/**
* Give a notification about OG role permissions change.
*
* @param $role
* The role object of the changed role.
* @param $grant
* A list of granted permission names.
* @param $revoke
* A list of revoked permission names.
*/
function hook_og_role_change_permissions($role, $grant, $revoke) {
if (!$role->gid) {
drupal_set_message(t('Global group permissions granted for @role users: @permissions', array('@role' => $role->name, '@permissions' => implode(', ', $grant))));
}
}
/**
* Provide information about fields that are related to Organic groups.
*
* Using this info, Organic groups is aware of the fields, and allows adding
* them to the correct bundle.
*
* - type: Array with the values "group" and/ or "group content". To define to
* which bundles the field may be attached.
* - Description: The description of the field.
* - field: The field info array as will be passed to field_create_field().
* - instance: The field instance array as will be passed to
* field_info_instance().
* - entity type: Optional; Array of the entity types this field can be attached
* to. The field will not be attachable to other entity types. Defaults to
* empty array.
*/
function hook_og_fields_info() {
$items = array();
$items[OG_GROUP_FIELD] = array(
'type' => array('group'),
'description' => t('Determine if this should be a group.'),
'field' => array(
'field_name' => OG_GROUP_FIELD,
'no_ui' => TRUE,
'type' => 'list_boolean',
'cardinality' => 1,
'settings' => array(
'allowed_values' => array(0 => 'Not a group type', 1 => 'Group type'),
'allowed_values_function' => '',
),
),
'instance' => array(
'label' => t('Group type'),
'widget_type' => 'options_select',
'required' => TRUE,
// Make the group type default.
'default_value' => array(0 => array('value' => 1)),
'view modes' => array(
'full' => array(
'label' => t('Full'),
'type' => 'og_group_subscribe',
'custom settings' => FALSE,
),
'teaser' => array(
'label' => t('Teaser'),
'type' => 'og_group_subscribe',
'custom settings' => FALSE,
),
),
),
);
return $items;
}
/**
* TODO
*/
function hook_og_fields_info_alter(&$fields_info) {
}
/**
* Act upon organic groups cache clearing.
*
* This can be used by implementing modules, that need to clear the cache
* as-well.
*/
function hook_og_invalidate_cache($gids = array()) {
$caches = array(
'og_foo',
'og_bar',
);
foreach ($caches as $cache) {
drupal_static_reset($cache);
}
}
/**
* Alter the permissions of a user in a group.
*
* @param $perm
* The permissions of a user, passed by reference.
* @param $context
* Array with:
* - string: The permission asked for the user.
* - group_type: The entity type of the group.
* - group: The group object.
* - account: The user account.
*/
function hook_og_user_access_alter(&$perm, $context) {
// If user ID 2 doesn't already have a permission then enable it.
if (empty($perm['foo']) && $context['account']->uid = 2) {
$perm['foo'] = TRUE;
}
}
/**
* Acts on OG membership types being loaded from the database.
*
* This hook is invoked during OG membership type loading, which is handled by
* entity_load(), via the EntityCRUDController.
*
* @param array $og_membership_types
* An array of OG membership type entities being loaded, keyed by id.
*
* @see hook_entity_load()
*/
function hook_og_membership_type_load(array $og_membership_types) {
$result = db_query('SELECT pid, foo FROM {mytable} WHERE pid IN(:ids)', array(':ids' => array_keys($entities)));
foreach ($result as $record) {
$entities[$record->pid]->foo = $record->foo;
}
}
/**
* Responds when a OG membership type is inserted.
*
* This hook is invoked after the OG membership type is inserted into the database.
*
* @param OgMembershipType $og_membership
* The OG membership type that is being inserted.
*
* @see hook_entity_insert()
*/
function hook_og_membership_type_insert(OgMembershipType $og_membership) {
db_insert('mytable')
->fields(array(
'id' => entity_id('og_membership_type', $og_membership),
'extra' => print_r($og_membership, TRUE),
))
->execute();
}
/**
* Acts on a OG membership type being inserted or updated.
*
* This hook is invoked before the OG membership type is saved to the database.
*
* @param OgMembershipType $og_membership
* The OG membership type that is being inserted or updated.
*
* @see hook_entity_presave()
*/
function hook_og_membership_type_presave(OgMembershipType $og_membership) {
$og_membership->name = 'foo';
}
/**
* Responds to a OG membership type being updated.
*
* This hook is invoked after the OG membership type has been updated in the database.
*
* @param OgMembershipType $og_membership
* The OG membership type that is being updated.
*
* @see hook_entity_update()
*/
function hook_og_membership_type_update(OgMembershipType $og_membership) {
db_update('mytable')
->fields(array('extra' => print_r($og_membership, TRUE)))
->condition('id', entity_id('og_membership_type', $og_membership))
->execute();
}
/**
* Responds to OG membership type deletion.
*
* This hook is invoked after the OG membership type has been removed from the database.
*
* @param OgMembershipType $og_membership
* The OG membership type that is being deleted.
*
* @see hook_entity_delete()
*/
function hook_og_membership_type_delete(OgMembershipType $og_membership) {
db_delete('mytable')
->condition('pid', entity_id('og_membership_type', $og_membership))
->execute();
}
/**
* Define default OG membership type configurations.
*
* @return
* An array of default OG membership types, keyed by machine names.
*
* @see hook_default_og_membership_type_alter()
*/
function hook_default_og_membership_type() {
$defaults['main'] = entity_create('og_membership_type', array(
// �
));
return $defaults;
}
/**
* Alter default OG membership type configurations.
*
* @param array $defaults
* An array of default OG membership types, keyed by machine names.
*
* @see hook_default_og_membership_type()
*/
function hook_default_og_membership_type_alter(array &$defaults) {
$defaults['main']->name = 'custom name';
}
/**
* Acts on OG memberships being loaded from the database.
*
* This hook is invoked during OG membership loading, which is handled by
* entity_load(), via the EntityCRUDController.
*
* @param array $og_memberships
* An array of OG membership entities being loaded, keyed by id.
*
* @see hook_entity_load()
*/
function hook_og_membership_load(array $og_memberships) {
$result = db_query('SELECT pid, foo FROM {mytable} WHERE pid IN(:ids)', array(':ids' => array_keys($og_memberships)));
foreach ($result as $record) {
$og_memberships[$record->pid]->foo = $record->foo;
}
}
/**
* Responds when a OG membership is inserted.
*
* This hook is invoked after the OG membership is inserted into the database.
*
* @param OgMembership $og_membership
* The OG membership that is being inserted.
*
* @see hook_entity_insert()
*/
function hook_og_membership_insert(OgMembership $og_membership) {
db_insert('mytable')
->fields(array(
'id' => entity_id('og_membership', $og_membership),
'extra' => print_r($og_membership, TRUE),
))
->execute();
}
/**
* Acts on a OG membership being inserted or updated.
*
* This hook is invoked before the OG membership is saved to the database.
*
* @param OgMembership $og_membership
* The OG membership that is being inserted or updated.
*
* @see hook_entity_presave()
*/
function hook_og_membership_presave(OgMembership $og_membership) {
$og_membership->name = 'foo';
}
/**
* Responds to a OG membership being updated.
*
* This hook is invoked after the OG membership has been updated in the database.
*
* @param OgMembership $og_membership
* The OG membership that is being updated.
*
* @see hook_entity_update()
*/
function hook_og_membership_update(OgMembership $og_membership) {
db_update('mytable')
->fields(array('extra' => print_r($og_membership, TRUE)))
->condition('id', entity_id('og_membership', $og_membership))
->execute();
}
/**
* Responds to OG membership deletion.
*
* This hook is invoked after the OG membership has been removed from the database.
*
* @param OgMembership $og_membership
* The OG membership that is being deleted.
*
* @see hook_entity_delete()
*/
function hook_og_membership_delete(OgMembership $og_membership) {
db_delete('mytable')
->condition('pid', entity_id('og_membership', $og_membership))
->execute();
}
/**
* @} End of "addtogroup hooks".
*/