profile2.api.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. /**
  3. * @file
  4. * This file contains no working PHP code; it exists to provide additional
  5. * documentation for doxygen as well as to document hooks in the standard
  6. * Drupal manner.
  7. */
  8. /**
  9. * @addtogroup hooks
  10. * @{
  11. */
  12. /**
  13. * Act on profiles being loaded from the database.
  14. *
  15. * This hook is invoked during profile loading, which is handled by
  16. * entity_load(), via the EntityCRUDController.
  17. *
  18. * @param $entities
  19. * An array of profile2 entities being loaded, keyed by id.
  20. *
  21. * @see hook_entity_load()
  22. */
  23. function hook_profile2_load($entities) {
  24. $result = db_query('SELECT pid, foo FROM {mytable} WHERE pid IN(:ids)', array(':ids' => array_keys($entities)));
  25. foreach ($result as $record) {
  26. $entities[$record->pid]->foo = $record->foo;
  27. }
  28. }
  29. /**
  30. * Respond when a profile is inserted.
  31. *
  32. * This hook is invoked after the profile is inserted into the database.
  33. *
  34. * @param profile
  35. * The profile that is being inserted.
  36. *
  37. * @see hook_entity_insert()
  38. */
  39. function hook_profile2_insert($profile) {
  40. db_insert('mytable')
  41. ->fields(array(
  42. 'pid' => $profile->pid,
  43. 'extra' => $profile->extra,
  44. ))
  45. ->execute();
  46. }
  47. /**
  48. * Act on a profile being inserted or updated.
  49. *
  50. * This hook is invoked before the profile is saved to the database.
  51. *
  52. * @param $profile
  53. * The profile that is being inserted or updated.
  54. *
  55. * @see hook_entity_presave()
  56. */
  57. function hook_profile2_presave($profile) {
  58. $profile->extra = 'foo';
  59. }
  60. /**
  61. * Respond to a profile being updated.
  62. *
  63. * This hook is invoked after the profile has been updated in the database.
  64. *
  65. * @param $profile
  66. * The $profile that is being updated.
  67. *
  68. * @see hook_entity_update()
  69. */
  70. function hook_profile2_update($profile) {
  71. db_update('mytable')
  72. ->fields(array('extra' => $profile->extra))
  73. ->condition('pid', $profile->pid)
  74. ->execute();
  75. }
  76. /**
  77. * Respond to profile deletion.
  78. *
  79. * This hook is invoked after the profile has been removed from the database.
  80. *
  81. * @param $profile
  82. * The profile that is being deleted.
  83. *
  84. * @see hook_entity_delete()
  85. */
  86. function hook_profile2_delete($profile) {
  87. db_delete('mytable')
  88. ->condition('pid', $profile->pid)
  89. ->execute();
  90. }
  91. /**
  92. * Act on a profile that is being assembled before rendering.
  93. *
  94. * @param $profile
  95. * The profile entity.
  96. * @param $view_mode
  97. * The view mode the profile is rendered in.
  98. * @param $langcode
  99. * The language code used for rendering.
  100. *
  101. * The module may add elements to $profile->content prior to rendering. The
  102. * structure of $profile->content is a renderable array as expected by
  103. * drupal_render().
  104. *
  105. * @see hook_entity_prepare_view()
  106. * @see hook_entity_view()
  107. */
  108. function hook_profile2_view($profile, $view_mode, $langcode) {
  109. $profile->content['my_additional_field'] = array(
  110. '#markup' => $additional_field,
  111. '#weight' => 10,
  112. '#theme' => 'mymodule_my_additional_field',
  113. );
  114. }
  115. /**
  116. * Alter the results of entity_view() for profiles.
  117. *
  118. * @param $build
  119. * A renderable array representing the profile content.
  120. *
  121. * This hook is called after the content has been assembled in a structured
  122. * array and may be used for doing processing which requires that the complete
  123. * profile content structure has been built.
  124. *
  125. * If the module wishes to act on the rendered HTML of the profile rather than
  126. * the structured content array, it may use this hook to add a #post_render
  127. * callback. Alternatively, it could also implement hook_preprocess_profile2().
  128. * See drupal_render() and theme() documentation respectively for details.
  129. *
  130. * @see hook_entity_view_alter()
  131. */
  132. function hook_profile2_view_alter(&$build) {
  133. if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
  134. // Change its weight.
  135. $build['an_additional_field']['#weight'] = -10;
  136. // Add a #post_render callback to act on the rendered HTML of the entity.
  137. $build['#post_render'][] = 'my_module_post_render';
  138. }
  139. }
  140. /**
  141. * Act on profile type being loaded from the database.
  142. *
  143. * This hook is invoked during profile type loading, which is handled by
  144. * entity_load(), via the EntityCRUDController.
  145. *
  146. * @param $types
  147. * An array of profiles being loaded, keyed by profile type names.
  148. */
  149. function hook_profile2_type_load($types) {
  150. if (isset($types['main'])) {
  151. $types['main']->userCategory = FALSE;
  152. $types['main']->userView = FALSE;
  153. }
  154. }
  155. /**
  156. * Respond when a profile type is inserted.
  157. *
  158. * This hook is invoked after the profile type is inserted into the database.
  159. *
  160. * @param $type
  161. * The profile type that is being inserted.
  162. */
  163. function hook_profile2_type_insert($type) {
  164. db_insert('mytable')
  165. ->fields(array(
  166. 'id' => $type->id,
  167. 'extra' => $type->extra,
  168. ))
  169. ->execute();
  170. }
  171. /**
  172. * Act on a profile type being inserted or updated.
  173. *
  174. * This hook is invoked before the profile type is saved to the database.
  175. *
  176. * @param $type
  177. * The profile type that is being inserted or updated.
  178. */
  179. function hook_profile2_type_presave($type) {
  180. $type->extra = 'foo';
  181. }
  182. /**
  183. * Respond to updates to a profile.
  184. *
  185. * This hook is invoked after the profile type has been updated in the database.
  186. *
  187. * @param $type
  188. * The profile type that is being updated.
  189. */
  190. function hook_profile2_type_update($type) {
  191. db_update('mytable')
  192. ->fields(array('extra' => $type->extra))
  193. ->condition('id', $type->id)
  194. ->execute();
  195. }
  196. /**
  197. * Respond to profile type deletion.
  198. *
  199. * This hook is invoked after the profile type has been removed from the
  200. * database.
  201. *
  202. * @param $type
  203. * The profile type that is being deleted.
  204. */
  205. function hook_profile2_type_delete($type) {
  206. db_delete('mytable')
  207. ->condition('id', $type->id)
  208. ->execute();
  209. }
  210. /**
  211. * Define default profile type configurations.
  212. *
  213. * @return
  214. * An array of default profile types, keyed by profile type names.
  215. */
  216. function hook_default_profile2_type() {
  217. $types['main'] = new ProfileType(array(
  218. 'type' => 'main',
  219. 'label' => t('Profile'),
  220. 'weight' => 0,
  221. 'locked' => TRUE,
  222. ));
  223. return $types;
  224. }
  225. /**
  226. * Alter default profile type configurations.
  227. *
  228. * @param $defaults
  229. * An array of default profile types, keyed by type names.
  230. *
  231. * @see hook_default_profile2_type()
  232. */
  233. function hook_default_profile2_type_alter(&$defaults) {
  234. $defaults['main']->label = 'custom label';
  235. }
  236. /**
  237. * Alter profile2 forms.
  238. *
  239. * Modules may alter the profile2 entity form regardless to which form it is
  240. * attached by making use of this hook or the profile type specifiy
  241. * hook_form_profile2_edit_PROFILE_TYPE_form_alter(). #entity_builders may be
  242. * used in order to copy the values of added form elements to the entity, just
  243. * as described by entity_form_submit_build_entity().
  244. *
  245. * @param $form
  246. * Nested array of form elements that comprise the form.
  247. * @param $form_state
  248. * A keyed array containing the current state of the form.
  249. *
  250. * @see profile2_attach_form()
  251. */
  252. function hook_form_profile2_form_alter(&$form, &$form_state) {
  253. // Your alterations.
  254. }
  255. /**
  256. * Control access to profiles.
  257. *
  258. * Modules may implement this hook if they want to have a say in whether or not
  259. * a given user has access to perform a given operation on a profile.
  260. *
  261. * @param $op
  262. * The operation being performed. One of 'view', 'edit' (being the same as
  263. * 'create' or 'update') and 'delete'.
  264. * @param $profile
  265. * (optional) A profile to check access for. If nothing is given, access for
  266. * all profiles is determined.
  267. * @param $account
  268. * (optional) The user to check for. If no account is passed, access is
  269. * determined for the global user.
  270. * @return boolean
  271. * Return TRUE to grant access, FALSE to explicitly deny access. Return NULL
  272. * or nothing to not affect the operation.
  273. * Access is granted as soon as a module grants access and no one denies
  274. * access. Thus if no module explicitly grants access, access will be denied.
  275. *
  276. * @see profile2_access()
  277. */
  278. function hook_profile2_access($op, $profile = NULL, $account = NULL) {
  279. if (isset($profile)) {
  280. // Explicitly deny access for a 'secret' profile type.
  281. if ($profile->type == 'secret' && !user_access('custom permission')) {
  282. return FALSE;
  283. }
  284. // For profiles other than the default profile grant access.
  285. if ($profile->type != 'main' && user_access('custom permission')) {
  286. return TRUE;
  287. }
  288. // In other cases do not alter access.
  289. }
  290. }
  291. /**
  292. * @}
  293. */