og.api.php

  1. 7.x-2.x og/og.api.php
  2. 7.x-1.x og/og.api.php
  3. 6.x og/og.api.php

Hooks provided by the Organic groups module.

Functions

Namesort descending Description
hook_default_og_membership_type Define default OG membership type configurations.
hook_default_og_membership_type_alter Alter default OG membership type configurations.
hook_og_default_roles Set a default role that will be used as a global role.
hook_og_default_roles_alter Alter the default roles.
hook_og_fields_info Provide information about fields that are related to Organic groups.
hook_og_fields_info_alter TODO
hook_og_invalidate_cache Act upon organic groups cache clearing.
hook_og_membership_delete Responds to OG membership deletion.
hook_og_membership_insert Responds when a OG membership is inserted.
hook_og_membership_load Acts on OG memberships being loaded from the database.
hook_og_membership_presave Acts on a OG membership being inserted or updated.
hook_og_membership_type_delete Responds to OG membership type deletion.
hook_og_membership_type_insert Responds when a OG membership type is inserted.
hook_og_membership_type_load Acts on OG membership types being loaded from the database.
hook_og_membership_type_presave Acts on a OG membership type being inserted or updated.
hook_og_membership_type_update Responds to a OG membership type being updated.
hook_og_membership_update Responds to a OG membership being updated.
hook_og_permission Add group permissions.
hook_og_permission_alter Alter the organic groups permissions.
hook_og_role_change_permissions Give a notification about OG role permissions change.
hook_og_role_delete Allow modules to act upon existing group role deletion.
hook_og_role_grant Allow modules to react upon a role being granted.
hook_og_role_insert Allow modules to act upon new group role.
hook_og_role_revoke Allow modules to react upon a role being revoked.
hook_og_role_update Allow modules to act upon existing group role update.
hook_og_user_access_alter Alter the permissions of a user in a group.

File

