Index: docs/developer/examples/node_example.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/docs/developer/examples/node_example.module,v
retrieving revision 1.15
diff -u -r1.15 node_example.module
--- docs/developer/examples/node_example.module	12 Dec 2005 20:42:43 -0000	1.15
+++ docs/developer/examples/node_example.module	23 Feb 2006 00:31:57 -0000
@@ -13,10 +13,12 @@
  * Database definition:
  * @code
  *   CREATE TABLE node_example (
+ *     vid int(10) unsigned NOT NULL default '0',
  *     nid int(10) unsigned NOT NULL default '0',
  *     color varchar(255) NOT NULL default '',
  *     quantity int(10) unsigned NOT NULL default '0',
- *     PRIMARY KEY  (nid)
+ *     PRIMARY KEY (vid, nid),
+ *     KEY `node_example_nid` (nid)
  *   )
  * @endcode
  */
@@ -89,25 +91,6 @@
 }
 
 /**
- * Implementation of hook_link().
- *
- * This is implemented so that an edit link is displayed for users who have
- * the rights to edit a node.
- */
-function node_example_link($type, $node = 0, $main) {
-  $links = array();
-
-  if ($type == 'node' && $node->type == 'node_example') {
-    // Don't display a redundant edit link if they are node administrators.
-    if (node_example_access('update', $node) && !user_access('administer nodes')) {
-      $links[] = l(t('edit this example node'), "node/$node->nid/edit");
-    }
-  }
-
-  return $links;
-}
-
-/**
  * Implementation of hook_menu().
  *
  * In order for users to be able to add nodes of their own, we need to
@@ -132,21 +115,41 @@
  * a sub array containing information for each element in the form.
  */
 function node_example_form(&$node) {
-  $form['title'] = array('#type' => 'textfield', '#title' => t('Title'),
-    '#size' => 60, '#maxlength' => 128, '#required' => TRUE,
-    '#default_value' => $node->title);
-  $form['body'] = array('#type' => 'textarea', '#title' => t('Body'),
-    '#default_value' => $node->body, '#required' => FALSE );
+  // We need to define form elements for the node's title and body.
+  $form['title'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Title'),
+    '#required' => TRUE,
+    '#default_value' => $node->title,
+    '#weight' => -5
+  );
+  // We want the body and filter elements to be adjacent. We could try doing
+  // this by setting their weights, but another module might add elements to the
+  // form with the same weights and end up between ours. By putting them into a
+  // sub-array together, we're able force them to be rendered together.
+  $form['body_filter']['body'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Body'),
+    '#default_value' => $node->body,
+    '#required' => FALSE
+  );
+  $form['body_filter']['filter'] = filter_form($node->format);
 
   // Now we define the form elements specific to our node type.
-  $form['color'] = array('#type' => 'textfield', '#title' => t('Color'),
-    '#default_value' => $node->color, '#size' =>  60, '#maxlength' => 128,
-    '#weight' => 0);
-  $form['quantity'] = array('#type' => 'textfield', '#title' => t('Quantity'),
-    '#default_value' => $node->quantity, '#size' =>  10, '#maxlength' => 10,
-    '#weight' => 1);
+  $form['color'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Color'),
+    '#default_value' => $node->color
+  );
+  $form['quantity'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Quantity'),
+    '#default_value' => $node->quantity,
+    '#size' => 10,
+    '#maxlength' => 10
+  );
 
-  return array_merge($form, filter_form($node->format));
+  return $form;
 }
 
 /**
@@ -177,7 +180,7 @@
  * database inserts.
  */
 function node_example_insert($node) {
-  db_query("INSERT INTO {node_example} (nid, color, quantity) VALUES (%d, '%s', %d)", $node->nid, $node->color, $node->quantity);
+  db_query("INSERT INTO {node_example} (vid, nid, color, quantity) VALUES (%d, %d, '%s', %d)", $node->vid, $node->nid, $node->color, $node->quantity);
 }
 
 /**
@@ -187,15 +190,38 @@
  * database updates.
  */
 function node_example_update($node) {
-  db_query("UPDATE {node_example} SET color = '%s', quantity = %d WHERE nid = %d", $node->color, $node->quantity, $node->nid);
+  // if this is a new node or we're adding a new revision,
+  if ($node->revision) {
+    node_example_insert($node);
+  }
+  else {
+    db_query("UPDATE {node_example} SET color = '%s', quantity = %d WHERE vid = %d", $node->color, $node->quantity, $node->vid);
+  }
+}
+
+/**
+ * Implementation of hook_nodeapi().
+ *
+ * When a node revision is deleted, we need to remove the corresponding record
+ * from our table. The only way to handle revision deletion is by implementing
+ * hook_nodeapi().
+ */
+function node_example_nodeapi(&$node, $op, $teaser, $page) {
+  switch ($op) {
+    case 'delete revision':
+      // Notice that we're matching a single revision based on the node's vid.
+      db_query('DELETE FROM {node_example} WHERE vid = %d', $node->vid);
+      break;
+  }
 }
 
 /**
  * Implementation of hook_delete().
  *
- * When a node is deleted, we need to clean up related tables.
+ * When a node is deleted, we need to remove all related records from out table.
  */
 function node_example_delete($node) {
+  // Notice that we're matching all revision, by using the node's nid.
   db_query('DELETE FROM {node_example} WHERE nid = %d', $node->nid);
 }
 
@@ -207,7 +233,7 @@
  * every time a node is loaded, and allows us to do some loading of our own.
  */
 function node_example_load($node) {
-  $additions = db_fetch_object(db_query('SELECT color, quantity FROM {node_example} WHERE nid = %d', $node->nid));
+  $additions = db_fetch_object(db_query('SELECT color, quantity FROM {node_example} WHERE vid = %d', $node->vid));
   return $additions;
 }
 

