/[drupal]/drupal/modules/node/node.api.php
ViewVC logotype

Contents of /drupal/modules/node/node.api.php

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.43 - (show annotations) (download) (as text)
Sat Nov 7 13:35:20 2009 UTC (3 weeks ago) by dries
Branch: MAIN
Changes since 1.42: +11 -12 lines
File MIME type: text/x-php
- Patch #607244 by sun: added permission to decrease performance impact of contextual links.
1 <?php
2 // $Id: node.api.php,v 1.42 2009/11/01 12:11:10 dries Exp $
3
4 /**
5 * @file
6 * Hooks provided by the Node module.
7 */
8
9 /**
10 * @addtogroup hooks
11 * @{
12 */
13
14 /**
15 * Inform the node access system what permissions the user has.
16 *
17 * This hook is for implementation by node access modules. In this hook,
18 * the module grants a user different "grant IDs" within one or more
19 * "realms". In hook_node_access_records(), the realms and grant IDs are
20 * associated with permission to view, edit, and delete individual nodes.
21 *
22 * The realms and grant IDs can be arbitrarily defined by your node access
23 * module; it is common to use role IDs as grant IDs, but that is not
24 * required. Your module could instead maintain its own list of users, where
25 * each list has an ID. In that case, the return value of this hook would be
26 * an array of the list IDs that this user is a member of.
27 *
28 * A node access module may implement as many realms as necessary to
29 * properly define the access privileges for the nodes.
30 *
31 * @param $user
32 * The user object whose grants are requested.
33 * @param $op
34 * The node operation to be performed, such as "view", "update", or "delete".
35 *
36 * @return
37 * An array whose keys are "realms" of grants, and whose values are arrays of
38 * the grant IDs within this realm that this user is being granted.
39 *
40 * For a detailed example, see node_access_example.module.
41 *
42 * @ingroup node_access
43 */
44 function hook_node_grants($account, $op) {
45 if (user_access('access private content', $account)) {
46 $grants['example'] = array(1);
47 }
48 $grants['example_owner'] = array($user->uid);
49 return $grants;
50 }
51
52 /**
53 * Set permissions for a node to be written to the database.
54 *
55 * When a node is saved, a module implementing hook_node_access_records() will
56 * be asked if it is interested in the access permissions for a node. If it is
57 * interested, it must respond with an array of permissions arrays for that
58 * node.
59 *
60 * Each permissions item in the array is an array with the following elements:
61 * - 'realm': The name of a realm that the module has defined in
62 * hook_node_grants().
63 * - 'gid': A 'grant ID' from hook_node_grants().
64 * - 'grant_view': If set to TRUE a user that has been identified as a member
65 * of this gid within this realm can view this node.
66 * - 'grant_edit': If set to TRUE a user that has been identified as a member
67 * of this gid within this realm can edit this node.
68 * - 'grant_delete': If set to TRUE a user that has been identified as a member
69 * of this gid within this realm can delete this node.
70 * - 'priority': If multiple modules seek to set permissions on a node, the
71 * realms that have the highest priority will win out, and realms with a lower
72 * priority will not be written. If there is any doubt, it is best to
73 * leave this 0.
74 *
75 * @ingroup node_access
76 */
77 function hook_node_access_records(stdClass $node) {
78 if (node_access_example_disabling()) {
79 return;
80 }
81
82 // We only care about the node if it has been marked private. If not, it is
83 // treated just like any other node and we completely ignore it.
84 if ($node->private) {
85 $grants = array();
86 $grants[] = array(
87 'realm' => 'example',
88 'gid' => TRUE,
89 'grant_view' => TRUE,
90 'grant_update' => FALSE,
91 'grant_delete' => FALSE,
92 'priority' => 0,
93 );
94
95 // For the example_author array, the GID is equivalent to a UID, which
96 // means there are many many groups of just 1 user.
97 $grants[] = array(
98 'realm' => 'example_author',
99 'gid' => $node->uid,
100 'grant_view' => TRUE,
101 'grant_update' => TRUE,
102 'grant_delete' => TRUE,
103 'priority' => 0,
104 );
105 return $grants;
106 }
107 }
108
109 /**
110 * Alter permissions for a node before it is written to the database.
111 *
112 * Node access modules establish rules for user access to content. Node access
113 * records are stored in the {node_access} table and define which permissions
114 * are required to access a node. This hook is invoked after node access modules
115 * returned their requirements via hook_node_access_records(); doing so allows
116 * modules to modify the $grants array by reference before it is stored, so
117 * custom or advanced business logic can be applied.
118 *
119 * @see hook_node_access_records()
120 *
121 * Upon viewing, editing or deleting a node, hook_node_grants() builds a
122 * permissions array that is compared against the stored access records. The
123 * user must have one or more matching permissions in order to complete the
124 * requested operation.
125 *
126 * @see hook_node_grants()
127 * @see hook_node_grants_alter()
128 *
129 * @param &$grants
130 * The $grants array returned by hook_node_access_records().
131 * @param $node
132 * The node for which the grants were acquired.
133 *
134 * The preferred use of this hook is in a module that bridges multiple node
135 * access modules with a configurable behavior, as shown in the example
136 * by the variable 'example_preview_terms'. This variable would
137 * be a configuration setting for your module.
138 *
139 * @ingroup node_access
140 */
141 function hook_node_access_records_alter(&$grants, stdClass $node) {
142 // Our module allows editors to tag specific articles as 'preview'
143 // content using the taxonomy system. If the node being saved
144 // contains one of the preview terms defined in our variable
145 // 'example_preview_terms', then only our grants are retained,
146 // and other grants are removed. Doing so ensures that our rules
147 // are enforced no matter what priority other grants are given.
148 $preview = variable_get('example_preview_terms', array());
149 // Check to see if we have enabled complex behavior.
150 if (!empty($preview)) {
151 foreach ($preview as $term_id) {
152 if (isset($node->taxonomy[$term_id])) {
153 // Our module grants are set in $grants['example'].
154 $temp = $grants['example'];
155 // Now remove all module grants but our own.
156 $grants = array('example' => $temp);
157 // No need to check additonal terms.
158 break;
159 }
160 }
161 }
162 }
163
164 /**
165 * Alter user access rules when trying to view, edit or delete a node.
166 *
167 * Node access modules establish rules for user access to content.
168 * hook_node_grants() defines permissions for a user to view, edit or
169 * delete nodes by building a $grants array that indicates the permissions
170 * assigned to the user by each node access module. This hook is called to allow
171 * modules to modify the $grants array by reference, so the interaction of
172 * multiple node access modules can be altered or advanced business logic can be
173 * applied.
174 *
175 * @see hook_node_grants()
176 *
177 * The resulting grants are then checked against the records stored in the
178 * {node_access} table to determine if the operation may be completed.
179 *
180 * @see hook_node_access_records()
181 * @see hook_node_access_records_alter()
182 *
183 * @param &$grants
184 * The $grants array returned by hook_node_grants().
185 * @param $account
186 * The user account requesting access to content.
187 * @param $op
188 * The operation being performed, 'view', 'update' or 'delete'.
189 *
190 * Developers may use this hook to either add additional grants to a user
191 * or to remove existing grants. These rules are typically based on either the
192 * permissions assigned to a user role, or specific attributes of a user
193 * account.
194 *
195 * @ingroup node_access
196 */
197 function hook_node_grants_alter(&$grants, $account, $op) {
198 // Our sample module never allows certain roles to edit or delete
199 // content. Since some other node access modules might allow this
200 // permission, we expressly remove it by returning an empty $grants
201 // array for roles specified in our variable setting.
202
203 // Get our list of banned roles.
204 $restricted = variable_get('example_restricted_roles', array());
205
206 if ($op != 'view' && !empty($restricted)) {
207 // Now check the roles for this account against the restrictions.
208 foreach ($restricted as $role_id) {
209 if (isset($user->roles[$role_id])) {
210 $grants = array();
211 }
212 }
213 }
214 }
215
216 /**
217 * Add mass node operations.
218 *
219 * This hook enables modules to inject custom operations into the mass operations
220 * dropdown found at admin/content, by associating a callback function with
221 * the operation, which is called when the form is submitted. The callback function
222 * receives one initial argument, which is an array of the checked nodes.
223 *
224 * @return
225 * An array of operations. Each operation is an associative array that may
226 * contain the following key-value pairs:
227 * - "label": Required. The label for the operation, displayed in the dropdown menu.
228 * - "callback": Required. The function to call for the operation.
229 * - "callback arguments": Optional. An array of additional arguments to pass to
230 * the callback function.
231 *
232 */
233 function hook_node_operations() {
234 $operations = array(
235 'approve' => array(
236 'label' => t('Approve the selected posts'),
237 'callback' => 'node_operations_approve',
238 ),
239 'promote' => array(
240 'label' => t('Promote the selected posts'),
241 'callback' => 'node_operations_promote',
242 ),
243 'sticky' => array(
244 'label' => t('Make the selected posts sticky'),
245 'callback' => 'node_operations_sticky',
246 ),
247 'demote' => array(
248 'label' => t('Demote the selected posts'),
249 'callback' => 'node_operations_demote',
250 ),
251 'unpublish' => array(
252 'label' => t('Unpublish the selected posts'),
253 'callback' => 'node_operations_unpublish',
254 ),
255 'delete' => array(
256 'label' => t('Delete the selected posts'),
257 ),
258 );
259 return $operations;
260 }
261
262 /**
263 * Act on node deletion.
264 *
265 * @param $node
266 * The node that is being deleted.
267 */
268 function hook_node_delete(stdClass $node) {
269 db_delete('mytable')
270 ->condition('nid', $node->nid)
271 ->execute();
272 }
273
274 /**
275 * A revision of the node is deleted.
276 *
277 * You can delete data associated with that revision.
278 *
279 * @param $node
280 * The node the action is being performed on.
281 */
282 function hook_node_revision_delete(stdClass $node) {
283 db_delete('upload')->condition('vid', $node->vid)->execute();
284 if (!is_array($node->files)) {
285 return;
286 }
287 foreach ($node->files as $file) {
288 file_delete($file);
289 }
290 }
291
292 /**
293 * Respond to node insertion.
294 *
295 * Take action when a new node of any type is being inserted in the database.
296 *
297 * @param $node
298 * The node the action is being performed on.
299 */
300 function hook_node_insert(stdClass $node) {
301 db_insert('mytable')
302 ->fields(array(
303 'nid' => $node->nid,
304 'extra' => $node->extra,
305 ))
306 ->execute();
307 }
308
309 /**
310 * Act on node objects when loaded.
311 *
312 * This hook allows you to add information to node objects when loaded from
313 * the database. It takes an array of nodes indexed by nid as its first
314 * parameter. For performance reasons, information for all available nodes
315 * should be loaded in a single query where possible.
316 *
317 * The types of all nodes being passed in are also available in the $types
318 * parameter. If your module keeps track of the node types it supports, this
319 * allows for an early return if nothing needs to be done.
320 *
321 * Due to the internal cache in node_load_multiple(), you should not use this
322 * hook to modify information returned from the {node} table itself, since
323 * this may affect the way nodes are returned from the cache in subsequent
324 * calls to the function.
325 *
326 * @see comment_node_load()
327 * @see taxonomy_node_load()
328 * @see forum_node_load()
329 *
330 * @param $nodes
331 * An array of node objects indexed by nid.
332 * @param $types
333 * An array containing the types of the nodes.
334 */
335 function hook_node_load($nodes, $types) {
336 $result = db_query('SELECT nid, foo FROM {mytable} WHERE nid IN(:nids)', array(':nids' => array_keys($nodes)));
337 foreach ($result as $record) {
338 $nodes[$record->nid]->foo = $record->foo;
339 }
340 }
341
342 /**
343 * Control access to a node.
344 *
345 * Modules may implement this hook if they want to have a say in whether or not
346 * a given user has access to perform a given operation on a node.
347 *
348 * The administrative account (user ID #1) always passes any access check,
349 * so this hook is not called in that case. Users with the "bypass node access"
350 * permission may always view and edit content through the administrative
351 * interface.
352 *
353 * Note that not all modules will want to influence access on all
354 * node types. If your module does not want to actively grant or
355 * block access, return NODE_ACCESS_IGNORE or simply return nothing.
356 * Blindly returning FALSE will break other node access modules.
357 *
358 * @link http://api.drupal.org/api/group/node_access/7 More on the node access system @endlink
359 * @ingroup node_access
360 * @param $node
361 * The node on which the operation is to be performed, or, if it does
362 * not yet exist, the type of node to be created.
363 * @param $op
364 * The operation to be performed. Possible values:
365 * - "create"
366 * - "delete"
367 * - "update"
368 * - "view"
369 * @param $account
370 * A user object representing the user for whom the operation is to be
371 * performed.
372 * @return
373 * NODE_ACCESS_ALLOW if the operation is to be allowed;
374 * NODE_ACCESS_DENY if the operation is to be denied;
375 * NODE_ACCESSS_IGNORE to not affect this operation at all.
376 */
377 function hook_node_access($node, $op, $account) {
378 $type = is_string($node) ? $node : $node->type;
379
380 if (in_array($type, node_permissions_get_configured_types())) {
381 if ($op == 'create' && user_access('create ' . $type . ' content', $account)) {
382 return NODE_ACCESS_ALLOW;
383 }
384
385 if ($op == 'update') {
386 if (user_access('edit any ' . $type . ' content', $account) || (user_access('edit own ' . $type . ' content', $account) && ($account->uid == $node->uid))) {
387 return NODE_ACCESS_ALLOW;
388 }
389 }
390
391 if ($op == 'delete') {
392 if (user_access('delete any ' . $type . ' content', $account) || (user_access('delete own ' . $type . ' content', $account) && ($account->uid == $node->uid))) {
393 return NODE_ACCESS_ALLOW;
394 }
395 }
396 }
397
398 // Returning nothing from this function would have the same effect.
399 return NODE_ACCESS_IGNORE;
400 }
401
402
403 /**
404 * The node is about to be shown on the add/edit form.
405 *
406 * @param $node
407 * The node the action is being performed on.
408 */
409 function hook_node_prepare(stdClass $node) {
410 if (!isset($node->comment)) {
411 $node->comment = variable_get("comment_$node->type", COMMENT_NODE_OPEN);
412 }
413 }
414
415 /**
416 * The node is being cloned for translation.
417 *
418 * This hook can be used to load additional data or copy values from
419 * $node->translation_source.
420 *
421 * @param $node
422 * The node the action is being performed on.
423 */
424 function hook_node_prepare_translation(stdClass $node) {
425 }
426
427 /**
428 * The node is being displayed as a search result.
429 *
430 * If you want to display extra information with the result, return it.
431 *
432 * @param $node
433 * The node the action is being performed on.
434 * @return
435 * Extra information to be displayed with search result.
436 */
437 function hook_node_search_result(stdClass $node) {
438 $comments = db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = :nid', array('nid' => $node->nid))->fetchField();
439 return format_plural($comments, '1 comment', '@count comments');
440 }
441
442 /**
443 * The node passed validation and is about to be saved.
444 *
445 * Modules may make changes to the node before it is saved to the database.
446 *
447 * @param $node
448 * The node the action is being performed on.
449 */
450 function hook_node_presave(stdClass $node) {
451 if ($node->nid && $node->moderate) {
452 // Reset votes when node is updated:
453 $node->score = 0;
454 $node->users = '';
455 $node->votes = 0;
456 }
457 }
458
459 /**
460 * The node being updated.
461 *
462 * @param $node
463 * The node the action is being performed on.
464 */
465 function hook_node_update(stdClass $node) {
466 db_update('mytable')
467 ->fields(array('extra' => $node->extra))
468 ->condition('nid', $node->nid)
469 ->execute();
470 }
471
472 /**
473 * The node is being indexed.
474 *
475 * If you want additional information to be indexed which is not already
476 * visible through node "view", then you should return it here.
477 *
478 * @param $node
479 * The node the action is being performed on.
480 * @return
481 * Array of additional information to be indexed.
482 */
483 function hook_node_update_index(stdClass $node) {
484 $text = '';
485 $comments = db_query('SELECT subject, comment, format FROM {comment} WHERE nid = :nid AND status = :status', array(':nid' => $node->nid, ':status' => COMMENT_PUBLISHED));
486 foreach ($comments as $comment) {
487 $text .= '<h2>' . check_plain($comment->subject) . '</h2>' . check_markup($comment->comment, $comment->format, '', TRUE);
488 }
489 return $text;
490 }
491
492 /**
493 * The user has finished editing the node and is previewing or submitting it.
494 *
495 * This hook can be used to check the node data. Errors should be set with
496 * form_set_error().
497 *
498 * @param $node
499 * The node the action is being performed on.
500 * @param $form
501 * The $form parameter from node_validate().
502 */
503 function hook_node_validate(stdClass $node, $form) {
504 if (isset($node->end) && isset($node->start)) {
505 if ($node->start > $node->end) {
506 form_set_error('time', t('An event may not end before it starts.'));
507 }
508 }
509 }
510
511 /**
512 * The node content is being assembled before rendering.
513 *
514 * TODO D7 This needs work to clearly explain the different build modes.
515 *
516 * The module may add elements to $node->content prior to rendering. This hook
517 * will be called after hook_view(). The structure of $node->content is a
518 * renderable array as expected by drupal_render().
519 *
520 * When $build_mode is 'rss', modules can also add extra RSS elements and
521 * namespaces to $node->rss_elements and $node->rss_namespaces respectively for
522 * the RSS item generated for this node.
523 * For details on how this is used @see node_feed()
524 *
525 * @see taxonomy_node_view()
526 * @see upload_node_view()
527 * @see comment_node_view()
528 *
529 * @param $node
530 * The node the action is being performed on.
531 * @param $build_mode
532 * The $build_mode parameter from node_build().
533 */
534 function hook_node_view(stdClass $node, $build_mode) {
535 $node->content['my_additional_field'] = array(
536 '#value' => $additional_field,
537 '#weight' => 10,
538 '#theme' => 'mymodule_my_additional_field',
539 );
540 }
541
542 /**
543 * The node content was built; the module may modify the structured content.
544 *
545 * This hook is called after the content has been assembled in a structured array
546 * and may be used for doing processing which requires that the complete node
547 * content structure has been built.
548 *
549 * If the module wishes to act on the rendered HTML of the node rather than the
550 * structured content array, it may use this hook to add a #post_render callback.
551 * Alternatively, it could also implement hook_preprocess_node(). See
552 * drupal_render() and theme() documentation respectively for details.
553 *
554 * @param $build
555 * A renderable array representing the node content.
556 *
557 * @see node_build()
558 */
559 function hook_node_build_alter($build) {
560 if ($build['#build_mode'] == 'full' && isset($build['an_additional_field'])) {
561 // Change its weight.
562 $build['an_additional_field']['#weight'] = -10;
563 }
564
565 // Add a #post_render callback to act on the rendered HTML of the node.
566 $build['#post_render'][] = 'my_module_node_post_render';
567 }
568
569 /**
570 * Defines module-provided node types.
571 *
572 * This hook allows a module to define one or more of its own node types. For
573 * example, the blog module uses it to define a blog node-type named "Blog
574 * entry." The name and attributes of each desired node type are specified in
575 * an array returned by the hook.
576 *
577 * Only module-provided node types should be defined through this hook. User-
578 * provided (or 'custom') node types should be defined only in the 'node_type'
579 * database table, and should be maintained by using the node_type_save() and
580 * node_type_delete() functions.
581 *
582 * @return
583 * An array of information defining the module's node types. The array
584 * contains a sub-array for each node type, with the machine-readable type
585 * name as the key. Each sub-array has up to 10 attributes. Possible
586 * attributes:
587 * - "name": the human-readable name of the node type. Required.
588 * - "base": the base string used to construct callbacks corresponding to
589 * this node type.
590 * (i.e. if base is defined as example_foo, then example_foo_insert will
591 * be called when inserting a node of that type). This string is usually
592 * the name of the module, but not always. Required.
593 * - "description": a brief description of the node type. Required.
594 * - "help": help information shown to the user when creating a node of
595 * this type.. Optional (defaults to '').
596 * - "has_title": boolean indicating whether or not this node type has a title
597 * field. Optional (defaults to TRUE).
598 * - "title_label": the label for the title field of this content type.
599 * Optional (defaults to 'Title').
600 * - "has_body": boolean indicating whether or not this node type has a body
601 * field. Optional (defaults to TRUE).
602 * - "body_label": the label for the body field of this content type. Optional
603 * (defaults to 'Body').
604 * - "locked": boolean indicating whether the administrator can change the
605 * machine name of this type. FALSE = changable (not locked),
606 * TRUE = unchangable (locked). Optional (defaults to TRUE).
607 *
608 * The machine-readable name of a node type should contain only letters,
609 * numbers, and underscores. Underscores will be converted into hyphens for the
610 * purpose of contructing URLs.
611 *
612 * All attributes of a node type that are defined through this hook (except for
613 * 'locked') can be edited by a site administrator. This includes the
614 * machine-readable name of a node type, if 'locked' is set to FALSE.
615 *
616 * For a detailed usage example, see node_example.module.
617 */
618 function hook_node_info() {
619 return array(
620 'blog' => array(
621 'name' => t('Blog entry'),
622 'base' => 'blog',
623 'description' => t('Use for multi-user blogs. Every user gets a personal blog.'),
624 )
625 );
626 }
627
628 /**
629 * Provide additional methods of scoring for core search results for nodes.
630 *
631 * A node's search score is used to rank it among other nodes matched by the
632 * search, with the highest-ranked nodes appearing first in the search listing.
633 *
634 * For example, a module allowing users to vote on content could expose an
635 * option to allow search results' rankings to be influenced by the average
636 * voting score of a node.
637 *
638 * All scoring mechanisms are provided as options to site administrators, and
639 * may be tweaked based on individual sites or disabled altogether if they do
640 * not make sense. Individual scoring mechanisms, if enabled, are assigned a
641 * weight from 1 to 10. The weight represents the factor of magnification of
642 * the ranking mechanism, with higher-weighted ranking mechanisms having more
643 * influence. In order for the weight system to work, each scoring mechanism
644 * must return a value between 0 and 1 for every node. That value is then
645 * multiplied by the administrator-assigned weight for the ranking mechanism,
646 * and then the weighted scores from all ranking mechanisms are added, which
647 * brings about the same result as a weighted average.
648 *
649 * @return
650 * An associative array of ranking data. The keys should be strings,
651 * corresponding to the internal name of the ranking mechanism, such as
652 * 'recent', or 'comments'. The values should be arrays themselves, with the
653 * following keys available:
654 * - "title": the human readable name of the ranking mechanism. Required.
655 * - "join": part of a query string to join to any additional necessary
656 * table. This is not necessary if the table required is already joined to
657 * by the base query, such as for the {node} table. Other tables should use
658 * the full table name as an alias to avoid naming collisions. Optional.
659 * - "score": part of a query string to calculate the score for the ranking
660 * mechanism based on values in the database. This does not need to be
661 * wrapped in parentheses, as it will be done automatically; it also does
662 * not need to take the weighted system into account, as it will be done
663 * automatically. It does, however, need to calculate a decimal between
664 * 0 and 1; be careful not to cast the entire score to an integer by
665 * inadvertantly introducing a variable argument. Required.
666 * - "arguments": if any arguments are required for the score, they can be
667 * specified in an array here.
668 */
669 function hook_ranking() {
670 // If voting is disabled, we can avoid returning the array, no hard feelings.
671 if (variable_get('vote_node_enabled', TRUE)) {
672 return array(
673 'vote_average' => array(
674 'title' => t('Average vote'),
675 // Note that we use i.sid, the search index's search item id, rather than
676 // n.nid.
677 'join' => 'LEFT JOIN {vote_node_data} vote_node_data ON vote_node_data.nid = i.sid',
678 // The highest possible score should be 1, and the lowest possible score,
679 // always 0, should be 0.
680 'score' => 'vote_node_data.average / CAST(%f AS DECIMAL)',
681 // Pass in the highest possible voting score as a decimal argument.
682 'arguments' => array(variable_get('vote_score_max', 5)),
683 ),
684 );
685 }
686 }
687
688
689 /**
690 * Act on node type creation.
691 *
692 * This hook allows modules to take action when a node type is created.
693 *
694 * @param $info
695 * The node type object which is being created.
696 */
697 function hook_node_type_insert($info) {
698 }
699
700 /**
701 * Act on node type changes.
702 *
703 * This hook allows modules to take action when a node type is modified.
704 *
705 * @param $info
706 * The node type object which is being modified.
707 */
708 function hook_node_type_update($info) {
709 if (!empty($info->old_type) && $info->old_type != $info->type) {
710 $setting = variable_get('comment_' . $info->old_type, COMMENT_NODE_OPEN);
711 variable_del('comment_' . $info->old_type);
712 variable_set('comment_' . $info->type, $setting);
713 }
714 }
715
716 /**
717 * Act on node type deletion.
718 *
719 * This hook allows modules to take action when a node type is deleted.
720 *
721 * @param $info
722 * The node type object which is being deleted.
723 */
724 function hook_node_type_delete($info) {
725 variable_del('comment_' . $info->type);
726 }
727
728 /**
729 * Respond to node deletion.
730 *
731 * This is a hook used by node modules. It is called to allow the module
732 * to take action when a node is being deleted from the database by, for
733 * example, deleting information from related tables.
734 *
735 * @param $node
736 * The node being deleted.
737 *
738 * To take action when nodes of any type are deleted (not just nodes of
739 * the type defined by this module), use hook_node() instead.
740 *
741 * For a detailed usage example, see node_example.module.
742 */
743 function hook_delete(stdClass $node) {
744 db_delete('mytable')
745 ->condition('nid', $nid->nid)
746 ->execute();
747 }
748
749 /**
750 * This is a hook used by node modules. It is called after load but before the
751 * node is shown on the add/edit form.
752 *
753 * @param $node
754 * The node being saved.
755 *
756 * For a usage example, see image.module.
757 */
758 function hook_prepare(stdClass $node) {
759 if ($file = file_check_upload($field_name)) {
760 $file = file_save_upload($field_name, _image_filename($file->filename, NULL, TRUE));
761 if ($file) {
762 if (!image_get_info($file->uri)) {
763 form_set_error($field_name, t('Uploaded file is not a valid image'));
764 return;
765 }
766 }
767 else {
768 return;
769 }
770 $node->images['_original'] = $file->uri;
771 _image_build_derivatives($node, TRUE);
772 $node->new_file = TRUE;
773 }
774 }
775
776 /**
777 * Display a node editing form.
778 *
779 * This hook, implemented by node modules, is called to retrieve the form
780 * that is displayed when one attempts to "create/edit" an item. This form is
781 * displayed at the URI http://www.example.com/?q=node/<add|edit>/nodetype.
782 *
783 * @param $node
784 * The node being added or edited.
785 * @param $form_state
786 * The form state array. Changes made to this variable will have no effect.
787 * @return
788 * An array containing the form elements to be displayed in the node
789 * edit form.
790 *
791 * The submit and preview buttons, taxonomy controls, and administrative
792 * accoutrements are displayed automatically by node.module. This hook
793 * needs to return the node title, the body text area, and fields
794 * specific to the node type.
795 *
796 * For a detailed usage example, see node_example.module.
797 */
798 function hook_form(stdClass $node, $form_state) {
799 $type = node_type_get_type($node);
800
801 $form['title'] = array(
802 '#type' => 'textfield',
803 '#title' => check_plain($type->title_label),
804 '#required' => TRUE,
805 );
806 $form['body'] = array(
807 '#type' => 'textarea',
808 '#title' => check_plain($type->body_label),
809 '#rows' => 20,
810 '#required' => TRUE,
811 );
812 $form['field1'] = array(
813 '#type' => 'textfield',
814 '#title' => t('Custom field'),
815 '#default_value' => $node->field1,
816 '#maxlength' => 127,
817 );
818 $form['selectbox'] = array(
819 '#type' => 'select',
820 '#title' => t('Select box'),
821 '#default_value' => $node->selectbox,
822 '#options' => array(
823 1 => 'Option A',
824 2 => 'Option B',
825 3 => 'Option C',
826 ),
827 '#description' => t('Please choose an option.'),
828 );
829
830 return $form;
831 }
832
833 /**
834 * Respond to node insertion.
835 *
836 * This is a hook used by node modules. It is called to allow the module
837 * to take action when a new node is being inserted in the database by,
838 * for example, inserting information into related tables.
839 *
840 * @param $node
841 * The node being inserted.
842 *
843 * To take action when nodes of any type are inserted (not just nodes of
844 * the type(s) defined by this module), use hook_node() instead.
845 *
846 * For a detailed usage example, see node_example.module.
847 */
848 function hook_insert(stdClass $node) {
849 db_insert('mytable')
850 ->fields(array(
851 'nid' => $node->nid,
852 'extra' => $node->extra,
853 ))
854 ->execute();
855 }
856
857 /**
858 * Load node-type-specific information.
859 *
860 * This is a hook used by node modules. It is called to allow the module
861 * a chance to load extra information that it stores about a node. The hook
862 * should not be used to replace information from the core {node} table since
863 * this may interfere with the way nodes are fetched from cache.
864 *
865 * @param $nodes
866 * An array of the nodes being loaded, keyed by nid. At call time,
867 * node.module has already loaded the basic information about the nodes, such
868 * as node ID (nid), title, and body.
869 *
870 * For a detailed usage example, see node_example.module.
871 */
872 function hook_load($nodes) {
873 $result = db_query('SELECT nid, foo FROM {mytable} WHERE nid IN (:nids)', array(':nids' => array_keys($nodes)));
874 foreach ($result as $record) {
875 $nodes[$record->nid]->foo = $record->foo;
876 }
877 }
878
879 /**
880 * Respond to node updating.
881 *
882 * This is a hook used by node modules. It is called to allow the module
883 * to take action when an edited node is being updated in the database by,
884 * for example, updating information in related tables.
885 *
886 * @param $node
887 * The node being updated.
888 *
889 * To take action when nodes of any type are updated (not just nodes of
890 * the type(s) defined by this module), use hook_node() instead.
891 *
892 * For a detailed usage example, see node_example.module.
893 */
894 function hook_update(stdClass $node) {
895 db_update('mytable')
896 ->fields(array('extra' => $node->extra))
897 ->condition('nid', $node->nid)
898 ->execute();
899 }
900
901 /**
902 * Verify a node editing form.
903 *
904 * This is a hook used by node modules. It is called to allow the module
905 * to verify that the node is in a format valid to post to the site.
906 * Errors should be set with form_set_error().
907 *
908 * @param $node
909 * The node to be validated.
910 * @param $form
911 * The node edit form array.
912 *
913 * To validate nodes of all types (not just nodes of the type(s) defined by
914 * this module), use hook_node() instead.
915 *
916 * Changes made to the $node object within a hook_validate() function will
917 * have no effect. The preferred method to change a node's content is to use
918 * hook_node_presave() instead. If it is really necessary to change
919 * the node at the validate stage, you can use function form_set_value().
920 *
921 * For a detailed usage example, see node_example.module.
922 */
923 function hook_validate(stdClass $node, &$form) {
924 if (isset($node->end) && isset($node->start)) {
925 if ($node->start > $node->end) {
926 form_set_error('time', t('An event may not end before it starts.'));
927 }
928 }
929 }
930
931 /**
932 * Display a node.
933 *
934 * This is a hook used by node modules. It allows a module to define a
935 * custom method of displaying its nodes, usually by displaying extra
936 * information particular to that node type.
937 *
938 * @param $node
939 * The node to be displayed, as returned by node_load().
940 * @param $build_mode
941 * Build mode, e.g. 'full', 'teaser', ...
942 * @return
943 * $node. The passed $node parameter should be modified as necessary and
944 * returned so it can be properly presented. Nodes are prepared for display
945 * by assembling a structured array, formatted as in the Form API, in
946 * $node->content. As with Form API arrays, the #weight property can be
947 * used to control the relative positions of added elements. After this
948 * hook is invoked, node_build() calls field_attach_view() to add field
949 * views to $node->content, and then invokes hook_node_view() and
950 * hook_node_build_alter(), so if you want to affect the final
951 * view of the node, you might consider implementing one of these hooks
952 * instead.
953 *
954 * For a detailed usage example, see node_example.module.
955 */
956 function hook_view(stdClass $node, $build_mode = 'full') {
957 if ((bool)menu_get_object()) {
958 $breadcrumb = array();
959 $breadcrumb[] = array('path' => 'example', 'title' => t('example'));
960 $breadcrumb[] = array('path' => 'example/' . $node->field1,
961 'title' => t('%category', array('%category' => $node->field1)));
962 $breadcrumb[] = array('path' => 'node/' . $node->nid);
963 menu_set_location($breadcrumb);
964 }
965
966 $node->content['myfield'] = array(
967 '#value' => theme('mymodule_myfield', $node->myfield),
968 '#weight' => 1,
969 );
970
971 return $node;
972 }
973
974 /**
975 * @} End of "addtogroup hooks".
976 */

  ViewVC Help
Powered by ViewVC 1.1.2