Index: nodeapi_example/nodeapi_example.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/examples/nodeapi_example/nodeapi_example.install,v
retrieving revision 1.2
diff -u -r1.2 nodeapi_example.install
--- nodeapi_example/nodeapi_example.install	3 Oct 2009 14:56:46 -0000	1.2
+++ nodeapi_example/nodeapi_example.install	24 Feb 2010 18:12:39 -0000
@@ -1,23 +1,32 @@
 <?php
-
 // $Id: nodeapi_example.install,v 1.2 2009/10/03 14:56:46 rfay Exp $
 
 /**
- * Implementation of hook_install().
+ * @file
+ * This is an example outlining how a module can be used to extend existing
+ * content types.
+ *
+ * It defines a schema for, and installs, a table containing two columns:
+ *  - a node id (nid)
+ *  - a rating for that node (rating)
+ */
+
+/**
+ * Implements hook_install().
  */
 function nodeapi_example_install() {
   drupal_install_schema('nodeapi_example');
 }
 
 /**
- * Implementation of hook_uninstall().
+ * Implements hook_uninstall().
  */
 function nodeapi_example_uninstall() {
   drupal_uninstall_schema('nodeapi_example');
 }
 
 /**
- * Implementation of hook_schema().
+ * Implements hook_schema().
  */
 function nodeapi_example_schema() {
   $schema['nodeapi_example'] = array(
Index: nodeapi_example/nodeapi_example.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/examples/nodeapi_example/nodeapi_example.module,v
retrieving revision 1.2
diff -u -r1.2 nodeapi_example.module
--- nodeapi_example/nodeapi_example.module	3 Oct 2009 14:56:46 -0000	1.2
+++ nodeapi_example/nodeapi_example.module	24 Feb 2010 18:12:40 -0000
@@ -11,34 +11,35 @@
  */
 
 /**
- * Implementation of hook_form_alter().
+ * Implements hook_form_alter().
  *
  * By implementing this hook, we're able to modify any form. We'll only make
- * changes to two types: a node's content type configuration and edit forms.
+ * changes to two forms: a node's content type configuration and edit forms.
  *
  * We need to have a way for administrators to indicate which content types
- * should have our rating field added. This is done by inserting a checkbox in
- * the node's content type configuration page.
+ * should have our rating field added. This is done by inserting a pair of
+ * radio buttons in the node's content type configuration page.
  */
-function nodeapi_example_form_alter(&$form, $form_state, $form_id) {
-  if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
-    // Alter the node type's configuration form to add our setting. We don't
+function nodeapi_example_form_alter(&$form, &$form_state, $form_id) {
+  // Check if this is the node type's configuration form
+  if ($form_id == 'node_type_form' && isset($form['type'])) {
+    // Alter the form to add our setting. We don't
     // need to worry about saving this value back to the variable, the form
     // we're altering will do it for us.
     $form['workflow']['nodeapi_example'] = array(
       '#type' => 'radios',
       '#title' => t('NodeAPI Example Rating'),
-      '#default_value' => variable_get('nodeapi_example_'. $form['#node_type']->type, 0),
+      '#default_value' => variable_get('nodeapi_example_' . $form['#node_type']->type, 0),
       '#options' => array(0 => t('Disabled'), 1 => t('Enabled')),
       '#description' => t('Should this node have a rating attached to it?'),
     );
   }
-  // If the type and node field are set this may be a node edit form.
-  elseif (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) {
+  // Otherwise, check if this is a node edit form.
+  elseif (!empty($form['#node_edit_form'])) {
     // If the rating is enabled for this node type, we insert our control
     // into the form.
     $node = $form['#node'];
-    if (variable_get('nodeapi_example_'. $form['type']['#value'], 0)) {
+    if (variable_get('nodeapi_example_' . $form['type']['#value'], 0)) {
       $form['nodeapi_example_rating'] = array(
         '#type' => 'select',
         '#title' => t('Rating'),
@@ -52,84 +53,129 @@
 }
 
 /**
- * Implementation of hook_nodeapi().
+ * Implements hook_node_validate().
  *
- * We will implement several node API operations here. This hook allows us to
- * act on all major node operations, so we can manage our additional data
- * appropriately.
- */
-function nodeapi_example_nodeapi(&$node, $op, $teaser, $page) {
-  switch ($op) {
-    // When the content editing form is submitted, we need to validate the input
-    // to make sure the user made a selection, since we are requiring the rating
-    // field. We have to check that the value has been set to avoid showing an
-    // error message when a new blank form is presented. Calling form_set_error()
-    // when the field is set but zero ensures not only that an error message is
-    // presented, but also that the user must correct the error before being able
-    // to submit the node.
-    case 'validate':
-      if (variable_get('nodeapi_example_'. $node->type, TRUE)) {
-        if (isset($node->nodeapi_example_rating) && !$node->nodeapi_example_rating) {
-          form_set_error('nodeapi_example_rating', t('You must rate this content.'));
-        }
-      }
-      break;
-
-    // Now we need to take care of loading one of the extended nodes from the
-    // database. An array containing our extra field needs to be returned.
-    case 'load':
-      $rating = db_result(db_query('SELECT rating FROM {nodeapi_example} WHERE nid = %d', $node->nid));
-      return array('nodeapi_example_rating' => $rating);
-      break;
-
-    // Insert is called after the node has been validated and saved to the
-    // database. It gives us a chance to create our own record in the database.
-    case 'insert':
-      db_query('INSERT INTO {nodeapi_example} (nid, rating) VALUES (%d, %d)', $node->nid, $node->nodeapi_example_rating);
-      break;
-
-    // Update is called when an existing node has been changed. Here, we use a
-    // DELETE then an INSERT rather than an UPDATE. The reason is that a node
-    // created before this module was installed won't already have a rating
-    // saved so there would be nothing to update.
-    case 'update':
-      db_query('DELETE FROM {nodeapi_example} WHERE nid = %d', $node->nid);
-      db_query('INSERT INTO {nodeapi_example} (nid, rating) VALUES (%d, %d)', $node->nid, $node->nodeapi_example_rating);
-      break;
-
-    // Delete is called whn the node is being deleted, it gives us a chance
-    // to delete the rating too.
-    case 'delete':
-      db_query('DELETE FROM {nodeapi_example} WHERE nid = %d', $node->nid);
-      break;
-
-    // Finally, we need to take care of displaying our rating when the node is
-    // viewed. This operation is called after the node has already been prepared
-    // into HTML and filtered as necessary, so we know we are dealing with an
-    // HTML teaser and body. We will inject our additional information at the front
-    // of the node copy.
-    //
-    // Using nodeapi('view') is more appropriate than using a filter here, because
-    // filters transform user-supplied content, whereas we are extending it with
-    // additional information.
-    case 'view':
-      $node->content['nodeapi_example'] = array(
-        '#value' => theme('nodeapi_example_rating', $node->nodeapi_example_rating),
+ * When the content editing form is submitted, we need to validate the input
+ * to make sure the user made a selection, since we are requiring the rating
+ * field. We have to check that the value has been set to avoid showing an
+ * error message when a new blank form is presented. Calling form_set_error()
+ * when the field is set but zero ensures not only that an error message is
+ * presented, but also that the user must correct the error before being able
+ * to submit the node.
+ */
+function nodeapi_example_node_validate($node, $form) {
+  if (variable_get('nodeapi_example_' . $node->type, TRUE)) {
+    if (isset($node->nodeapi_example_rating) && !$node->nodeapi_example_rating) {
+      form_set_error('nodeapi_example_rating', t('You must rate this content.'));
+    }
+  }
+}
+
+/**
+ * Implements hook_node_load().
+ *
+ * Now we need to take care of loading one of the extended nodes from the
+ * database. An array containing our extra rating field needs to be returned.
+ */
+function nodeapi_example_node_load($nodes, $types) {
+  $result = db_query('SELECT nid, rating FROM {nodeapi_example} WHERE nid IN(:nids)', array(':nids' => array_keys($nodes)));
+  
+  foreach ($result as $record) {
+    $nodes[$record->nid]->nodeapi_example_rating = $record->rating;
+  }
+}
+
+/**
+ * Implements hook_node_insert().
+ *
+ * The node_insert hook is called after the node has been validated and saved to the
+ * database. It gives us a chance to create our own associated rating record in the database.
+ */
+function nodeapi_example_node_insert($node) {
+  $nid = db_insert('nodeapi_example')
+  ->fields(array(
+    'nid' => $node->nid,
+    'rating' => $node->nodeapi_example_rating,
+  ))
+  ->execute();
+}
+
+/**
+ * Implements hook_node_update().
+ *
+ * Update is called when an existing node has been changed. We first need to check
+ * whether this node already has a rating. If so, we UPDATE the rating. Otherwise, we
+ * INSERT a new rating into the nodeapi_example_rating table.
+ * The reason that some nodes may not have a rating is that they might have been created
+ * before this module was installed, so there would be nothing to update.
+ */
+function nodeapi_example_node_update($node) {
+  // Check whether a rating already exists for this node
+  $query = db_select('nodeapi_example', 'n')->condition('n.nid', $node->nid, '=');
+  $num_rows = $query->countQuery()->execute()->fetchField();
+
+  if($num_rows) {
+    db_update('nodeapi_example')
+    ->fields(array(
+      'rating' => $node->nodeapi_example_rating,
+    ))
+    ->condition('nid', $node->nid)
+    ->execute();
+  }
+  else {
+    db_insert('nodeapi_example')
+    ->fields(array(
+      'nid' => $node->nid,
+      'rating' => $node->nodeapi_example_rating,
+    ))
+    ->execute();
+  }
+}
+
+/**
+ * Implements hook_node_delete().
+ *
+ * Delete is called when the node is being deleted, it gives us a chance
+ * to delete the rating too.
+ */
+function nodeapi_example_node_delete($node) {
+    db_delete('nodeapi_example')
+    ->condition('nid', $node->nid)
+    ->execute();
+}
+
+/**
+ * Implements hook_node_view().
+ *
+ * Finally, we need to take care of displaying our rating when the node is
+ * viewed. This operation is called after the node has already been prepared
+ * into HTML and filtered as necessary, so we know we are dealing with an
+ * HTML teaser and body. We will inject our additional information at the front
+ * of the node copy.
+ *
+ * Using node_view is more appropriate than using a filter here, because
+ * filters transform user-supplied content, whereas we are extending it with
+ * additional information.
+ */
+function nodeapi_example_node_view($node, $view_mode) {
+  if(isset($node->nodeapi_example_rating) && $node->nodeapi_example_rating) {
+  $node->content['nodeapi_example'] = array(
         '#weight' => -1,
+        '#theme' => 'nodeapi_example_rating',
+        '#rating' => $node->nodeapi_example_rating,  // This will be passed in the $variables array to theme function
       );
-      break;
   }
 }
 
 /**
- * Implementation of hook_theme().
+ * Implements hook_theme().
  *
  * This lets us tell Drupal about our theme functions and their arguments.
  */
 function nodeapi_example_theme() {
   return array(
     'nodeapi_example_rating' => array(
-      'arguments' => array('rating'),
+      'variables' => array('rating' => NULL),
     ),
   );
 }
@@ -142,7 +188,9 @@
  * also wrap the default presentation in a CSS class that is prefixed by the module
  * name. This way, style sheets can modify the output without requiring theme code.
  */
-function theme_nodeapi_example_rating($rating) {
+function theme_nodeapi_example_rating($variables) {
+  $rating = $variables['rating'];
+ 
   $options = array(
     0 => t('Unrated'),
     1 => t('Poor'),
@@ -153,6 +201,6 @@
   $output = '<div class="nodeapi_example_rating">';
   $output .= t('Rating: %rating', array('%rating' => $options[(int) $rating]));
   $output .= '</div>';
-  return $output;
-}
 
+  return $output;
+}
\ No newline at end of file
