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