flag.api.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Flag module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Define one or more flag types.
  12. *
  13. * This hook may be placed in a $module.flag.inc file.
  14. *
  15. * @return
  16. * An array whose keys are flag type names and whose values are properties of
  17. * the flag type.
  18. * When a flag type is for an entity, the flag type name must match the entity
  19. * type.
  20. * Properties for flag types are as follows:
  21. * - 'title': The main label of the flag type.
  22. * - 'description': A longer description shown in the UI when creating a new
  23. * flag.
  24. * - 'handler': The name of the class implementing this flag type.
  25. * - 'module': (optional) The name of the module that should be registered as a
  26. * dependency for this flag type.
  27. *
  28. * @see flag_fetch_definition()
  29. */
  30. function hook_flag_type_info() {
  31. return array(
  32. 'node' => array(
  33. 'title' => t('Nodes'),
  34. 'description' => t("Nodes are a Drupal site's primary content."),
  35. 'handler' => 'flag_node',
  36. 'module' => 'node',
  37. ),
  38. );
  39. }
  40. /**
  41. * Alter flag type definitions provided by other modules.
  42. *
  43. * This hook may be placed in a $module.flag.inc file.
  44. *
  45. * @param $definitions
  46. * An array of flag definitions returned by hook_flag_type_info().
  47. */
  48. function hook_flag_type_info_alter(&$definitions) {
  49. }
  50. /**
  51. * Define default flags.
  52. */
  53. function hook_flag_default_flags() {
  54. $flags = array();
  55. $flags['bookmarks'] = array (
  56. 'entity_type' => 'node',
  57. 'title' => 'Bookmarks',
  58. 'global' => FALSE,
  59. 'types' => array (
  60. 0 => 'article',
  61. 1 => 'blog',
  62. ),
  63. 'flag_short' => 'Bookmark this',
  64. 'flag_long' => 'Add this post to your bookmarks',
  65. 'flag_message' => 'This post has been added to your bookmarks',
  66. 'unflag_short' => 'Unbookmark this',
  67. 'unflag_long' => 'Remove this post from your bookmarks',
  68. 'unflag_message' => 'This post has been removed from your bookmarks',
  69. 'unflag_denied_text' => '',
  70. 'link_type' => 'toggle',
  71. 'weight' => 0,
  72. 'show_in_links' => array (
  73. 'full' => TRUE,
  74. 'token' => FALSE,
  75. ),
  76. 'show_as_field' => FALSE,
  77. 'show_on_form' => FALSE,
  78. 'access_author' => '',
  79. 'show_contextual_link' => TRUE,
  80. 'show_on_profile' => FALSE,
  81. 'access_uid' => '',
  82. 'api_version' => 3,
  83. );
  84. return $flags;
  85. }
  86. /**
  87. * Allow modules to alter a flag when it is initially loaded.
  88. *
  89. * @see flag_get_flags().
  90. */
  91. function hook_flag_alter(&$flag) {
  92. }
  93. /**
  94. * Alter a flag's default options.
  95. *
  96. * Modules that wish to extend flags and provide additional options must declare
  97. * them here so that their additions to the flag admin form are saved into the
  98. * flag object.
  99. *
  100. * @param $options
  101. * The array of default options for the flag type, with the options for the
  102. * flag's link type merged in.
  103. * @param $flag
  104. * The flag object.
  105. *
  106. * @see flag_flag::options()
  107. */
  108. function hook_flag_options_alter(&$options, $flag) {
  109. }
  110. /**
  111. * Act on an object being flagged.
  112. *
  113. * @param $flag
  114. * The flag object.
  115. * @param $entity_id
  116. * The id of the entity the flag is on.
  117. * @param $account
  118. * The user account performing the action.
  119. * @param $flagging_id
  120. * The flagging entity.
  121. */
  122. function hook_flag_flag($flag, $entity_id, $account, $flagging) {
  123. }
  124. /**
  125. * Act on an object being unflagged.
  126. *
  127. * This is invoked after the flag count has been decreased, but before the
  128. * flagging entity has been deleted.
  129. *
  130. * @param $flag
  131. * The flag object.
  132. * @param $entity_id
  133. * The id of the entity the flag is on.
  134. * @param $account
  135. * The user account performing the action.
  136. * @param $flagging
  137. * The flagging entity.
  138. */
  139. function hook_flag_unflag($flag, $entity_id, $account, $flagging) {
  140. }
  141. /**
  142. * Perform custom validation on a flag before flagging/unflagging.
  143. *
  144. * @param $action
  145. * The action about to be carried out. Either 'flag' or 'unflag'.
  146. * @param $flag
  147. * The flag object.
  148. * @param $entity_id
  149. * The id of the entity the user is trying to flag or unflag.
  150. * @param $account
  151. * The user account performing the action.
  152. * @param $flagging
  153. * The flagging entity.
  154. *
  155. * @return
  156. * Optional array: textual error with the error-name as the key.
  157. * If the error name is 'access-denied' and javascript is disabled,
  158. * drupal_access_denied will be called and a 403 will be returned.
  159. * If validation is successful, do not return a value.
  160. */
  161. function hook_flag_validate($action, $flag, $entity_id, $account, $skip_permission_check, $flagging) {
  162. // We're only operating on the "test" flag, and users may always unflag.
  163. if ($flag->name == 'test' && $action == 'flag') {
  164. // Get all flags set by the current user.
  165. $flags = flag_get_user_flags('node', NULL, $account->uid, $sid = NULL, $reset = FALSE);
  166. // Check if this user has any flags of this type set.
  167. if (isset($flags['test'])) {
  168. $count = count($flags[$flag->name]);
  169. if ($count >= 2) {
  170. // Users may flag only 2 nodes with this flag.
  171. return(array('access-denied' => t('You may only flag 2 nodes with the test flag.')));
  172. }
  173. }
  174. }
  175. }
  176. /**
  177. * Allow modules to allow or deny access to flagging for a single entity.
  178. *
  179. * Called when displaying a single entity view or edit page. For flag access
  180. * checks from within Views, implement hook_flag_access_multiple().
  181. *
  182. * @param $flag
  183. * The flag object.
  184. * @param $entity_id
  185. * The id of the entity in question.
  186. * @param $action
  187. * The action to test. Either 'flag' or 'unflag'.
  188. * @param $account
  189. * The user on whose behalf to test the flagging action.
  190. *
  191. * @return
  192. * One of the following values:
  193. * - TRUE: User has access to the flag.
  194. * - FALSE: User does not have access to the flag.
  195. * - NULL: This module does not perform checks on this flag/action.
  196. *
  197. * NOTE: Any module that returns FALSE will prevent the user from
  198. * being able to use the flag.
  199. *
  200. * @see hook_flag_access_multiple()
  201. * @see flag_flag:access()
  202. */
  203. function hook_flag_access($flag, $entity_id, $action, $account) {
  204. }
  205. /**
  206. * Allow modules to allow or deny access to flagging for multiple entities.
  207. *
  208. * Called when preparing a View or list of multiple flaggable entities.
  209. * For flag access checks for individual entities, see hook_flag_access().
  210. *
  211. * @param $flag
  212. * The flag object.
  213. * @param $entity_ids
  214. * An array of object ids to check access.
  215. * @param $account
  216. * The user on whose behalf to test the flagging action.
  217. *
  218. * @return
  219. * An array whose keys are the object IDs and values are booleans indicating
  220. * access.
  221. *
  222. * @see hook_flag_access()
  223. * @see flag_flag:access_multiple()
  224. */
  225. function hook_flag_access_multiple($flag, $entity_ids, $account) {
  226. }
  227. /**
  228. * Define one or more flag link types.
  229. *
  230. * Link types defined here must be returned by this module's hook_flag_link().
  231. *
  232. * This hook may be placed in a $module.flag.inc file.
  233. *
  234. * @return
  235. * An array of one or more types, keyed by the machine name of the type, and
  236. * where each value is a link type definition as an array with the following
  237. * properties:
  238. * - 'title': The human-readable name of the type.
  239. * - 'description': The description of the link type.
  240. * - 'options': An array of extra options for the link type.
  241. * - 'uses standard js': Boolean, indicates whether the link requires Flag
  242. * module's own JS file for links.
  243. * - 'uses standard css': Boolean, indicates whether the link requires Flag
  244. * module's own CSS file for links.
  245. * - 'provides form': (optional) Boolean indicating that this link type shows
  246. * the user a flagging entity form. This property is used in the UI, eg to
  247. * warn the admin user of link types that are not compatible with other
  248. * flag options. Defaults to FALSE.
  249. *
  250. * @see flag_get_link_types()
  251. * @see hook_flag_link_type_info_alter()
  252. */
  253. function hook_flag_link_type_info() {
  254. }
  255. /**
  256. * Alter other modules' definitions of flag link types.
  257. *
  258. * This hook may be placed in a $module.flag.inc file.
  259. *
  260. * @param $link_types
  261. * An array of the link types defined by all modules.
  262. *
  263. * @see flag_get_link_types()
  264. * @see hook_flag_link_type_info()
  265. */
  266. function hook_flag_link_type_info_alter(&$link_types) {
  267. }
  268. /**
  269. * Return the link for the link types this module defines.
  270. *
  271. * The type of link to be produced is given by $flag->link_type.
  272. *
  273. * When Flag uses a link type provided by this module, it will call this
  274. * implementation of hook_flag_link(). This should return a single link's
  275. * attributes, using the same structure as hook_link(). Note that "title" is
  276. * provided by the Flag configuration if not specified here.
  277. *
  278. * @param $flag
  279. * The full flag object for the flag link being generated.
  280. * @param $action
  281. * The action this link should perform. Either 'flag' or 'unflag'.
  282. * @param $entity_id
  283. * The ID of the node, comment, user, or other object being flagged. The type
  284. * of the object can be deduced from the flag type.
  285. *
  286. * @return
  287. * An array defining properties of the link.
  288. *
  289. * @see hook_flag_link_type_info()
  290. * @see template_preprocess_flag()
  291. */
  292. function hook_flag_link() {
  293. }
  294. /**
  295. * Act on flag deletion.
  296. *
  297. * This is invoked after all the flag database tables have had their relevant
  298. * entries deleted.
  299. *
  300. * @param $flag
  301. * The flag object that has been deleted.
  302. */
  303. function hook_flag_delete($flag) {
  304. }
  305. /**
  306. * Act when a flag is reset.
  307. *
  308. * @param $flag
  309. * The flag object.
  310. * @param $entity_id
  311. * The entity ID on which all flaggings are to be removed. May be NULL, in
  312. * which case all of this flag's entities are to be unflagged.
  313. * @param $rows
  314. * Database rows from the {flagging} table.
  315. *
  316. * @see flag_reset_flag()
  317. */
  318. function hook_flag_reset($flag, $entity_id, $rows) {
  319. }
  320. /**
  321. * Alter the javascript structure that describes the flag operation.
  322. *
  323. * @param $flag
  324. * The full flag object.
  325. * @param $entity_id
  326. * The ID of the node, comment, user or other object being flagged.
  327. *
  328. * @see flag_build_javascript_info()
  329. */
  330. function hook_flag_javascript_info_alter() {
  331. }
  332. /**
  333. * Alter a flag object that is being prepared for exporting.
  334. *
  335. * @param $flag
  336. * The flag object.
  337. *
  338. * @see flag_export_flags()
  339. */
  340. function hook_flag_export_alter($flag) {
  341. }