Mercurial > hg > rr-repo
comparison modules/node/node.api.php @ 0:ff03f76ab3fe
initial version
author | danieleb <danielebarchiesi@me.com> |
---|---|
date | Wed, 21 Aug 2013 18:51:11 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:ff03f76ab3fe |
---|---|
1 <?php | |
2 | |
3 /** | |
4 * @file | |
5 * Hooks provided by the Node module. | |
6 */ | |
7 | |
8 /** | |
9 * @defgroup node_api_hooks Node API Hooks | |
10 * @{ | |
11 * Functions to define and modify content types. | |
12 * | |
13 * Each content type is maintained by a primary module, which is either | |
14 * node.module (for content types created in the user interface) or the module | |
15 * that implements hook_node_info() to define the content type. | |
16 * | |
17 * During node operations (create, update, view, delete, etc.), there are | |
18 * several sets of hooks that get invoked to allow modules to modify the base | |
19 * node operation: | |
20 * - Node-type-specific hooks: These hooks are only invoked on the primary | |
21 * module, using the "base" return component of hook_node_info() as the | |
22 * function prefix. For example, poll.module defines the base for the Poll | |
23 * content type as "poll", so during creation of a poll node, hook_insert() is | |
24 * only invoked by calling poll_insert(). | |
25 * - All-module hooks: This set of hooks is invoked on all implementing modules, | |
26 * to allow other modules to modify what the primary node module is doing. For | |
27 * example, hook_node_insert() is invoked on all modules when creating a poll | |
28 * node. | |
29 * - Field hooks: Hooks related to the fields attached to the node. These are | |
30 * invoked from the field operations functions described below, and can be | |
31 * either field-type-specific or all-module hooks. | |
32 * - Entity hooks: Generic hooks for "entity" operations. These are always | |
33 * invoked on all modules. | |
34 * | |
35 * Here is a list of the node and entity hooks that are invoked, field | |
36 * operations, and other steps that take place during node operations: | |
37 * - Creating a new node (calling node_save() on a new node): | |
38 * - field_attach_presave() | |
39 * - hook_node_presave() (all) | |
40 * - hook_entity_presave() (all) | |
41 * - Node and revision records are written to the database | |
42 * - hook_insert() (node-type-specific) | |
43 * - field_attach_insert() | |
44 * - hook_node_insert() (all) | |
45 * - hook_entity_insert() (all) | |
46 * - hook_node_access_records() (all) | |
47 * - hook_node_access_records_alter() (all) | |
48 * - Updating an existing node (calling node_save() on an existing node): | |
49 * - field_attach_presave() | |
50 * - hook_node_presave() (all) | |
51 * - hook_entity_presave() (all) | |
52 * - Node and revision records are written to the database | |
53 * - hook_update() (node-type-specific) | |
54 * - field_attach_update() | |
55 * - hook_node_update() (all) | |
56 * - hook_entity_update() (all) | |
57 * - hook_node_access_records() (all) | |
58 * - hook_node_access_records_alter() (all) | |
59 * - Loading a node (calling node_load(), node_load_multiple() or entity_load() | |
60 * with $entity_type of 'node'): | |
61 * - Node and revision information is read from database. | |
62 * - hook_load() (node-type-specific) | |
63 * - field_attach_load_revision() and field_attach_load() | |
64 * - hook_entity_load() (all) | |
65 * - hook_node_load() (all) | |
66 * - Viewing a single node (calling node_view() - note that the input to | |
67 * node_view() is a loaded node, so the Loading steps above are already done): | |
68 * - hook_view() (node-type-specific) | |
69 * - field_attach_prepare_view() | |
70 * - hook_entity_prepare_view() (all) | |
71 * - field_attach_view() | |
72 * - hook_node_view() (all) | |
73 * - hook_entity_view() (all) | |
74 * - hook_node_view_alter() (all) | |
75 * - hook_entity_view_alter() (all) | |
76 * - Viewing multiple nodes (calling node_view_multiple() - note that the input | |
77 * to node_view_multiple() is a set of loaded nodes, so the Loading steps | |
78 * above are already done): | |
79 * - field_attach_prepare_view() | |
80 * - hook_entity_prepare_view() (all) | |
81 * - hook_view() (node-type-specific) | |
82 * - field_attach_view() | |
83 * - hook_node_view() (all) | |
84 * - hook_entity_view() (all) | |
85 * - hook_node_view_alter() (all) | |
86 * - hook_entity_view_alter() (all) | |
87 * - Deleting a node (calling node_delete() or node_delete_multiple()): | |
88 * - Node is loaded (see Loading section above) | |
89 * - hook_delete() (node-type-specific) | |
90 * - hook_node_delete() (all) | |
91 * - hook_entity_delete() (all) | |
92 * - field_attach_delete() | |
93 * - Node and revision information are deleted from database | |
94 * - Deleting a node revision (calling node_revision_delete()): | |
95 * - Node is loaded (see Loading section above) | |
96 * - Revision information is deleted from database | |
97 * - hook_node_revision_delete() (all) | |
98 * - field_attach_delete_revision() | |
99 * - Preparing a node for editing (calling node_form() - note that if it is an | |
100 * existing node, it will already be loaded; see the Loading section above): | |
101 * - hook_prepare() (node-type-specific) | |
102 * - hook_node_prepare() (all) | |
103 * - hook_form() (node-type-specific) | |
104 * - field_attach_form() | |
105 * - Validating a node during editing form submit (calling | |
106 * node_form_validate()): | |
107 * - hook_validate() (node-type-specific) | |
108 * - hook_node_validate() (all) | |
109 * - field_attach_form_validate() | |
110 * - Searching (calling node_search_execute()): | |
111 * - hook_ranking() (all) | |
112 * - Query is executed to find matching nodes | |
113 * - Resulting node is loaded (see Loading section above) | |
114 * - Resulting node is prepared for viewing (see Viewing a single node above) | |
115 * - comment_node_update_index() is called. | |
116 * - hook_node_search_result() (all) | |
117 * - Search indexing (calling node_update_index()): | |
118 * - Node is loaded (see Loading section above) | |
119 * - Node is prepared for viewing (see Viewing a single node above) | |
120 * - hook_node_update_index() (all) | |
121 * @} | |
122 */ | |
123 | |
124 /** | |
125 * @addtogroup hooks | |
126 * @{ | |
127 */ | |
128 | |
129 /** | |
130 * Inform the node access system what permissions the user has. | |
131 * | |
132 * This hook is for implementation by node access modules. In this hook, | |
133 * the module grants a user different "grant IDs" within one or more | |
134 * "realms". In hook_node_access_records(), the realms and grant IDs are | |
135 * associated with permission to view, edit, and delete individual nodes. | |
136 * | |
137 * The realms and grant IDs can be arbitrarily defined by your node access | |
138 * module; it is common to use role IDs as grant IDs, but that is not required. | |
139 * Your module could instead maintain its own list of users, where each list has | |
140 * an ID. In that case, the return value of this hook would be an array of the | |
141 * list IDs that this user is a member of. | |
142 * | |
143 * A node access module may implement as many realms as necessary to properly | |
144 * define the access privileges for the nodes. Note that the system makes no | |
145 * distinction between published and unpublished nodes. It is the module's | |
146 * responsibility to provide appropriate realms to limit access to unpublished | |
147 * content. | |
148 * | |
149 * Node access records are stored in the {node_access} table and define which | |
150 * grants are required to access a node. There is a special case for the view | |
151 * operation -- a record with node ID 0 corresponds to a "view all" grant for | |
152 * the realm and grant ID of that record. If there are no node access modules | |
153 * enabled, the core node module adds a node ID 0 record for realm 'all'. Node | |
154 * access modules can also grant "view all" permission on their custom realms; | |
155 * for example, a module could create a record in {node_access} with: | |
156 * @code | |
157 * $record = array( | |
158 * 'nid' => 0, | |
159 * 'gid' => 888, | |
160 * 'realm' => 'example_realm', | |
161 * 'grant_view' => 1, | |
162 * 'grant_update' => 0, | |
163 * 'grant_delete' => 0, | |
164 * ); | |
165 * drupal_write_record('node_access', $record); | |
166 * @endcode | |
167 * And then in its hook_node_grants() implementation, it would need to return: | |
168 * @code | |
169 * if ($op == 'view') { | |
170 * $grants['example_realm'] = array(888); | |
171 * } | |
172 * @endcode | |
173 * If you decide to do this, be aware that the node_access_rebuild() function | |
174 * will erase any node ID 0 entry when it is called, so you will need to make | |
175 * sure to restore your {node_access} record after node_access_rebuild() is | |
176 * called. | |
177 * | |
178 * @see node_access_view_all_nodes() | |
179 * @see node_access_rebuild() | |
180 * | |
181 * @param $account | |
182 * The user object whose grants are requested. | |
183 * @param $op | |
184 * The node operation to be performed, such as 'view', 'update', or 'delete'. | |
185 * | |
186 * @return | |
187 * An array whose keys are "realms" of grants, and whose values are arrays of | |
188 * the grant IDs within this realm that this user is being granted. | |
189 * | |
190 * For a detailed example, see node_access_example.module. | |
191 * | |
192 * @ingroup node_access | |
193 */ | |
194 function hook_node_grants($account, $op) { | |
195 if (user_access('access private content', $account)) { | |
196 $grants['example'] = array(1); | |
197 } | |
198 $grants['example_owner'] = array($account->uid); | |
199 return $grants; | |
200 } | |
201 | |
202 /** | |
203 * Set permissions for a node to be written to the database. | |
204 * | |
205 * When a node is saved, a module implementing hook_node_access_records() will | |
206 * be asked if it is interested in the access permissions for a node. If it is | |
207 * interested, it must respond with an array of permissions arrays for that | |
208 * node. | |
209 * | |
210 * Node access grants apply regardless of the published or unpublished status | |
211 * of the node. Implementations must make sure not to grant access to | |
212 * unpublished nodes if they don't want to change the standard access control | |
213 * behavior. Your module may need to create a separate access realm to handle | |
214 * access to unpublished nodes. | |
215 * | |
216 * Note that the grant values in the return value from your hook must be | |
217 * integers and not boolean TRUE and FALSE. | |
218 * | |
219 * Each permissions item in the array is an array with the following elements: | |
220 * - 'realm': The name of a realm that the module has defined in | |
221 * hook_node_grants(). | |
222 * - 'gid': A 'grant ID' from hook_node_grants(). | |
223 * - 'grant_view': If set to 1 a user that has been identified as a member | |
224 * of this gid within this realm can view this node. This should usually be | |
225 * set to $node->status. Failure to do so may expose unpublished content | |
226 * to some users. | |
227 * - 'grant_update': If set to 1 a user that has been identified as a member | |
228 * of this gid within this realm can edit this node. | |
229 * - 'grant_delete': If set to 1 a user that has been identified as a member | |
230 * of this gid within this realm can delete this node. | |
231 * - 'priority': If multiple modules seek to set permissions on a node, the | |
232 * realms that have the highest priority will win out, and realms with a lower | |
233 * priority will not be written. If there is any doubt, it is best to | |
234 * leave this 0. | |
235 * | |
236 * | |
237 * When an implementation is interested in a node but want to deny access to | |
238 * everyone, it may return a "deny all" grant: | |
239 * | |
240 * @code | |
241 * $grants[] = array( | |
242 * 'realm' => 'all', | |
243 * 'gid' => 0, | |
244 * 'grant_view' => 0, | |
245 * 'grant_update' => 0, | |
246 * 'grant_delete' => 0, | |
247 * 'priority' => 1, | |
248 * ); | |
249 * @endcode | |
250 * | |
251 * Setting the priority should cancel out other grants. In the case of a | |
252 * conflict between modules, it is safer to use hook_node_access_records_alter() | |
253 * to return only the deny grant. | |
254 * | |
255 * Note: a deny all grant is not written to the database; denies are implicit. | |
256 * | |
257 * @see node_access_write_grants() | |
258 * | |
259 * @param $node | |
260 * The node that has just been saved. | |
261 * | |
262 * @return | |
263 * An array of grants as defined above. | |
264 * | |
265 * @see hook_node_access_records_alter() | |
266 * @ingroup node_access | |
267 */ | |
268 function hook_node_access_records($node) { | |
269 // We only care about the node if it has been marked private. If not, it is | |
270 // treated just like any other node and we completely ignore it. | |
271 if ($node->private) { | |
272 $grants = array(); | |
273 // Only published nodes should be viewable to all users. If we allow access | |
274 // blindly here, then all users could view an unpublished node. | |
275 if ($node->status) { | |
276 $grants[] = array( | |
277 'realm' => 'example', | |
278 'gid' => 1, | |
279 'grant_view' => 1, | |
280 'grant_update' => 0, | |
281 'grant_delete' => 0, | |
282 'priority' => 0, | |
283 ); | |
284 } | |
285 // For the example_author array, the GID is equivalent to a UID, which | |
286 // means there are many groups of just 1 user. | |
287 // Note that an author can always view his or her nodes, even if they | |
288 // have status unpublished. | |
289 $grants[] = array( | |
290 'realm' => 'example_author', | |
291 'gid' => $node->uid, | |
292 'grant_view' => 1, | |
293 'grant_update' => 1, | |
294 'grant_delete' => 1, | |
295 'priority' => 0, | |
296 ); | |
297 | |
298 return $grants; | |
299 } | |
300 } | |
301 | |
302 /** | |
303 * Alter permissions for a node before it is written to the database. | |
304 * | |
305 * Node access modules establish rules for user access to content. Node access | |
306 * records are stored in the {node_access} table and define which permissions | |
307 * are required to access a node. This hook is invoked after node access modules | |
308 * returned their requirements via hook_node_access_records(); doing so allows | |
309 * modules to modify the $grants array by reference before it is stored, so | |
310 * custom or advanced business logic can be applied. | |
311 * | |
312 * @see hook_node_access_records() | |
313 * | |
314 * Upon viewing, editing or deleting a node, hook_node_grants() builds a | |
315 * permissions array that is compared against the stored access records. The | |
316 * user must have one or more matching permissions in order to complete the | |
317 * requested operation. | |
318 * | |
319 * A module may deny all access to a node by setting $grants to an empty array. | |
320 * | |
321 * @see hook_node_grants() | |
322 * @see hook_node_grants_alter() | |
323 * | |
324 * @param $grants | |
325 * The $grants array returned by hook_node_access_records(). | |
326 * @param $node | |
327 * The node for which the grants were acquired. | |
328 * | |
329 * The preferred use of this hook is in a module that bridges multiple node | |
330 * access modules with a configurable behavior, as shown in the example with the | |
331 * 'is_preview' field. | |
332 * | |
333 * @ingroup node_access | |
334 */ | |
335 function hook_node_access_records_alter(&$grants, $node) { | |
336 // Our module allows editors to mark specific articles with the 'is_preview' | |
337 // field. If the node being saved has a TRUE value for that field, then only | |
338 // our grants are retained, and other grants are removed. Doing so ensures | |
339 // that our rules are enforced no matter what priority other grants are given. | |
340 if ($node->is_preview) { | |
341 // Our module grants are set in $grants['example']. | |
342 $temp = $grants['example']; | |
343 // Now remove all module grants but our own. | |
344 $grants = array('example' => $temp); | |
345 } | |
346 } | |
347 | |
348 /** | |
349 * Alter user access rules when trying to view, edit or delete a node. | |
350 * | |
351 * Node access modules establish rules for user access to content. | |
352 * hook_node_grants() defines permissions for a user to view, edit or delete | |
353 * nodes by building a $grants array that indicates the permissions assigned to | |
354 * the user by each node access module. This hook is called to allow modules to | |
355 * modify the $grants array by reference, so the interaction of multiple node | |
356 * access modules can be altered or advanced business logic can be applied. | |
357 * | |
358 * @see hook_node_grants() | |
359 * | |
360 * The resulting grants are then checked against the records stored in the | |
361 * {node_access} table to determine if the operation may be completed. | |
362 * | |
363 * A module may deny all access to a user by setting $grants to an empty array. | |
364 * | |
365 * @see hook_node_access_records() | |
366 * @see hook_node_access_records_alter() | |
367 * | |
368 * @param $grants | |
369 * The $grants array returned by hook_node_grants(). | |
370 * @param $account | |
371 * The user account requesting access to content. | |
372 * @param $op | |
373 * The operation being performed, 'view', 'update' or 'delete'. | |
374 * | |
375 * Developers may use this hook to either add additional grants to a user or to | |
376 * remove existing grants. These rules are typically based on either the | |
377 * permissions assigned to a user role, or specific attributes of a user | |
378 * account. | |
379 * | |
380 * @ingroup node_access | |
381 */ | |
382 function hook_node_grants_alter(&$grants, $account, $op) { | |
383 // Our sample module never allows certain roles to edit or delete | |
384 // content. Since some other node access modules might allow this | |
385 // permission, we expressly remove it by returning an empty $grants | |
386 // array for roles specified in our variable setting. | |
387 | |
388 // Get our list of banned roles. | |
389 $restricted = variable_get('example_restricted_roles', array()); | |
390 | |
391 if ($op != 'view' && !empty($restricted)) { | |
392 // Now check the roles for this account against the restrictions. | |
393 foreach ($restricted as $role_id) { | |
394 if (isset($account->roles[$role_id])) { | |
395 $grants = array(); | |
396 } | |
397 } | |
398 } | |
399 } | |
400 | |
401 /** | |
402 * Add mass node operations. | |
403 * | |
404 * This hook enables modules to inject custom operations into the mass | |
405 * operations dropdown found at admin/content, by associating a callback | |
406 * function with the operation, which is called when the form is submitted. The | |
407 * callback function receives one initial argument, which is an array of the | |
408 * checked nodes. | |
409 * | |
410 * @return | |
411 * An array of operations. Each operation is an associative array that may | |
412 * contain the following key-value pairs: | |
413 * - label: (required) The label for the operation, displayed in the dropdown | |
414 * menu. | |
415 * - callback: (required) The function to call for the operation. | |
416 * - callback arguments: (optional) An array of additional arguments to pass | |
417 * to the callback function. | |
418 */ | |
419 function hook_node_operations() { | |
420 $operations = array( | |
421 'publish' => array( | |
422 'label' => t('Publish selected content'), | |
423 'callback' => 'node_mass_update', | |
424 'callback arguments' => array('updates' => array('status' => NODE_PUBLISHED)), | |
425 ), | |
426 'unpublish' => array( | |
427 'label' => t('Unpublish selected content'), | |
428 'callback' => 'node_mass_update', | |
429 'callback arguments' => array('updates' => array('status' => NODE_NOT_PUBLISHED)), | |
430 ), | |
431 'promote' => array( | |
432 'label' => t('Promote selected content to front page'), | |
433 'callback' => 'node_mass_update', | |
434 'callback arguments' => array('updates' => array('status' => NODE_PUBLISHED, 'promote' => NODE_PROMOTED)), | |
435 ), | |
436 'demote' => array( | |
437 'label' => t('Demote selected content from front page'), | |
438 'callback' => 'node_mass_update', | |
439 'callback arguments' => array('updates' => array('promote' => NODE_NOT_PROMOTED)), | |
440 ), | |
441 'sticky' => array( | |
442 'label' => t('Make selected content sticky'), | |
443 'callback' => 'node_mass_update', | |
444 'callback arguments' => array('updates' => array('status' => NODE_PUBLISHED, 'sticky' => NODE_STICKY)), | |
445 ), | |
446 'unsticky' => array( | |
447 'label' => t('Make selected content not sticky'), | |
448 'callback' => 'node_mass_update', | |
449 'callback arguments' => array('updates' => array('sticky' => NODE_NOT_STICKY)), | |
450 ), | |
451 'delete' => array( | |
452 'label' => t('Delete selected content'), | |
453 'callback' => NULL, | |
454 ), | |
455 ); | |
456 return $operations; | |
457 } | |
458 | |
459 /** | |
460 * Respond to node deletion. | |
461 * | |
462 * This hook is invoked from node_delete_multiple() after the type-specific | |
463 * hook_delete() has been invoked, but before hook_entity_delete and | |
464 * field_attach_delete() are called, and before the node is removed from the | |
465 * node table in the database. | |
466 * | |
467 * @param $node | |
468 * The node that is being deleted. | |
469 * | |
470 * @ingroup node_api_hooks | |
471 */ | |
472 function hook_node_delete($node) { | |
473 db_delete('mytable') | |
474 ->condition('nid', $node->nid) | |
475 ->execute(); | |
476 } | |
477 | |
478 /** | |
479 * Respond to deletion of a node revision. | |
480 * | |
481 * This hook is invoked from node_revision_delete() after the revision has been | |
482 * removed from the node_revision table, and before | |
483 * field_attach_delete_revision() is called. | |
484 * | |
485 * @param $node | |
486 * The node revision (node object) that is being deleted. | |
487 * | |
488 * @ingroup node_api_hooks | |
489 */ | |
490 function hook_node_revision_delete($node) { | |
491 db_delete('mytable') | |
492 ->condition('vid', $node->vid) | |
493 ->execute(); | |
494 } | |
495 | |
496 /** | |
497 * Respond to creation of a new node. | |
498 * | |
499 * This hook is invoked from node_save() after the database query that will | |
500 * insert the node into the node table is scheduled for execution, after the | |
501 * type-specific hook_insert() is invoked, and after field_attach_insert() is | |
502 * called. | |
503 * | |
504 * Note that when this hook is invoked, the changes have not yet been written to | |
505 * the database, because a database transaction is still in progress. The | |
506 * transaction is not finalized until the save operation is entirely completed | |
507 * and node_save() goes out of scope. You should not rely on data in the | |
508 * database at this time as it is not updated yet. You should also note that any | |
509 * write/update database queries executed from this hook are also not committed | |
510 * immediately. Check node_save() and db_transaction() for more info. | |
511 * | |
512 * @param $node | |
513 * The node that is being created. | |
514 * | |
515 * @ingroup node_api_hooks | |
516 */ | |
517 function hook_node_insert($node) { | |
518 db_insert('mytable') | |
519 ->fields(array( | |
520 'nid' => $node->nid, | |
521 'extra' => $node->extra, | |
522 )) | |
523 ->execute(); | |
524 } | |
525 | |
526 /** | |
527 * Act on arbitrary nodes being loaded from the database. | |
528 * | |
529 * This hook should be used to add information that is not in the node or node | |
530 * revisions table, not to replace information that is in these tables (which | |
531 * could interfere with the entity cache). For performance reasons, information | |
532 * for all available nodes should be loaded in a single query where possible. | |
533 * | |
534 * This hook is invoked during node loading, which is handled by entity_load(), | |
535 * via classes NodeController and DrupalDefaultEntityController. After the node | |
536 * information is read from the database or the entity cache, hook_load() is | |
537 * invoked on the node's content type module, then field_attach_load_revision() | |
538 * or field_attach_load() is called, then hook_entity_load() is invoked on all | |
539 * implementing modules, and finally hook_node_load() is invoked on all | |
540 * implementing modules. | |
541 * | |
542 * @param $nodes | |
543 * An array of the nodes being loaded, keyed by nid. | |
544 * @param $types | |
545 * An array containing the node types present in $nodes. Allows for an early | |
546 * return for modules that only support certain node types. However, if your | |
547 * module defines a content type, you can use hook_load() to respond to | |
548 * loading of just that content type. | |
549 * | |
550 * For a detailed usage example, see nodeapi_example.module. | |
551 * | |
552 * @ingroup node_api_hooks | |
553 */ | |
554 function hook_node_load($nodes, $types) { | |
555 // Decide whether any of $types are relevant to our purposes. | |
556 if (count(array_intersect($types_we_want_to_process, $types))) { | |
557 // Gather our extra data for each of these nodes. | |
558 $result = db_query('SELECT nid, foo FROM {mytable} WHERE nid IN(:nids)', array(':nids' => array_keys($nodes))); | |
559 // Add our extra data to the node objects. | |
560 foreach ($result as $record) { | |
561 $nodes[$record->nid]->foo = $record->foo; | |
562 } | |
563 } | |
564 } | |
565 | |
566 /** | |
567 * Control access to a node. | |
568 * | |
569 * Modules may implement this hook if they want to have a say in whether or not | |
570 * a given user has access to perform a given operation on a node. | |
571 * | |
572 * The administrative account (user ID #1) always passes any access check, so | |
573 * this hook is not called in that case. Users with the "bypass node access" | |
574 * permission may always view and edit content through the administrative | |
575 * interface. | |
576 * | |
577 * Note that not all modules will want to influence access on all node types. If | |
578 * your module does not want to actively grant or block access, return | |
579 * NODE_ACCESS_IGNORE or simply return nothing. Blindly returning FALSE will | |
580 * break other node access modules. | |
581 * | |
582 * Also note that this function isn't called for node listings (e.g., RSS feeds, | |
583 * the default home page at path 'node', a recent content block, etc.) See | |
584 * @link node_access Node access rights @endlink for a full explanation. | |
585 * | |
586 * @param $node | |
587 * Either a node object or the machine name of the content type on which to | |
588 * perform the access check. | |
589 * @param $op | |
590 * The operation to be performed. Possible values: | |
591 * - "create" | |
592 * - "delete" | |
593 * - "update" | |
594 * - "view" | |
595 * @param $account | |
596 * The user object to perform the access check operation on. | |
597 * | |
598 * @return | |
599 * - NODE_ACCESS_ALLOW: if the operation is to be allowed. | |
600 * - NODE_ACCESS_DENY: if the operation is to be denied. | |
601 * - NODE_ACCESS_IGNORE: to not affect this operation at all. | |
602 * | |
603 * @ingroup node_access | |
604 */ | |
605 function hook_node_access($node, $op, $account) { | |
606 $type = is_string($node) ? $node : $node->type; | |
607 | |
608 if (in_array($type, node_permissions_get_configured_types())) { | |
609 if ($op == 'create' && user_access('create ' . $type . ' content', $account)) { | |
610 return NODE_ACCESS_ALLOW; | |
611 } | |
612 | |
613 if ($op == 'update') { | |
614 if (user_access('edit any ' . $type . ' content', $account) || (user_access('edit own ' . $type . ' content', $account) && ($account->uid == $node->uid))) { | |
615 return NODE_ACCESS_ALLOW; | |
616 } | |
617 } | |
618 | |
619 if ($op == 'delete') { | |
620 if (user_access('delete any ' . $type . ' content', $account) || (user_access('delete own ' . $type . ' content', $account) && ($account->uid == $node->uid))) { | |
621 return NODE_ACCESS_ALLOW; | |
622 } | |
623 } | |
624 } | |
625 | |
626 // Returning nothing from this function would have the same effect. | |
627 return NODE_ACCESS_IGNORE; | |
628 } | |
629 | |
630 | |
631 /** | |
632 * Act on a node object about to be shown on the add/edit form. | |
633 * | |
634 * This hook is invoked from node_object_prepare() after the type-specific | |
635 * hook_prepare() is invoked. | |
636 * | |
637 * @param $node | |
638 * The node that is about to be shown on the add/edit form. | |
639 * | |
640 * @ingroup node_api_hooks | |
641 */ | |
642 function hook_node_prepare($node) { | |
643 if (!isset($node->comment)) { | |
644 $node->comment = variable_get("comment_$node->type", COMMENT_NODE_OPEN); | |
645 } | |
646 } | |
647 | |
648 /** | |
649 * Act on a node being displayed as a search result. | |
650 * | |
651 * This hook is invoked from node_search_execute(), after node_load() and | |
652 * node_view() have been called. | |
653 * | |
654 * @param $node | |
655 * The node being displayed in a search result. | |
656 * | |
657 * @return array | |
658 * Extra information to be displayed with search result. This information | |
659 * should be presented as an associative array. It will be concatenated with | |
660 * the post information (last updated, author) in the default search result | |
661 * theming. | |
662 * | |
663 * @see template_preprocess_search_result() | |
664 * @see search-result.tpl.php | |
665 * | |
666 * @ingroup node_api_hooks | |
667 */ | |
668 function hook_node_search_result($node) { | |
669 $comments = db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = :nid', array('nid' => $node->nid))->fetchField(); | |
670 return array('comment' => format_plural($comments, '1 comment', '@count comments')); | |
671 } | |
672 | |
673 /** | |
674 * Act on a node being inserted or updated. | |
675 * | |
676 * This hook is invoked from node_save() before the node is saved to the | |
677 * database. | |
678 * | |
679 * @param $node | |
680 * The node that is being inserted or updated. | |
681 * | |
682 * @ingroup node_api_hooks | |
683 */ | |
684 function hook_node_presave($node) { | |
685 if ($node->nid && $node->moderate) { | |
686 // Reset votes when node is updated: | |
687 $node->score = 0; | |
688 $node->users = ''; | |
689 $node->votes = 0; | |
690 } | |
691 } | |
692 | |
693 /** | |
694 * Respond to updates to a node. | |
695 * | |
696 * This hook is invoked from node_save() after the database query that will | |
697 * update node in the node table is scheduled for execution, after the | |
698 * type-specific hook_update() is invoked, and after field_attach_update() is | |
699 * called. | |
700 * | |
701 * Note that when this hook is invoked, the changes have not yet been written to | |
702 * the database, because a database transaction is still in progress. The | |
703 * transaction is not finalized until the save operation is entirely completed | |
704 * and node_save() goes out of scope. You should not rely on data in the | |
705 * database at this time as it is not updated yet. You should also note that any | |
706 * write/update database queries executed from this hook are also not committed | |
707 * immediately. Check node_save() and db_transaction() for more info. | |
708 * | |
709 * @param $node | |
710 * The node that is being updated. | |
711 * | |
712 * @ingroup node_api_hooks | |
713 */ | |
714 function hook_node_update($node) { | |
715 db_update('mytable') | |
716 ->fields(array('extra' => $node->extra)) | |
717 ->condition('nid', $node->nid) | |
718 ->execute(); | |
719 } | |
720 | |
721 /** | |
722 * Act on a node being indexed for searching. | |
723 * | |
724 * This hook is invoked during search indexing, after node_load(), and after the | |
725 * result of node_view() is added as $node->rendered to the node object. | |
726 * | |
727 * @param $node | |
728 * The node being indexed. | |
729 * | |
730 * @return string | |
731 * Additional node information to be indexed. | |
732 * | |
733 * @ingroup node_api_hooks | |
734 */ | |
735 function hook_node_update_index($node) { | |
736 $text = ''; | |
737 $comments = db_query('SELECT subject, comment, format FROM {comment} WHERE nid = :nid AND status = :status', array(':nid' => $node->nid, ':status' => COMMENT_PUBLISHED)); | |
738 foreach ($comments as $comment) { | |
739 $text .= '<h2>' . check_plain($comment->subject) . '</h2>' . check_markup($comment->comment, $comment->format, '', TRUE); | |
740 } | |
741 return $text; | |
742 } | |
743 | |
744 /** | |
745 * Perform node validation before a node is created or updated. | |
746 * | |
747 * This hook is invoked from node_validate(), after a user has has finished | |
748 * editing the node and is previewing or submitting it. It is invoked at the | |
749 * end of all the standard validation steps, and after the type-specific | |
750 * hook_validate() is invoked. | |
751 * | |
752 * To indicate a validation error, use form_set_error(). | |
753 * | |
754 * Note: Changes made to the $node object within your hook implementation will | |
755 * have no effect. The preferred method to change a node's content is to use | |
756 * hook_node_presave() instead. If it is really necessary to change the node at | |
757 * the validate stage, you can use form_set_value(). | |
758 * | |
759 * @param $node | |
760 * The node being validated. | |
761 * @param $form | |
762 * The form being used to edit the node. | |
763 * @param $form_state | |
764 * The form state array. | |
765 * | |
766 * @ingroup node_api_hooks | |
767 */ | |
768 function hook_node_validate($node, $form, &$form_state) { | |
769 if (isset($node->end) && isset($node->start)) { | |
770 if ($node->start > $node->end) { | |
771 form_set_error('time', t('An event may not end before it starts.')); | |
772 } | |
773 } | |
774 } | |
775 | |
776 /** | |
777 * Act on a node after validated form values have been copied to it. | |
778 * | |
779 * This hook is invoked when a node form is submitted with either the "Save" or | |
780 * "Preview" button, after form values have been copied to the form state's node | |
781 * object, but before the node is saved or previewed. It is a chance for modules | |
782 * to adjust the node's properties from what they are simply after a copy from | |
783 * $form_state['values']. This hook is intended for adjusting non-field-related | |
784 * properties. See hook_field_attach_submit() for customizing field-related | |
785 * properties. | |
786 * | |
787 * @param $node | |
788 * The node object being updated in response to a form submission. | |
789 * @param $form | |
790 * The form being used to edit the node. | |
791 * @param $form_state | |
792 * The form state array. | |
793 * | |
794 * @ingroup node_api_hooks | |
795 */ | |
796 function hook_node_submit($node, $form, &$form_state) { | |
797 // Decompose the selected menu parent option into 'menu_name' and 'plid', if | |
798 // the form used the default parent selection widget. | |
799 if (!empty($form_state['values']['menu']['parent'])) { | |
800 list($node->menu['menu_name'], $node->menu['plid']) = explode(':', $form_state['values']['menu']['parent']); | |
801 } | |
802 } | |
803 | |
804 /** | |
805 * Act on a node that is being assembled before rendering. | |
806 * | |
807 * The module may add elements to $node->content prior to rendering. This hook | |
808 * will be called after hook_view(). The structure of $node->content is a | |
809 * renderable array as expected by drupal_render(). | |
810 * | |
811 * When $view_mode is 'rss', modules can also add extra RSS elements and | |
812 * namespaces to $node->rss_elements and $node->rss_namespaces respectively for | |
813 * the RSS item generated for this node. | |
814 * For details on how this is used, see node_feed(). | |
815 * | |
816 * @see blog_node_view() | |
817 * @see forum_node_view() | |
818 * @see comment_node_view() | |
819 * | |
820 * @param $node | |
821 * The node that is being assembled for rendering. | |
822 * @param $view_mode | |
823 * The $view_mode parameter from node_view(). | |
824 * @param $langcode | |
825 * The language code used for rendering. | |
826 * | |
827 * @see hook_entity_view() | |
828 * | |
829 * @ingroup node_api_hooks | |
830 */ | |
831 function hook_node_view($node, $view_mode, $langcode) { | |
832 $node->content['my_additional_field'] = array( | |
833 '#markup' => $additional_field, | |
834 '#weight' => 10, | |
835 '#theme' => 'mymodule_my_additional_field', | |
836 ); | |
837 } | |
838 | |
839 /** | |
840 * Alter the results of node_view(). | |
841 * | |
842 * This hook is called after the content has been assembled in a structured | |
843 * array and may be used for doing processing which requires that the complete | |
844 * node content structure has been built. | |
845 * | |
846 * If the module wishes to act on the rendered HTML of the node rather than the | |
847 * structured content array, it may use this hook to add a #post_render | |
848 * callback. Alternatively, it could also implement hook_preprocess_node(). See | |
849 * drupal_render() and theme() documentation respectively for details. | |
850 * | |
851 * @param $build | |
852 * A renderable array representing the node content. | |
853 * | |
854 * @see node_view() | |
855 * @see hook_entity_view_alter() | |
856 * | |
857 * @ingroup node_api_hooks | |
858 */ | |
859 function hook_node_view_alter(&$build) { | |
860 if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) { | |
861 // Change its weight. | |
862 $build['an_additional_field']['#weight'] = -10; | |
863 } | |
864 | |
865 // Add a #post_render callback to act on the rendered HTML of the node. | |
866 $build['#post_render'][] = 'my_module_node_post_render'; | |
867 } | |
868 | |
869 /** | |
870 * Define module-provided node types. | |
871 * | |
872 * This hook allows a module to define one or more of its own node types. For | |
873 * example, the blog module uses it to define a blog node-type named "Blog | |
874 * entry." The name and attributes of each desired node type are specified in an | |
875 * array returned by the hook. | |
876 * | |
877 * Only module-provided node types should be defined through this hook. User- | |
878 * provided (or 'custom') node types should be defined only in the 'node_type' | |
879 * database table, and should be maintained by using the node_type_save() and | |
880 * node_type_delete() functions. | |
881 * | |
882 * @return | |
883 * An array of information defining the module's node types. The array | |
884 * contains a sub-array for each node type, with the machine-readable type | |
885 * name as the key. Each sub-array has up to 10 attributes. Possible | |
886 * attributes: | |
887 * - name: (required) The human-readable name of the node type. | |
888 * - base: (required) The base string used to construct callbacks | |
889 * corresponding to this node type (for example, if base is defined as | |
890 * example_foo, then example_foo_insert will be called when inserting a node | |
891 * of that type). This string is usually the name of the module, but not | |
892 * always. | |
893 * - description: (required) A brief description of the node type. | |
894 * - help: (optional) Help information shown to the user when creating a node | |
895 * of this type. | |
896 * - has_title: (optional) A Boolean indicating whether or not this node type | |
897 * has a title field. | |
898 * - title_label: (optional) The label for the title field of this content | |
899 * type. | |
900 * - locked: (optional) A Boolean indicating whether the administrator can | |
901 * change the machine name of this type. FALSE = changeable (not locked), | |
902 * TRUE = unchangeable (locked). | |
903 * | |
904 * The machine name of a node type should contain only letters, numbers, and | |
905 * underscores. Underscores will be converted into hyphens for the purpose of | |
906 * constructing URLs. | |
907 * | |
908 * All attributes of a node type that are defined through this hook (except for | |
909 * 'locked') can be edited by a site administrator. This includes the | |
910 * machine-readable name of a node type, if 'locked' is set to FALSE. | |
911 * | |
912 * @ingroup node_api_hooks | |
913 */ | |
914 function hook_node_info() { | |
915 return array( | |
916 'blog' => array( | |
917 'name' => t('Blog entry'), | |
918 'base' => 'blog', | |
919 'description' => t('Use for multi-user blogs. Every user gets a personal blog.'), | |
920 ) | |
921 ); | |
922 } | |
923 | |
924 /** | |
925 * Provide additional methods of scoring for core search results for nodes. | |
926 * | |
927 * A node's search score is used to rank it among other nodes matched by the | |
928 * search, with the highest-ranked nodes appearing first in the search listing. | |
929 * | |
930 * For example, a module allowing users to vote on content could expose an | |
931 * option to allow search results' rankings to be influenced by the average | |
932 * voting score of a node. | |
933 * | |
934 * All scoring mechanisms are provided as options to site administrators, and | |
935 * may be tweaked based on individual sites or disabled altogether if they do | |
936 * not make sense. Individual scoring mechanisms, if enabled, are assigned a | |
937 * weight from 1 to 10. The weight represents the factor of magnification of | |
938 * the ranking mechanism, with higher-weighted ranking mechanisms having more | |
939 * influence. In order for the weight system to work, each scoring mechanism | |
940 * must return a value between 0 and 1 for every node. That value is then | |
941 * multiplied by the administrator-assigned weight for the ranking mechanism, | |
942 * and then the weighted scores from all ranking mechanisms are added, which | |
943 * brings about the same result as a weighted average. | |
944 * | |
945 * @return | |
946 * An associative array of ranking data. The keys should be strings, | |
947 * corresponding to the internal name of the ranking mechanism, such as | |
948 * 'recent', or 'comments'. The values should be arrays themselves, with the | |
949 * following keys available: | |
950 * - title: (required) The human readable name of the ranking mechanism. | |
951 * - join: (optional) The part of a query string to join to any additional | |
952 * necessary table. This is not necessary if the table required is already | |
953 * joined to by the base query, such as for the {node} table. Other tables | |
954 * should use the full table name as an alias to avoid naming collisions. | |
955 * - score: (required) The part of a query string to calculate the score for | |
956 * the ranking mechanism based on values in the database. This does not need | |
957 * to be wrapped in parentheses, as it will be done automatically; it also | |
958 * does not need to take the weighted system into account, as it will be | |
959 * done automatically. It does, however, need to calculate a decimal between | |
960 * 0 and 1; be careful not to cast the entire score to an integer by | |
961 * inadvertently introducing a variable argument. | |
962 * - arguments: (optional) If any arguments are required for the score, they | |
963 * can be specified in an array here. | |
964 * | |
965 * @ingroup node_api_hooks | |
966 */ | |
967 function hook_ranking() { | |
968 // If voting is disabled, we can avoid returning the array, no hard feelings. | |
969 if (variable_get('vote_node_enabled', TRUE)) { | |
970 return array( | |
971 'vote_average' => array( | |
972 'title' => t('Average vote'), | |
973 // Note that we use i.sid, the search index's search item id, rather than | |
974 // n.nid. | |
975 'join' => 'LEFT JOIN {vote_node_data} vote_node_data ON vote_node_data.nid = i.sid', | |
976 // The highest possible score should be 1, and the lowest possible score, | |
977 // always 0, should be 0. | |
978 'score' => 'vote_node_data.average / CAST(%f AS DECIMAL)', | |
979 // Pass in the highest possible voting score as a decimal argument. | |
980 'arguments' => array(variable_get('vote_score_max', 5)), | |
981 ), | |
982 ); | |
983 } | |
984 } | |
985 | |
986 | |
987 /** | |
988 * Respond to node type creation. | |
989 * | |
990 * This hook is invoked from node_type_save() after the node type is added to | |
991 * the database. | |
992 * | |
993 * @param $info | |
994 * The node type object that is being created. | |
995 */ | |
996 function hook_node_type_insert($info) { | |
997 drupal_set_message(t('You have just created a content type with a machine name %type.', array('%type' => $info->type))); | |
998 } | |
999 | |
1000 /** | |
1001 * Respond to node type updates. | |
1002 * | |
1003 * This hook is invoked from node_type_save() after the node type is updated in | |
1004 * the database. | |
1005 * | |
1006 * @param $info | |
1007 * The node type object that is being updated. | |
1008 */ | |
1009 function hook_node_type_update($info) { | |
1010 if (!empty($info->old_type) && $info->old_type != $info->type) { | |
1011 $setting = variable_get('comment_' . $info->old_type, COMMENT_NODE_OPEN); | |
1012 variable_del('comment_' . $info->old_type); | |
1013 variable_set('comment_' . $info->type, $setting); | |
1014 } | |
1015 } | |
1016 | |
1017 /** | |
1018 * Respond to node type deletion. | |
1019 * | |
1020 * This hook is invoked from node_type_delete() after the node type is removed | |
1021 * from the database. | |
1022 * | |
1023 * @param $info | |
1024 * The node type object that is being deleted. | |
1025 */ | |
1026 function hook_node_type_delete($info) { | |
1027 variable_del('comment_' . $info->type); | |
1028 } | |
1029 | |
1030 /** | |
1031 * Respond to node deletion. | |
1032 * | |
1033 * This hook is invoked only on the module that defines the node's content type | |
1034 * (use hook_node_delete() to respond to all node deletions). | |
1035 * | |
1036 * This hook is invoked from node_delete_multiple() after the node has been | |
1037 * removed from the node table in the database, before hook_node_delete() is | |
1038 * invoked, and before field_attach_delete() is called. | |
1039 * | |
1040 * @param $node | |
1041 * The node that is being deleted. | |
1042 * | |
1043 * @ingroup node_api_hooks | |
1044 */ | |
1045 function hook_delete($node) { | |
1046 db_delete('mytable') | |
1047 ->condition('nid', $node->nid) | |
1048 ->execute(); | |
1049 } | |
1050 | |
1051 /** | |
1052 * Act on a node object about to be shown on the add/edit form. | |
1053 * | |
1054 * This hook is invoked only on the module that defines the node's content type | |
1055 * (use hook_node_prepare() to act on all node preparations). | |
1056 * | |
1057 * This hook is invoked from node_object_prepare() before the general | |
1058 * hook_node_prepare() is invoked. | |
1059 * | |
1060 * @param $node | |
1061 * The node that is about to be shown on the add/edit form. | |
1062 * | |
1063 * @ingroup node_api_hooks | |
1064 */ | |
1065 function hook_prepare($node) { | |
1066 if ($file = file_check_upload($field_name)) { | |
1067 $file = file_save_upload($field_name, _image_filename($file->filename, NULL, TRUE)); | |
1068 if ($file) { | |
1069 if (!image_get_info($file->uri)) { | |
1070 form_set_error($field_name, t('Uploaded file is not a valid image')); | |
1071 return; | |
1072 } | |
1073 } | |
1074 else { | |
1075 return; | |
1076 } | |
1077 $node->images['_original'] = $file->uri; | |
1078 _image_build_derivatives($node, TRUE); | |
1079 $node->new_file = TRUE; | |
1080 } | |
1081 } | |
1082 | |
1083 /** | |
1084 * Display a node editing form. | |
1085 * | |
1086 * This hook, implemented by node modules, is called to retrieve the form | |
1087 * that is displayed to create or edit a node. This form is displayed at path | |
1088 * node/add/[node type] or node/[node ID]/edit. | |
1089 * | |
1090 * The submit and preview buttons, administrative and display controls, and | |
1091 * sections added by other modules (such as path settings, menu settings, | |
1092 * comment settings, and fields managed by the Field UI module) are | |
1093 * displayed automatically by the node module. This hook just needs to | |
1094 * return the node title and form editing fields specific to the node type. | |
1095 * | |
1096 * @param $node | |
1097 * The node being added or edited. | |
1098 * @param $form_state | |
1099 * The form state array. | |
1100 * | |
1101 * @return | |
1102 * An array containing the title and any custom form elements to be displayed | |
1103 * in the node editing form. | |
1104 * | |
1105 * @ingroup node_api_hooks | |
1106 */ | |
1107 function hook_form($node, &$form_state) { | |
1108 $type = node_type_get_type($node); | |
1109 | |
1110 $form['title'] = array( | |
1111 '#type' => 'textfield', | |
1112 '#title' => check_plain($type->title_label), | |
1113 '#default_value' => !empty($node->title) ? $node->title : '', | |
1114 '#required' => TRUE, '#weight' => -5 | |
1115 ); | |
1116 | |
1117 $form['field1'] = array( | |
1118 '#type' => 'textfield', | |
1119 '#title' => t('Custom field'), | |
1120 '#default_value' => $node->field1, | |
1121 '#maxlength' => 127, | |
1122 ); | |
1123 $form['selectbox'] = array( | |
1124 '#type' => 'select', | |
1125 '#title' => t('Select box'), | |
1126 '#default_value' => $node->selectbox, | |
1127 '#options' => array( | |
1128 1 => 'Option A', | |
1129 2 => 'Option B', | |
1130 3 => 'Option C', | |
1131 ), | |
1132 '#description' => t('Choose an option.'), | |
1133 ); | |
1134 | |
1135 return $form; | |
1136 } | |
1137 | |
1138 /** | |
1139 * Respond to creation of a new node. | |
1140 * | |
1141 * This hook is invoked only on the module that defines the node's content type | |
1142 * (use hook_node_insert() to act on all node insertions). | |
1143 * | |
1144 * This hook is invoked from node_save() after the node is inserted into the | |
1145 * node table in the database, before field_attach_insert() is called, and | |
1146 * before hook_node_insert() is invoked. | |
1147 * | |
1148 * @param $node | |
1149 * The node that is being created. | |
1150 * | |
1151 * @ingroup node_api_hooks | |
1152 */ | |
1153 function hook_insert($node) { | |
1154 db_insert('mytable') | |
1155 ->fields(array( | |
1156 'nid' => $node->nid, | |
1157 'extra' => $node->extra, | |
1158 )) | |
1159 ->execute(); | |
1160 } | |
1161 | |
1162 /** | |
1163 * Act on nodes being loaded from the database. | |
1164 * | |
1165 * This hook is invoked only on the module that defines the node's content type | |
1166 * (use hook_node_load() to respond to all node loads). | |
1167 * | |
1168 * This hook is invoked during node loading, which is handled by entity_load(), | |
1169 * via classes NodeController and DrupalDefaultEntityController. After the node | |
1170 * information is read from the database or the entity cache, hook_load() is | |
1171 * invoked on the node's content type module, then field_attach_node_revision() | |
1172 * or field_attach_load() is called, then hook_entity_load() is invoked on all | |
1173 * implementing modules, and finally hook_node_load() is invoked on all | |
1174 * implementing modules. | |
1175 * | |
1176 * This hook should only be used to add information that is not in the node or | |
1177 * node revisions table, not to replace information that is in these tables | |
1178 * (which could interfere with the entity cache). For performance reasons, | |
1179 * information for all available nodes should be loaded in a single query where | |
1180 * possible. | |
1181 * | |
1182 * @param $nodes | |
1183 * An array of the nodes being loaded, keyed by nid. | |
1184 * | |
1185 * For a detailed usage example, see node_example.module. | |
1186 * | |
1187 * @ingroup node_api_hooks | |
1188 */ | |
1189 function hook_load($nodes) { | |
1190 $result = db_query('SELECT nid, foo FROM {mytable} WHERE nid IN (:nids)', array(':nids' => array_keys($nodes))); | |
1191 foreach ($result as $record) { | |
1192 $nodes[$record->nid]->foo = $record->foo; | |
1193 } | |
1194 } | |
1195 | |
1196 /** | |
1197 * Respond to updates to a node. | |
1198 * | |
1199 * This hook is invoked only on the module that defines the node's content type | |
1200 * (use hook_node_update() to act on all node updates). | |
1201 * | |
1202 * This hook is invoked from node_save() after the node is updated in the | |
1203 * node table in the database, before field_attach_update() is called, and | |
1204 * before hook_node_update() is invoked. | |
1205 * | |
1206 * @param $node | |
1207 * The node that is being updated. | |
1208 * | |
1209 * @ingroup node_api_hooks | |
1210 */ | |
1211 function hook_update($node) { | |
1212 db_update('mytable') | |
1213 ->fields(array('extra' => $node->extra)) | |
1214 ->condition('nid', $node->nid) | |
1215 ->execute(); | |
1216 } | |
1217 | |
1218 /** | |
1219 * Perform node validation before a node is created or updated. | |
1220 * | |
1221 * This hook is invoked only on the module that defines the node's content type | |
1222 * (use hook_node_validate() to act on all node validations). | |
1223 * | |
1224 * This hook is invoked from node_validate(), after a user has finished | |
1225 * editing the node and is previewing or submitting it. It is invoked at the end | |
1226 * of all the standard validation steps, and before hook_node_validate() is | |
1227 * invoked. | |
1228 * | |
1229 * To indicate a validation error, use form_set_error(). | |
1230 * | |
1231 * Note: Changes made to the $node object within your hook implementation will | |
1232 * have no effect. The preferred method to change a node's content is to use | |
1233 * hook_node_presave() instead. | |
1234 * | |
1235 * @param $node | |
1236 * The node being validated. | |
1237 * @param $form | |
1238 * The form being used to edit the node. | |
1239 * @param $form_state | |
1240 * The form state array. | |
1241 * | |
1242 * @ingroup node_api_hooks | |
1243 */ | |
1244 function hook_validate($node, $form, &$form_state) { | |
1245 if (isset($node->end) && isset($node->start)) { | |
1246 if ($node->start > $node->end) { | |
1247 form_set_error('time', t('An event may not end before it starts.')); | |
1248 } | |
1249 } | |
1250 } | |
1251 | |
1252 /** | |
1253 * Display a node. | |
1254 * | |
1255 * This hook is invoked only on the module that defines the node's content type | |
1256 * (use hook_node_view() to act on all node views). | |
1257 * | |
1258 * This hook is invoked during node viewing after the node is fully loaded, so | |
1259 * that the node type module can define a custom method for display, or add to | |
1260 * the default display. | |
1261 * | |
1262 * @param $node | |
1263 * The node to be displayed, as returned by node_load(). | |
1264 * @param $view_mode | |
1265 * View mode, e.g. 'full', 'teaser', ... | |
1266 * @return | |
1267 * The passed $node parameter should be modified as necessary and returned so | |
1268 * it can be properly presented. Nodes are prepared for display by assembling | |
1269 * a structured array, formatted as in the Form API, in $node->content. As | |
1270 * with Form API arrays, the #weight property can be used to control the | |
1271 * relative positions of added elements. After this hook is invoked, | |
1272 * node_view() calls field_attach_view() to add field views to $node->content, | |
1273 * and then invokes hook_node_view() and hook_node_view_alter(), so if you | |
1274 * want to affect the final view of the node, you might consider implementing | |
1275 * one of these hooks instead. | |
1276 * | |
1277 * @ingroup node_api_hooks | |
1278 */ | |
1279 function hook_view($node, $view_mode) { | |
1280 if ($view_mode == 'full' && node_is_page($node)) { | |
1281 $breadcrumb = array(); | |
1282 $breadcrumb[] = l(t('Home'), NULL); | |
1283 $breadcrumb[] = l(t('Example'), 'example'); | |
1284 $breadcrumb[] = l($node->field1, 'example/' . $node->field1); | |
1285 drupal_set_breadcrumb($breadcrumb); | |
1286 } | |
1287 | |
1288 $node->content['myfield'] = array( | |
1289 '#markup' => theme('mymodule_myfield', $node->myfield), | |
1290 '#weight' => 1, | |
1291 ); | |
1292 | |
1293 return $node; | |
1294 } | |
1295 | |
1296 /** | |
1297 * @} End of "addtogroup hooks". | |
1298 */ |