/[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.42 - (show annotations) (download) (as text)
Sun Nov 1 12:11:10 2009 UTC (4 weeks ago) by dries
Branch: MAIN
Changes since 1.41: +23 -23 lines
File MIME type: text/x-php
- Patch #595084 by c960657: use type hinting for .
1 <?php
2 // $Id: node.api.php,v 1.41 2009/10/24 11:49:52 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 $node->content
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 $node
555 * The node the action is being performed on.
556 * @param $build_mode
557 * The $build_mode parameter from node_build().
558 */
559 function hook_node_build_alter(stdClass $node, $build_mode) {
560 // Check for the existence of a field added by another module.
561 if (isset($node->content['an_additional_field'])) {
562 // Change its weight.
563 $node->content['an_additional_field']['#weight'] = -10;
564 }
565
566 // Add a #post_render callback to act on the rendered HTML of the node.
567 $node->content['#post_render'][] = 'my_module_node_post_render';
568 }
569
570 /**
571 * Defines module-provided node types.
572 *
573 * This hook allows a module to define one or more of its own node types. For
574 * example, the blog module uses it to define a blog node-type named "Blog
575 * entry." The name and attributes of each desired node type are specified in
576 * an array returned by the hook.
577 *
578 * Only module-provided node types should be defined through this hook. User-
579 * provided (or 'custom') node types should be defined only in the 'node_type'
580 * database table, and should be maintained by using the node_type_save() and
581 * node_type_delete() functions.
582 *
583 * @return
584 * An array of information defining the module's node types. The array
585 * contains a sub-array for each node type, with the machine-readable type
586 * name as the key. Each sub-array has up to 10 attributes. Possible
587 * attributes:
588 * - "name": the human-readable name of the node type. Required.
589 * - "base": the base string used to construct callbacks corresponding to
590 * this node type.
591 * (i.e. if base is defined as example_foo, then example_foo_insert will
592 * be called when inserting a node of that type). This string is usually
593 * the name of the module, but not always. Required.
594 * - "description": a brief description of the node type. Required.
595 * - "help": help information shown to the user when creating a node of
596 * this type.. Optional (defaults to '').
597 * - "has_title": boolean indicating whether or not this node type has a title
598 * field. Optional (defaults to TRUE).
599 * - "title_label": the label for the title field of this content type.
600 * Optional (defaults to 'Title').
601 * - "has_body": boolean indicating whether or not this node type has a body
602 * field. Optional (defaults to TRUE).
603 * - "body_label": the label for the body field of this content type. Optional
604 * (defaults to 'Body').
605 * - "locked": boolean indicating whether the administrator can change the
606 * machine name of this type. FALSE = changable (not locked),
607 * TRUE = unchangable (locked). Optional (defaults to TRUE).
608 *
609 * The machine-readable name of a node type should contain only letters,
610 * numbers, and underscores. Underscores will be converted into hyphens for the
611 * purpose of contructing URLs.
612 *
613 * All attributes of a node type that are defined through this hook (except for
614 * 'locked') can be edited by a site administrator. This includes the
615 * machine-readable name of a node type, if 'locked' is set to FALSE.
616 *
617 * For a detailed usage example, see node_example.module.
618 */
619 function hook_node_info() {
620 return array(
621 'blog' => array(
622 'name' => t('Blog entry'),
623 'base' => 'blog',
624 'description' => t('Use for multi-user blogs. Every user gets a personal blog.'),
625 )
626 );
627 }
628
629 /**
630 * Provide additional methods of scoring for core search results for nodes.
631 *
632 * A node's search score is used to rank it among other nodes matched by the
633 * search, with the highest-ranked nodes appearing first in the search listing.
634 *
635 * For example, a module allowing users to vote on content could expose an
636 * option to allow search results' rankings to be influenced by the average
637 * voting score of a node.
638 *
639 * All scoring mechanisms are provided as options to site administrators, and
640 * may be tweaked based on individual sites or disabled altogether if they do
641 * not make sense. Individual scoring mechanisms, if enabled, are assigned a
642 * weight from 1 to 10. The weight represents the factor of magnification of
643 * the ranking mechanism, with higher-weighted ranking mechanisms having more
644 * influence. In order for the weight system to work, each scoring mechanism
645 * must return a value between 0 and 1 for every node. That value is then
646 * multiplied by the administrator-assigned weight for the ranking mechanism,
647 * and then the weighted scores from all ranking mechanisms are added, which
648 * brings about the same result as a weighted average.
649 *
650 * @return
651 * An associative array of ranking data. The keys should be strings,
652 * corresponding to the internal name of the ranking mechanism, such as
653 * 'recent', or 'comments'. The values should be arrays themselves, with the
654 * following keys available:
655 * - "title": the human readable name of the ranking mechanism. Required.
656 * - "join": part of a query string to join to any additional necessary
657 * table. This is not necessary if the table required is already joined to
658 * by the base query, such as for the {node} table. Other tables should use
659 * the full table name as an alias to avoid naming collisions. Optional.
660 * - "score": part of a query string to calculate the score for the ranking
661 * mechanism based on values in the database. This does not need to be
662 * wrapped in parentheses, as it will be done automatically; it also does
663 * not need to take the weighted system into account, as it will be done
664 * automatically. It does, however, need to calculate a decimal between
665 * 0 and 1; be careful not to cast the entire score to an integer by
666 * inadvertantly introducing a variable argument. Required.
667 * - "arguments": if any arguments are required for the score, they can be
668 * specified in an array here.
669 */
670 function hook_ranking() {
671 // If voting is disabled, we can avoid returning the array, no hard feelings.
672 if (variable_get('vote_node_enabled', TRUE)) {
673 return array(
674 'vote_average' => array(
675 'title' => t('Average vote'),
676 // Note that we use i.sid, the search index's search item id, rather than
677 // n.nid.
678 'join' => 'LEFT JOIN {vote_node_data} vote_node_data ON vote_node_data.nid = i.sid',
679 // The highest possible score should be 1, and the lowest possible score,
680 // always 0, should be 0.
681 'score' => 'vote_node_data.average / CAST(%f AS DECIMAL)',
682 // Pass in the highest possible voting score as a decimal argument.
683 'arguments' => array(variable_get('vote_score_max', 5)),
684 ),
685 );
686 }
687 }
688
689
690 /**
691 * Act on node type creation.
692 *
693 * This hook allows modules to take action when a node type is created.
694 *
695 * @param $info
696 * The node type object which is being created.
697 */
698 function hook_node_type_insert($info) {
699 }
700
701 /**
702 * Act on node type changes.
703 *
704 * This hook allows modules to take action when a node type is modified.
705 *
706 * @param $info
707 * The node type object which is being modified.
708 */
709 function hook_node_type_update($info) {
710 if (!empty($info->old_type) && $info->old_type != $info->type) {
711 $setting = variable_get('comment_' . $info->old_type, COMMENT_NODE_OPEN);
712 variable_del('comment_' . $info->old_type);
713 variable_set('comment_' . $info->type, $setting);
714 }
715 }
716
717 /**
718 * Act on node type deletion.
719 *
720 * This hook allows modules to take action when a node type is deleted.
721 *
722 * @param $info
723 * The node type object which is being deleted.
724 */
725 function hook_node_type_delete($info) {
726 variable_del('comment_' . $info->type);
727 }
728
729 /**
730 * Respond to node deletion.
731 *
732 * This is a hook used by node modules. It is called to allow the module
733 * to take action when a node is being deleted from the database by, for
734 * example, deleting information from related tables.
735 *
736 * @param $node
737 * The node being deleted.
738 *
739 * To take action when nodes of any type are deleted (not just nodes of
740 * the type defined by this module), use hook_node() instead.
741 *
742 * For a detailed usage example, see node_example.module.
743 */
744 function hook_delete(stdClass $node) {
745 db_delete('mytable')
746 ->condition('nid', $nid->nid)
747 ->execute();
748 }
749
750 /**
751 * This is a hook used by node modules. It is called after load but before the
752 * node is shown on the add/edit form.
753 *
754 * @param $node
755 * The node being saved.
756 *
757 * For a usage example, see image.module.
758 */
759 function hook_prepare(stdClass $node) {
760 if ($file = file_check_upload($field_name)) {
761 $file = file_save_upload($field_name, _image_filename($file->filename, NULL, TRUE));
762 if ($file) {
763 if (!image_get_info($file->uri)) {
764 form_set_error($field_name, t('Uploaded file is not a valid image'));
765 return;
766 }
767 }
768 else {
769 return;
770 }
771 $node->images['_original'] = $file->uri;
772 _image_build_derivatives($node, TRUE);
773 $node->new_file = TRUE;
774 }
775 }
776
777 /**
778 * Display a node editing form.
779 *
780 * This hook, implemented by node modules, is called to retrieve the form
781 * that is displayed when one attempts to "create/edit" an item. This form is
782 * displayed at the URI http://www.example.com/?q=node/<add|edit>/nodetype.
783 *
784 * @param $node
785 * The node being added or edited.
786 * @param $form_state
787 * The form state array. Changes made to this variable will have no effect.
788 * @return
789 * An array containing the form elements to be displayed in the node
790 * edit form.
791 *
792 * The submit and preview buttons, taxonomy controls, and administrative
793 * accoutrements are displayed automatically by node.module. This hook
794 * needs to return the node title, the body text area, and fields
795 * specific to the node type.
796 *
797 * For a detailed usage example, see node_example.module.
798 */
799 function hook_form(stdClass $node, $form_state) {
800 $type = node_type_get_type($node);
801
802 $form['title'] = array(
803 '#type' => 'textfield',
804 '#title' => check_plain($type->title_label),
805 '#required' => TRUE,
806 );
807 $form['body'] = array(
808 '#type' => 'textarea',
809 '#title' => check_plain($type->body_label),
810 '#rows' => 20,
811 '#required' => TRUE,
812 );
813 $form['field1'] = array(
814 '#type' => 'textfield',
815 '#title' => t('Custom field'),
816 '#default_value' => $node->field1,
817 '#maxlength' => 127,
818 );
819 $form['selectbox'] = array(
820 '#type' => 'select',
821 '#title' => t('Select box'),
822 '#default_value' => $node->selectbox,
823 '#options' => array(
824 1 => 'Option A',
825 2 => 'Option B',
826 3 => 'Option C',
827 ),
828 '#description' => t('Please choose an option.'),
829 );
830
831 return $form;
832 }
833
834 /**
835 * Respond to node insertion.
836 *
837 * This is a hook used by node modules. It is called to allow the module
838 * to take action when a new node is being inserted in the database by,
839 * for example, inserting information into related tables.
840 *
841 * @param $node
842 * The node being inserted.
843 *
844 * To take action when nodes of any type are inserted (not just nodes of
845 * the type(s) defined by this module), use hook_node() instead.
846 *
847 * For a detailed usage example, see node_example.module.
848 */
849 function hook_insert(stdClass $node) {
850 db_insert('mytable')
851 ->fields(array(
852 'nid' => $node->nid,
853 'extra' => $node->extra,
854 ))
855 ->execute();
856 }
857
858 /**
859 * Load node-type-specific information.
860 *
861 * This is a hook used by node modules. It is called to allow the module
862 * a chance to load extra information that it stores about a node. The hook
863 * should not be used to replace information from the core {node} table since
864 * this may interfere with the way nodes are fetched from cache.
865 *
866 * @param $nodes
867 * An array of the nodes being loaded, keyed by nid. At call time,
868 * node.module has already loaded the basic information about the nodes, such
869 * as node ID (nid), title, and body.
870 *
871 * For a detailed usage example, see node_example.module.
872 */
873 function hook_load($nodes) {
874 $result = db_query('SELECT nid, foo FROM {mytable} WHERE nid IN (:nids)', array(':nids' => array_keys($nodes)));
875 foreach ($result as $record) {
876 $nodes[$record->nid]->foo = $record->foo;
877 }
878 }
879
880 /**
881 * Respond to node updating.
882 *
883 * This is a hook used by node modules. It is called to allow the module
884 * to take action when an edited node is being updated in the database by,
885 * for example, updating information in related tables.
886 *
887 * @param $node
888 * The node being updated.
889 *
890 * To take action when nodes of any type are updated (not just nodes of
891 * the type(s) defined by this module), use hook_node() instead.
892 *
893 * For a detailed usage example, see node_example.module.
894 */
895 function hook_update(stdClass $node) {
896 db_update('mytable')
897 ->fields(array('extra' => $node->extra))
898 ->condition('nid', $node->nid)
899 ->execute();
900 }
901
902 /**
903 * Verify a node editing form.
904 *
905 * This is a hook used by node modules. It is called to allow the module
906 * to verify that the node is in a format valid to post to the site.
907 * Errors should be set with form_set_error().
908 *
909 * @param $node
910 * The node to be validated.
911 * @param $form
912 * The node edit form array.
913 *
914 * To validate nodes of all types (not just nodes of the type(s) defined by
915 * this module), use hook_node() instead.
916 *
917 * Changes made to the $node object within a hook_validate() function will
918 * have no effect. The preferred method to change a node's content is to use
919 * hook_node_presave() instead. If it is really necessary to change
920 * the node at the validate stage, you can use function form_set_value().
921 *
922 * For a detailed usage example, see node_example.module.
923 */
924 function hook_validate(stdClass $node, &$form) {
925 if (isset($node->end) && isset($node->start)) {
926 if ($node->start > $node->end) {
927 form_set_error('time', t('An event may not end before it starts.'));
928 }
929 }
930 }
931
932 /**
933 * Display a node.
934 *
935 * This is a hook used by node modules. It allows a module to define a
936 * custom method of displaying its nodes, usually by displaying extra
937 * information particular to that node type.
938 *
939 * @param $node
940 * The node to be displayed, as returned by node_load().
941 * @param $build_mode
942 * Build mode, e.g. 'full', 'teaser', ...
943 * @return
944 * $node. The passed $node parameter should be modified as necessary and
945 * returned so it can be properly presented. Nodes are prepared for display
946 * by assembling a structured array, formatted as in the Form API, in
947 * $node->content. As with Form API arrays, the #weight property can be
948 * used to control the relative positions of added elements. After this
949 * hook is invoked, node_build() calls field_attach_view() to add field
950 * views to $node->content, and then invokes hook_node_view() and
951 * hook_node_build_alter(), so if you want to affect the final
952 * view of the node, you might consider implementing one of these hooks
953 * instead.
954 *
955 * For a detailed usage example, see node_example.module.
956 */
957 function hook_view(stdClass $node, $build_mode = 'full') {
958 if ((bool)menu_get_object()) {
959 $breadcrumb = array();
960 $breadcrumb[] = array('path' => 'example', 'title' => t('example'));
961 $breadcrumb[] = array('path' => 'example/' . $node->field1,
962 'title' => t('%category', array('%category' => $node->field1)));
963 $breadcrumb[] = array('path' => 'node/' . $node->nid);
964 menu_set_location($breadcrumb);
965 }
966
967 $node->content['myfield'] = array(
968 '#value' => theme('mymodule_myfield', $node->myfield),
969 '#weight' => 1,
970 );
971
972 return $node;
973 }
974
975 /**
976 * @} End of "addtogroup hooks".
977 */

  ViewVC Help
Powered by ViewVC 1.1.2