redirect.api.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Redirect module.
  5. */
  6. /**
  7. * @defgroup redirect_api_hooks Redirect API Hooks
  8. * @{
  9. * During redirect operations (create, update, view, delete, etc.), there are
  10. * several sets of hooks that get invoked to allow modules to modify the
  11. * redirect operation:
  12. * - All-module hooks: Generic hooks for "redirect" operations. These are
  13. * always invoked on all modules.
  14. * - Entity hooks: Generic hooks for "entity" operations. These are always
  15. * invoked on all modules.
  16. *
  17. * Here is a list of the redirect and entity hooks that are invoked, and other
  18. * steps that take place during redirect operations:
  19. * - Creating a new redirect (calling redirect_save() on a new redirect):
  20. * - hook_redirect_presave() (all)
  21. * - Redirect written to the database
  22. * - hook_redirect_insert() (all)
  23. * - hook_entity_insert() (all)
  24. * - Updating an existing redirect (calling redirect_save() on an existing redirect):
  25. * - hook_redirect_presave() (all)
  26. * - Redirect written to the database
  27. * - hook_redirect_update() (all)
  28. * - hook_entity_update() (all)
  29. * - Loading a redirect (calling redirect_load(), redirect_load_multiple(), or
  30. * entity_load() with $entity_type of 'redirect'):
  31. * - Redirect information is read from database.
  32. * - hook_entity_load() (all)
  33. * - hook_redirect_load() (all)
  34. * - Deleting a redirect (calling redirect_delete() or redirect_delete_multiple()):
  35. * - Redirect is loaded (see Loading section above)
  36. * - Redirect information is deleted from database
  37. * - hook_redirect_delete() (all)
  38. * - hook_entity_delete() (all)
  39. * - Preparing a redirect for editing (note that if it's
  40. * an existing redirect, it will already be loaded; see the Loading section
  41. * above):
  42. * - hook_redirect_prepare() (all)
  43. * - Validating a redirect during editing form submit (calling
  44. * redirect_form_validate()):
  45. * - hook_redirect_validate() (all)
  46. * @}
  47. */
  48. /**
  49. * @addtogroup hooks
  50. * @{
  51. */
  52. /**
  53. * Act on redirects being loaded from the database.
  54. *
  55. * This hook is invoked during redirect loading, which is handled by
  56. * entity_load(), via classes RedirectController and
  57. * DrupalDefaultEntityController. After the redirect information is read from
  58. * the database or the entity cache, hook_entity_load() is invoked on all
  59. * implementing modules, and then hook_redirect_load() is invoked on all
  60. * implementing modules.
  61. *
  62. * This hook should only be used to add information that is not in the redirect
  63. * table, not to replace information that is in that table (which could
  64. * interfere with the entity cache). For performance reasons, information for
  65. * all available redirects should be loaded in a single query where possible.
  66. *
  67. * The $types parameter allows for your module to have an early return (for
  68. * efficiency) if your module only supports certain redirect types.
  69. *
  70. * @param $redirects
  71. * An array of the redirects being loaded, keyed by rid.
  72. * @param $types
  73. * An array containing the types of the redirects.
  74. *
  75. * @ingroup redirect_api_hooks
  76. */
  77. function hook_redirect_load(array &$redirects, $types) {
  78. }
  79. /**
  80. * Alter the list of redirects matching a certain source.
  81. *
  82. * @param $redirects
  83. * An array of redirect objects.
  84. * @param $source
  85. * The source request path.
  86. * @param $context
  87. * An array with the following key/value pairs:
  88. * - language: The language code of the source request.
  89. * - query: An array of the source request query string.
  90. *
  91. * @see redirect_load_by_source()
  92. * @ingroup redirect_api_hooks
  93. */
  94. function hook_redirect_load_by_source_alter(array &$redirects, $source, array $context) {
  95. foreach ($redirects as $rid => $redirect) {
  96. if ($redirect->source !== $source) {
  97. // If the redirects to do not exactly match $source (e.g. case
  98. // insensitive matches), then remove them from the results.
  99. unset($redirects[$rid]);
  100. }
  101. }
  102. }
  103. /**
  104. * Control access to a redirect.
  105. *
  106. * Modules may implement this hook if they want to have a say in whether or not
  107. * a given user has access to perform a given operation on a redirect.
  108. *
  109. * The administrative account (user ID #1) always passes any access check,
  110. * so this hook is not called in that case. Users with the "administer redirects"
  111. * permission may always update and delete redirects through the administrative
  112. * interface.
  113. *
  114. * Note that not all modules will want to influence access on all
  115. * redirect types. If your module does not want to actively grant or
  116. * block access, return REDIRECT_ACCESS_IGNORE or simply return nothing.
  117. * Blindly returning FALSE will break other redirect access modules.
  118. *
  119. * @param $redirect
  120. * The redirect object on which the operation is to be performed, or, if it
  121. * does not yet exist, the type of redirect to be created.
  122. * @param $op
  123. * The operation to be performed. Possible values:
  124. * - "create"
  125. * - "delete"
  126. * - "update"
  127. * @param $account
  128. * A user object representing the user for whom the operation is to be
  129. * performed.
  130. *
  131. * @return
  132. * REDIRECT_ACCESS_ALLOW if the operation is to be allowed;
  133. * REDIRECT_ACCESS_DENY if the operation is to be denied;
  134. * REDIRECT_ACCESSS_IGNORE to not affect this operation at all.
  135. *
  136. * @see redirect_access()
  137. * @ingroup redirect_api_hooks
  138. */
  139. function hook_redirect_access($op, $redirect, $account) {
  140. $type = is_string($redirect) ? $redirect : $redirect->type;
  141. if (in_array($type, array('normal', 'special'))) {
  142. if ($op == 'create' && user_access('create ' . $type . ' redirects', $account)) {
  143. return REDIRECT_ACCESS_ALLOW;
  144. }
  145. if ($op == 'update') {
  146. if (user_access('edit any ' . $type . ' content', $account) || (user_access('edit own ' . $type . ' content', $account) && ($account->uid == $redirect->uid))) {
  147. return REDIRECT_ACCESS_ALLOW;
  148. }
  149. }
  150. if ($op == 'delete') {
  151. if (user_access('delete any ' . $type . ' content', $account) || (user_access('delete own ' . $type . ' content', $account) && ($account->uid == $redirect->uid))) {
  152. return REDIRECT_ACCESS_ALLOW;
  153. }
  154. }
  155. }
  156. // Returning nothing from this function would have the same effect.
  157. return REDIRECT_ACCESS_IGNORE;
  158. }
  159. /**
  160. * Act on a redirect object about to be shown on the add/edit form.
  161. *
  162. * This hook is invoked from redirect_object_prepare().
  163. *
  164. * @param $redirect
  165. * The redirect that is about to be shown on the add/edit form.
  166. *
  167. * @ingroup redirect_api_hooks
  168. */
  169. function hook_redirect_prepare($redirect) {
  170. }
  171. /**
  172. * Perform redirect validation before a redirect is created or updated.
  173. *
  174. * This hook is invoked from redirect_validate(), after a user has has finished
  175. * editing the redirect and is submitting it. It is invoked at the end of all
  176. * the standard validation steps.
  177. *
  178. * To indicate a validation error, use form_set_error().
  179. *
  180. * Note: Changes made to the $redirect object within your hook implementation
  181. * will have no effect. The preferred method to change a redirect's content is
  182. * to use hook_redirect_presave() instead. If it is really necessary to change
  183. * the redirect at the validate stage, you can use form_set_value().
  184. *
  185. * @param $redirect
  186. * The redirect being validated.
  187. * @param $form
  188. * The form being used to edit the redirect.
  189. * @param $form_state
  190. * The form state array.
  191. *
  192. * @see redirect_validate()
  193. * @ingroup redirect_api_hooks
  194. */
  195. function hook_redirect_validate($redirect, $form, $form_state) {
  196. }
  197. /**
  198. * Act on a redirect being inserted or updated.
  199. *
  200. * This hook is invoked from redirect_save() before the redirect is saved to
  201. * the database.
  202. *
  203. * @param $redirect
  204. * The redirect that is being inserted or updated.
  205. *
  206. * @see redirect_save()
  207. * @ingroup redirect_api_hooks
  208. */
  209. function hook_redirect_presave($redirect) {
  210. }
  211. /**
  212. * Respond to creation of a new redirect.
  213. *
  214. * This hook is invoked from redirect_save() after the redirect is inserted
  215. * into the redirect table in the database.
  216. *
  217. * @param $redirect
  218. * The redirect that is being created.
  219. *
  220. * @see redirect_save()
  221. * @ingroup redirect_api_hooks
  222. */
  223. function hook_redirect_insert($redirect) {
  224. }
  225. /**
  226. * Respond to updates to a redirect.
  227. *
  228. * This hook is invoked from redirect_save() after the redirect is updated in
  229. * the redirect table in the database.
  230. *
  231. * @param $redirect
  232. * The redirect that is being updated.
  233. *
  234. * @see redirect_save()
  235. * @ingroup redirect_api_hooks
  236. */
  237. function hook_redirect_update($redirect) {
  238. }
  239. /**
  240. * Respond to redirect deletion.
  241. *
  242. * This hook is invoked from redirect_delete_multiple() after the redirect has
  243. * been removed from the redirect table in the database.
  244. *
  245. * @param $redirect
  246. * The redirect that is being deleted.
  247. *
  248. * @see redirect_delete_multiple()
  249. * @ingroup redirect_api_hooks
  250. */
  251. function hook_redirect_delete($redirect) {
  252. }
  253. /**
  254. * Act on a redirect being redirected.
  255. *
  256. * This hook is invoked from redirect_redirect() before the redirect callback
  257. * is invoked.
  258. *
  259. * @param $redirect
  260. * The redirect that is being used for the redirect.
  261. *
  262. * @see redirect_redirect()
  263. * @see drupal_page_is_cacheable()
  264. * @ingroup redirect_api_hooks
  265. */
  266. function hook_redirect_alter($redirect) {
  267. }
  268. /**
  269. * @} End of "addtogroup hooks".
  270. */