og/og.api.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Organic groups module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Add group permissions.
  12. */
  13. function hook_og_permission() {
  14. return array(
  15. 'subscribe' => array(
  16. 'title' => t('Subscribe user to group'),
  17. 'description' => t("Allow user to be a member of a group (approval required)."),
  18. // Determine to which role to limit the permission. For example the
  19. // "subscribe" can't be assigned only to a non-member, as a member doesn't
  20. // need it.
  21. 'roles' => array(OG_ANONYMOUS_ROLE),
  22. // Determine to which roles the permissions will be enabled by default.
  23. 'default role' => array(OG_ANONYMOUS_ROLE),
  24. ),
  25. );
  26. }
  27. /**
  28. * Alter the organic groups permissions.
  29. *
  30. * @param $perms
  31. * The permissions passed by reference.
  32. */
  33. function hook_og_permission_alter(&$perms) {
  34. }
  35. /**
  36. * Set a default role that will be used as a global role.
  37. *
  38. * A global role, is a role that is assigned by default to all new groups.
  39. */
  40. function hook_og_default_roles() {
  41. return array('super admin');
  42. }
  43. /**
  44. * Alter the default roles.
  45. *
  46. * The anonymous and authenticated member roles are not alterable.
  47. *
  48. * @param $roles
  49. * Array with the default roles name.
  50. */
  51. function hook_og_default_roles_alter(&$roles) {
  52. // Remove a default role.
  53. unset($roles['super admin']);
  54. }
  55. /**
  56. * Allow modules to act upon new group role.
  57. *
  58. * @param $role
  59. * The group role object.
  60. */
  61. function hook_og_role_insert($role) {
  62. }
  63. /**
  64. * Allow modules to act upon existing group role update.
  65. *
  66. * @param $role
  67. * The group role object.
  68. */
  69. function hook_og_role_update($role) {
  70. }
  71. /**
  72. * Allow modules to act upon existing group role deletion.
  73. *
  74. * @param $role
  75. * The deleted group role object. The object is actually a dummy, as the data
  76. * is already deleted from the database. However, we pass the object to allow
  77. * implementing modules to properly identify the deleted role.
  78. */
  79. function hook_og_role_delete($role) {
  80. }
  81. /**
  82. * Allow modules to react upon a role being granted.
  83. *
  84. * @param $entity_type
  85. * The entity type of the group in which a role has been granted.
  86. * @param $gid
  87. * The group id of the group in which a role has been granted.
  88. * @param $uid
  89. * The user id of the user to whom a role has been granted.
  90. * @param $rid
  91. * The OG role id being granted to the user.
  92. */
  93. function hook_og_role_grant($entity_type, $gid, $uid, $rid) {
  94. }
  95. /**
  96. * Allow modules to react upon a role being revoked.
  97. *
  98. * @param $entity_type
  99. * The entity type of the group in which a role has been revoked.
  100. * @param $gid
  101. * The group id of the group in which a role has been revoked.
  102. * @param $uid
  103. * The user id of the user to whom a role has been revoked.
  104. * @param $rid
  105. * The OG role id being revoked from the user.
  106. */
  107. function hook_og_role_revoke($entity_type, $gid, $uid, $rid) {
  108. }
  109. /**
  110. * Give a notification about OG role permissions change.
  111. *
  112. * @param $role
  113. * The role object of the changed role.
  114. * @param $grant
  115. * A list of granted permission names.
  116. * @param $revoke
  117. * A list of revoked permission names.
  118. */
  119. function hook_og_role_change_permissions($role, $grant, $revoke) {
  120. if (!$role->gid) {
  121. drupal_set_message(t('Global group permissions granted for @role users: @permissions', array('@role' => $role->name, '@permissions' => implode(', ', $grant))));
  122. }
  123. }
  124. /**
  125. * Provide information about fields that are related to Organic groups.
  126. *
  127. * Using this info, Organic groups is aware of the fields, and allows adding
  128. * them to the correct bundle.
  129. *
  130. * - type: Array with the values "group" and/ or "group content". To define to
  131. * which bundles the field may be attached.
  132. * - Description: The description of the field.
  133. * - field: The field info array as will be passed to field_create_field().
  134. * - instance: The field instance array as will be passed to
  135. * field_info_instance().
  136. * - entity type: Optional; Array of the entity types this field can be attached
  137. * to. The field will not be attachable to other entity types. Defaults to
  138. * empty array.
  139. */
  140. function hook_og_fields_info() {
  141. $items = array();
  142. $items[OG_GROUP_FIELD] = array(
  143. 'type' => array('group'),
  144. 'description' => t('Determine if this should be a group.'),
  145. 'field' => array(
  146. 'field_name' => OG_GROUP_FIELD,
  147. 'no_ui' => TRUE,
  148. 'type' => 'list_boolean',
  149. 'cardinality' => 1,
  150. 'settings' => array(
  151. 'allowed_values' => array(0 => 'Not a group type', 1 => 'Group type'),
  152. 'allowed_values_function' => '',
  153. ),
  154. ),
  155. 'instance' => array(
  156. 'label' => t('Group type'),
  157. 'widget_type' => 'options_select',
  158. 'required' => TRUE,
  159. // Make the group type default.
  160. 'default_value' => array(0 => array('value' => 1)),
  161. 'view modes' => array(
  162. 'full' => array(
  163. 'label' => t('Full'),
  164. 'type' => 'og_group_subscribe',
  165. 'custom settings' => FALSE,
  166. ),
  167. 'teaser' => array(
  168. 'label' => t('Teaser'),
  169. 'type' => 'og_group_subscribe',
  170. 'custom settings' => FALSE,
  171. ),
  172. ),
  173. ),
  174. );
  175. return $items;
  176. }
  177. /**
  178. * TODO
  179. */
  180. function hook_og_fields_info_alter(&$fields_info) {
  181. }
  182. /**
  183. * Act upon organic groups cache clearing.
  184. *
  185. * This can be used by implementing modules, that need to clear the cache
  186. * as-well.
  187. */
  188. function hook_og_invalidate_cache($gids = array()) {
  189. $caches = array(
  190. 'og_foo',
  191. 'og_bar',
  192. );
  193. foreach ($caches as $cache) {
  194. drupal_static_reset($cache);
  195. }
  196. }
  197. /**
  198. * Alter the permissions of a user in a group.
  199. *
  200. * @param $perm
  201. * The permissions of a user, passed by reference.
  202. * @param $context
  203. * Array with:
  204. * - string: The permission asked for the user.
  205. * - group_type: The entity type of the group.
  206. * - group: The group object.
  207. * - account: The user account.
  208. */
  209. function hook_og_user_access_alter(&$perm, $context) {
  210. // If user ID 2 doesn't already have a permission then enable it.
  211. if (empty($perm['foo']) && $context['account']->uid = 2) {
  212. $perm['foo'] = TRUE;
  213. }
  214. }
  215. /**
  216. * Acts on OG membership types being loaded from the database.
  217. *
  218. * This hook is invoked during OG membership type loading, which is handled by
  219. * entity_load(), via the EntityCRUDController.
  220. *
  221. * @param array $og_membership_types
  222. * An array of OG membership type entities being loaded, keyed by id.
  223. *
  224. * @see hook_entity_load()
  225. */
  226. function hook_og_membership_type_load(array $og_membership_types) {
  227. $result = db_query('SELECT pid, foo FROM {mytable} WHERE pid IN(:ids)', array(':ids' => array_keys($entities)));
  228. foreach ($result as $record) {
  229. $entities[$record->pid]->foo = $record->foo;
  230. }
  231. }
  232. /**
  233. * Responds when a OG membership type is inserted.
  234. *
  235. * This hook is invoked after the OG membership type is inserted into the database.
  236. *
  237. * @param OgMembershipType $og_membership
  238. * The OG membership type that is being inserted.
  239. *
  240. * @see hook_entity_insert()
  241. */
  242. function hook_og_membership_type_insert(OgMembershipType $og_membership) {
  243. db_insert('mytable')
  244. ->fields(array(
  245. 'id' => entity_id('og_membership_type', $og_membership),
  246. 'extra' => print_r($og_membership, TRUE),
  247. ))
  248. ->execute();
  249. }
  250. /**
  251. * Acts on a OG membership type being inserted or updated.
  252. *
  253. * This hook is invoked before the OG membership type is saved to the database.
  254. *
  255. * @param OgMembershipType $og_membership
  256. * The OG membership type that is being inserted or updated.
  257. *
  258. * @see hook_entity_presave()
  259. */
  260. function hook_og_membership_type_presave(OgMembershipType $og_membership) {
  261. $og_membership->name = 'foo';
  262. }
  263. /**
  264. * Responds to a OG membership type being updated.
  265. *
  266. * This hook is invoked after the OG membership type has been updated in the database.
  267. *
  268. * @param OgMembershipType $og_membership
  269. * The OG membership type that is being updated.
  270. *
  271. * @see hook_entity_update()
  272. */
  273. function hook_og_membership_type_update(OgMembershipType $og_membership) {
  274. db_update('mytable')
  275. ->fields(array('extra' => print_r($og_membership, TRUE)))
  276. ->condition('id', entity_id('og_membership_type', $og_membership))
  277. ->execute();
  278. }
  279. /**
  280. * Responds to OG membership type deletion.
  281. *
  282. * This hook is invoked after the OG membership type has been removed from the database.
  283. *
  284. * @param OgMembershipType $og_membership
  285. * The OG membership type that is being deleted.
  286. *
  287. * @see hook_entity_delete()
  288. */
  289. function hook_og_membership_type_delete(OgMembershipType $og_membership) {
  290. db_delete('mytable')
  291. ->condition('pid', entity_id('og_membership_type', $og_membership))
  292. ->execute();
  293. }
  294. /**
  295. * Define default OG membership type configurations.
  296. *
  297. * @return
  298. * An array of default OG membership types, keyed by machine names.
  299. *
  300. * @see hook_default_og_membership_type_alter()
  301. */
  302. function hook_default_og_membership_type() {
  303. $defaults['main'] = entity_create('og_membership_type', array(
  304. // �
  305. ));
  306. return $defaults;
  307. }
  308. /**
  309. * Alter default OG membership type configurations.
  310. *
  311. * @param array $defaults
  312. * An array of default OG membership types, keyed by machine names.
  313. *
  314. * @see hook_default_og_membership_type()
  315. */
  316. function hook_default_og_membership_type_alter(array &$defaults) {
  317. $defaults['main']->name = 'custom name';
  318. }
  319. /**
  320. * Acts on OG memberships being loaded from the database.
  321. *
  322. * This hook is invoked during OG membership loading, which is handled by
  323. * entity_load(), via the EntityCRUDController.
  324. *
  325. * @param array $og_memberships
  326. * An array of OG membership entities being loaded, keyed by id.
  327. *
  328. * @see hook_entity_load()
  329. */
  330. function hook_og_membership_load(array $og_memberships) {
  331. $result = db_query('SELECT pid, foo FROM {mytable} WHERE pid IN(:ids)', array(':ids' => array_keys($og_memberships)));
  332. foreach ($result as $record) {
  333. $og_memberships[$record->pid]->foo = $record->foo;
  334. }
  335. }
  336. /**
  337. * Responds when a OG membership is inserted.
  338. *
  339. * This hook is invoked after the OG membership is inserted into the database.
  340. *
  341. * @param OgMembership $og_membership
  342. * The OG membership that is being inserted.
  343. *
  344. * @see hook_entity_insert()
  345. */
  346. function hook_og_membership_insert(OgMembership $og_membership) {
  347. db_insert('mytable')
  348. ->fields(array(
  349. 'id' => entity_id('og_membership', $og_membership),
  350. 'extra' => print_r($og_membership, TRUE),
  351. ))
  352. ->execute();
  353. }
  354. /**
  355. * Acts on a OG membership being inserted or updated.
  356. *
  357. * This hook is invoked before the OG membership is saved to the database.
  358. *
  359. * @param OgMembership $og_membership
  360. * The OG membership that is being inserted or updated.
  361. *
  362. * @see hook_entity_presave()
  363. */
  364. function hook_og_membership_presave(OgMembership $og_membership) {
  365. $og_membership->name = 'foo';
  366. }
  367. /**
  368. * Responds to a OG membership being updated.
  369. *
  370. * This hook is invoked after the OG membership has been updated in the database.
  371. *
  372. * @param OgMembership $og_membership
  373. * The OG membership that is being updated.
  374. *
  375. * @see hook_entity_update()
  376. */
  377. function hook_og_membership_update(OgMembership $og_membership) {
  378. db_update('mytable')
  379. ->fields(array('extra' => print_r($og_membership, TRUE)))
  380. ->condition('id', entity_id('og_membership', $og_membership))
  381. ->execute();
  382. }
  383. /**
  384. * Responds to OG membership deletion.
  385. *
  386. * This hook is invoked after the OG membership has been removed from the database.
  387. *
  388. * @param OgMembership $og_membership
  389. * The OG membership that is being deleted.
  390. *
  391. * @see hook_entity_delete()
  392. */
  393. function hook_og_membership_delete(OgMembership $og_membership) {
  394. db_delete('mytable')
  395. ->condition('pid', entity_id('og_membership', $og_membership))
  396. ->execute();
  397. }
  398. /**
  399. * @} End of "addtogroup hooks".
  400. */