flag.api.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 array
  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 array $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. * Alter the definition of default flags.
  88. *
  89. * @param array &$flags
  90. * An array keyed by flag machine name containing flag definitions.
  91. */
  92. function hook_flag_default_flags_alter(&$flags) {
  93. if (!empty($flags['bookmark'])) {
  94. $flags['bookmark']['title'] = 'Bananana Bookmark';
  95. }
  96. }
  97. /**
  98. * Allow modules to alter a flag when it is initially loaded.
  99. *
  100. * @see flag_get_flags()
  101. */
  102. function hook_flag_alter(&$flag) {
  103. }
  104. /**
  105. * Alter a flag's default options.
  106. *
  107. * Modules that wish to extend flags and provide additional options must declare
  108. * them here so that their additions to the flag admin form are saved into the
  109. * flag object.
  110. *
  111. * @param array $options
  112. * The array of default options for the flag type, with the options for the
  113. * flag's link type merged in.
  114. * @param flag_flag $flag
  115. * The flag object.
  116. *
  117. * @see flag_flag::options()
  118. */
  119. function hook_flag_options_alter(&$options, $flag) {
  120. }
  121. /**
  122. * Act on an object being flagged.
  123. *
  124. * @param flag_flag $flag
  125. * The flag object.
  126. * @param int $entity_id
  127. * The id of the entity the flag is on.
  128. * @param $account
  129. * The user account performing the action.
  130. * @param $flagging_id
  131. * The flagging entity.
  132. */
  133. function hook_flag_flag($flag, $entity_id, $account, $flagging) {
  134. }
  135. /**
  136. * Act on an object being unflagged.
  137. *
  138. * This is invoked after the flag count has been decreased, but before the
  139. * flagging entity has been deleted.
  140. *
  141. * @param $flag
  142. * The flag object.
  143. * @param int $entity_id
  144. * The id of the entity the flag is on.
  145. * @param $account
  146. * The user account performing the action.
  147. * @param $flagging
  148. * The flagging entity.
  149. */
  150. function hook_flag_unflag($flag, $entity_id, $account, $flagging) {
  151. }
  152. /**
  153. * Perform custom validation on a flag before flagging/unflagging.
  154. *
  155. * @param string $action
  156. * The action about to be carried out. Either 'flag' or 'unflag'.
  157. * @param flag_flag $flag
  158. * The flag object.
  159. * @param int $entity_id
  160. * The id of the entity the user is trying to flag or unflag.
  161. * @param $account
  162. * The user account performing the action.
  163. * @param $flagging
  164. * The flagging entity.
  165. *
  166. * @return array|NULL
  167. * Optional array: textual error with the error-name as the key.
  168. * If the error name is 'access-denied' and javascript is disabled,
  169. * drupal_access_denied will be called and a 403 will be returned.
  170. * If validation is successful, do not return a value.
  171. */
  172. function hook_flag_validate($action, $flag, $entity_id, $account, $skip_permission_check, $flagging) {
  173. // We're only operating on the "test" flag, and users may always unflag.
  174. if ($flag->name == 'test' && $action == 'flag') {
  175. // Get all flags set by the current user.
  176. $flags = flag_get_user_flags('node', NULL, $account->uid, $sid = NULL, $reset = FALSE);
  177. // Check if this user has any flags of this type set.
  178. if (isset($flags['test'])) {
  179. $count = count($flags[$flag->name]);
  180. if ($count >= 2) {
  181. // Users may flag only 2 nodes with this flag.
  182. return (array('access-denied' => t('You may only flag 2 nodes with the test flag.')));
  183. }
  184. }
  185. }
  186. }
  187. /**
  188. * Allow modules to allow or deny access to flagging for a single entity.
  189. *
  190. * Called when displaying a single entity view or edit page. For flag access
  191. * checks from within Views, implement hook_flag_access_multiple().
  192. *
  193. * @param flag_flag $flag
  194. * The flag object.
  195. * @param $entity_id
  196. * The id of the entity in question.
  197. * @param string $action
  198. * The action to test. Either 'flag' or 'unflag'.
  199. * @param $account
  200. * The user on whose behalf to test the flagging action.
  201. *
  202. * @return
  203. * One of the following values:
  204. * - TRUE: User has access to the flag.
  205. * - FALSE: User does not have access to the flag.
  206. * - NULL: This module does not perform checks on this flag/action.
  207. *
  208. * NOTE: Any module that returns FALSE will prevent the user from
  209. * being able to use the flag.
  210. *
  211. * @see hook_flag_access_multiple()
  212. * @see flag_flag:access()
  213. */
  214. function hook_flag_access($flag, $entity_id, $action, $account) {
  215. }
  216. /**
  217. * Allow modules to allow or deny access to flagging for multiple entities.
  218. *
  219. * Called when preparing a View or list of multiple flaggable entities.
  220. * For flag access checks for individual entities, see hook_flag_access().
  221. *
  222. * @param flag_flag $flag
  223. * The flag object.
  224. * @param array $entity_ids
  225. * An array of object ids to check access.
  226. * @param $account
  227. * The user on whose behalf to test the flagging action.
  228. *
  229. * @return array
  230. * An array whose keys are the object IDs and values are booleans indicating
  231. * access: TRUE to grant access, FALSE to deny it, and NULL to leave the core
  232. * access unchanged. If the implementation does not wish to override any
  233. * access, an empty array may be returned.
  234. *
  235. * @see hook_flag_access()
  236. * @see flag_flag:access_multiple()
  237. */
  238. function hook_flag_access_multiple($flag, $entity_ids, $account) {
  239. }
  240. /**
  241. * Define one or more flag link types.
  242. *
  243. * Link types defined here must be returned by this module's hook_flag_link().
  244. *
  245. * This hook may be placed in a $module.flag.inc file.
  246. *
  247. * @return
  248. * An array of one or more types, keyed by the machine name of the type, and
  249. * where each value is a link type definition as an array with the following
  250. * properties:
  251. * - 'title': The human-readable name of the type.
  252. * - 'description': The description of the link type.
  253. * - 'options': An array of extra options for the link type.
  254. * - 'uses standard js': Boolean, indicates whether the link requires Flag
  255. * module's own JS file for links.
  256. * - 'uses standard css': Boolean, indicates whether the link requires Flag
  257. * module's own CSS file for links.
  258. * - 'provides form': (optional) Boolean indicating that this link type shows
  259. * the user a flagging entity form. This property is used in the UI, eg to
  260. * warn the admin user of link types that are not compatible with other
  261. * flag options. Defaults to FALSE.
  262. *
  263. * @see flag_get_link_types()
  264. * @see hook_flag_link_type_info_alter()
  265. */
  266. function hook_flag_link_type_info() {
  267. }
  268. /**
  269. * Alter other modules' definitions of flag link types.
  270. *
  271. * This hook may be placed in a $module.flag.inc file.
  272. *
  273. * @param array $link_types
  274. * An array of the link types defined by all modules.
  275. *
  276. * @see flag_get_link_types()
  277. * @see hook_flag_link_type_info()
  278. */
  279. function hook_flag_link_type_info_alter(&$link_types) {
  280. }
  281. /**
  282. * Return the link for the link types this module defines.
  283. *
  284. * The type of link to be produced is given by $flag->link_type.
  285. *
  286. * When Flag uses a link type provided by this module, it will call this
  287. * implementation of hook_flag_link(). This should return a single link's
  288. * attributes, using the same structure as hook_link(). Note that "title" is
  289. * provided by the Flag configuration if not specified here.
  290. *
  291. * @param flag_flag $flag
  292. * The full flag object for the flag link being generated.
  293. * @param string $action
  294. * The action this link should perform. Either 'flag' or 'unflag'.
  295. * @param int $entity_id
  296. * The ID of the node, comment, user, or other object being flagged. The type
  297. * of the object can be deduced from the flag type.
  298. *
  299. * @return
  300. * An array defining properties of the link.
  301. *
  302. * @see hook_flag_link_type_info()
  303. * @see template_preprocess_flag()
  304. */
  305. function hook_flag_link($flag, $action, $entity_id) {
  306. }
  307. /**
  308. * Act on flag deletion.
  309. *
  310. * This is invoked after all the flag database tables have had their relevant
  311. * entries deleted.
  312. *
  313. * @param flag_flag $flag
  314. * The flag object that has been deleted.
  315. */
  316. function hook_flag_delete($flag) {
  317. }
  318. /**
  319. * Act when a flag is reset.
  320. *
  321. * @param flag_flag $flag
  322. * The flag object.
  323. * @param int $entity_id
  324. * The entity ID on which all flaggings are to be removed. May be NULL, in
  325. * which case all of this flag's entities are to be unflagged.
  326. * @param $rows
  327. * Database rows from the {flagging} table.
  328. *
  329. * @see flag_reset_flag()
  330. */
  331. function hook_flag_reset($flag, $entity_id, $rows) {
  332. }
  333. /**
  334. * Alter the javascript structure that describes the flag operation.
  335. *
  336. * @param array $info
  337. * The info array before it is returned from flag_build_javascript_info().
  338. * @param flag_flag $flag
  339. * The full flag object.
  340. *
  341. * @see flag_build_javascript_info()
  342. */
  343. function hook_flag_javascript_info_alter(&$info, $flag) {
  344. if ($flag->name === 'test') {
  345. $info['newLink'] = $flag->theme($flag->is_flagged($info['contentId']) ? 'unflag' : 'flag', $info['contentId'], array(
  346. 'after_flagging' => TRUE,
  347. 'errors' => $flag->get_errors(),
  348. // Additional options to pass to theme's preprocess function/template.
  349. 'icon' => TRUE,
  350. 'hide_text' => TRUE,
  351. ));
  352. }
  353. }
  354. /**
  355. * Alter a flag object that is being prepared for exporting.
  356. *
  357. * @param flag_flag $flag
  358. * The flag object.
  359. *
  360. * @see flag_export_flags()
  361. */
  362. function hook_flag_export_alter($flag) {
  363. }