-
Notifications
You must be signed in to change notification settings - Fork 377
/
redirect.api.php
304 lines (279 loc) · 9.05 KB
/
redirect.api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
/**
* @file
* Hooks provided by the Redirect module.
*/
/**
* @defgroup redirect_api_hooks Redirect API Hooks
* @{
* Redirects may be created by end-users to manually redirect from one URL on
* the site to another one (or to an external URL). For example the path
* "about" may be redirected to "pages/about-us" after a site redesign. The old
* URLs should be redirect to the new ones to maintain search engine rankings,
* existing links, and bookmarks to the old URL.
*
* Redirects are stored in the database in the "redirect" table. Loading and
* saving of redirects should be done through the Redirect API to ensure modules
* responding to the modification of redirects also have an opportunity to
* make adjustments as needed. The Redirect object is used to wrap around all
* the operations that can be done on redirects. It is recommended that
* redirects be loaded using one of the existing functions for loading, or
* if creating a new redirect, created by instantiating a new Redirect object.
*
* @see Redirect
* @see redirect_load()
* @see redirect_load_multiple()
* @see redirect_load_by_source()
*
* @}
*/
/**
* @addtogroup hooks
* @{
*/
/**
* Act on redirects being loaded from the database.
*
* This hook is invoked during redirect loading, which is handled by
* redirect_load_multiple(). After the redirect information is read from
* the database hook_redirect_load() is invoked on all implementing modules.
*
* This hook should only be used to add information that is not in the redirect
* table, not to replace information that is in that table (which could
* interfere with saving). For performance reasons, information for
* all available redirects should be loaded in a single query where possible.
*
* The $types parameter allows for your module to have an early return (for
* efficiency) if your module only supports certain redirect types.
*
* @param $redirects
* An array of the redirects being loaded, keyed by rid.
* @param $types
* An array containing the types of the redirects.
*
* @ingroup redirect_api_hooks
*/
function hook_redirect_load(array &$redirects, $types) {
}
/**
* Alter the list of redirects matching a certain source.
*
* @param $redirects
* An array of redirect objects.
* @param $source
* The source request path.
* @param $context
* An array with the following key/value pairs:
* - langcode: The language code of the source request.
* - query: An array of the source request query string.
*
* @see redirect_load_by_source()
* @ingroup redirect_api_hooks
*/
function hook_redirect_load_by_source_alter(array &$redirects, $source, array $context) {
foreach ($redirects as $rid => $redirect) {
if ($redirect->source !== $source) {
// If the redirects to do not exactly match $source (e.g. case
// insensitive matches), then remove them from the results.
unset($redirects[$rid]);
}
}
}
/**
* Control access to a redirect.
*
* Modules may implement this hook if they want to have a say in whether or not
* a given user has access to perform a given operation on a redirect.
*
* The administrative account (user ID #1) always passes any access check,
* so this hook is not called in that case. Users with the "administer redirects"
* permission may always update and delete redirects through the administrative
* interface.
*
* Note that not all modules will want to influence access on all
* redirect types. If your module does not want to actively grant or
* block access, return REDIRECT_ACCESS_IGNORE or simply return nothing.
* Blindly returning FALSE will break other redirect access modules.
*
* @param Redirect|string $redirect
* The redirect object on which the operation is to be performed, or, if it
* does not yet exist, the type of redirect to be created.
* @param $op
* The operation to be performed. Possible values:
* - "create"
* - "delete"
* - "update"
* @param $account
* A user object representing the user for whom the operation is to be
* performed.
*
* @return string|NULL
* REDIRECT_ACCESS_ALLOW if the operation is to be allowed;
* REDIRECT_ACCESS_DENY if the operation is to be denied;
* REDIRECT_ACCESS_IGNORE to not affect this operation at all.
*
* @see redirect_access()
* @ingroup redirect_api_hooks
*/
function hook_redirect_access($op, $redirect, $account) {
$type = is_string($redirect) ? $redirect : $redirect->type;
if (in_array($type, array('normal', 'special'))) {
if ($op == 'create' && user_access('create ' . $type . ' redirects', $account)) {
return REDIRECT_ACCESS_ALLOW;
}
if ($op == 'update') {
if (user_access('edit any ' . $type . ' content', $account) || (user_access('edit own ' . $type . ' content', $account) && ($account->uid == $redirect->uid))) {
return REDIRECT_ACCESS_ALLOW;
}
}
if ($op == 'delete') {
if (user_access('delete any ' . $type . ' content', $account) || (user_access('delete own ' . $type . ' content', $account) && ($account->uid == $redirect->uid))) {
return REDIRECT_ACCESS_ALLOW;
}
}
}
// Returning nothing from this function would have the same effect.
return REDIRECT_ACCESS_IGNORE;
}
/**
* Act on a redirect object about to be shown on the add/edit form.
*
* This hook is invoked from the Redirect object constructor in
* Redirect::__construct().
*
* @param Redirect $redirect
* A newly initialized redirect object. Likely to be shown on the form for
* adding or editing a redirect.
* @param array $values
* The default values for the Redirect from the database if editing an
* existing Redirect.
*
* @ingroup redirect_api_hooks
*/
function hook_redirect_prepare(Redirect $redirect, array $values) {
// Change the default type to be a 302 temporary redirect instead of a 301
// permanent redirect.
if (empty($values['type'])) {
$redirect->type = 302;
}
}
/**
* Perform redirect validation before a redirect is created or updated.
*
* This hook is invoked from redirect_validate(), after a user has has finished
* editing the redirect and is submitting it. It is invoked at the end of all
* the standard validation steps.
*
* To indicate a validation error, use form_set_error().
*
* Note: Changes made to the $redirect object within your hook implementation
* will have no effect. The preferred method to change a redirect's content is
* to use hook_redirect_presave() instead. If it is really necessary to change
* the redirect at the validate stage, you can use form_set_value().
*
* @param $redirect
* The redirect being validated.
* @param $form
* The form being used to edit the redirect.
* @param $form_state
* The form state array.
*
* @see redirect_validate()
* @ingroup redirect_api_hooks
*/
function hook_redirect_validate($redirect, $form, $form_state) {
}
/**
* Act on a redirect being inserted or updated.
*
* This hook is invoked from redirect_save() before the redirect is saved to
* the database.
*
* @param $redirect
* The redirect that is being inserted or updated.
*
* @see redirect_save()
* @ingroup redirect_api_hooks
*/
function hook_redirect_presave($redirect) {
}
/**
* Respond to creation of a new redirect.
*
* This hook is invoked from redirect_save() after the redirect is inserted
* into the redirect table in the database.
*
* @param $redirect
* The redirect that is being created.
*
* @see redirect_save()
* @ingroup redirect_api_hooks
*/
function hook_redirect_insert($redirect) {
}
/**
* Respond to updates to a redirect.
*
* This hook is invoked from redirect_save() after the redirect is updated in
* the redirect table in the database.
*
* @param $redirect
* The redirect that is being updated.
*
* @see redirect_save()
* @ingroup redirect_api_hooks
*/
function hook_redirect_update($redirect) {
}
/**
* Respond to redirect deletion.
*
* This hook is invoked from redirect_delete_multiple() after the redirect has
* been removed from the redirect table in the database.
*
* @param $redirect
* The redirect that is being deleted.
*
* @see redirect_delete_multiple()
* @ingroup redirect_api_hooks
*/
function hook_redirect_delete($redirect) {
}
/**
* Act on a redirect being redirected.
*
* This hook is invoked from redirect_redirect() before the redirect callback
* is invoked.
*
* @param $redirect
* The redirect that is being used for the redirect.
*
* @see redirect_redirect()
* @see backdrop_page_is_cacheable()
* @ingroup redirect_api_hooks
*/
function hook_redirect_alter($redirect) {
// Make any redirect going to the homepage considered temporary.
if ($redirect->redirect = '<front>') {
$redirect->type = 302;
}
}
/**
* Provide additional operations that may be done on redirects.
*
* @return array
* An array of callback information for executing the operation.
*/
function hook_redirect_operations() {
// Example from redirect_redirect_operations():
$operations['delete'] = array(
'action' => t('Delete'),
'action_past' => t('Deleted'),
'callback' => 'redirect_delete_multiple',
'confirm' => TRUE,
);
return $operations;
}
/**
* @} End of "addtogroup hooks".
*/