Drupal 如何创建一个annotate模块

Drupal 的所以自定义模块都是放在“sites/all/modules”文件夹下面。如果“sites/all”下没有“modules”文件夹,请手动建立一个。
以下模块是在6.25版本测试通过。

一、模块信息文件 annotate.info
代码如下
; $Id$
name = Annotate
description = Allows users to annotate nodes.
core = 6.x
package = Pro Drupal Development

二、模块程序文件 annotate.module

代码如下

<?php

// $Id$

/**
 * @file
 * Lets users add private annotations to nodes.
 *
 * Adds a text field when a node is displayed
 * so that authenticated users may make notes.
 */

/**
 * Implementation of hook_menu().
 */
function annotate_menu() {
  $items['admin/settings/annotate'] = array(
    'title' => 'Annotation settings',
    'description' => 'Change how annotations behave',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('annotate_admin_settings'),
    'access arguments' => array('administer site configuration'),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'annotate.admin.inc',
  );
 
  return $items;
}

/**
 * Implementation of hook_nodeapi().
 */
function annotate_nodeapi(&$node, $op, $teaser, $page) {
  global $user;
  switch ($op) {
    // The 'view' operation means the node is about to be displayed.
    case 'view':
      // Abort if the user is an anonymous user (not logged in) or
      // if the node is not being displayed on a page by itself
      // (for example, it could be in a node listing or search result).
      if ($user->uid == 0 || !$page) {
        break;
      }
      // Find out which node types we should annotate.
      $types_to_annotate = variable_get('annotate_nodetypes', array('page'));
     
      // Abort if this node is not one of the types we should annotate.
      if (!in_array($node->type, $types_to_annotate)) {
        break;
      }
     
      // Get the current annotation for this node from the database
      // and store it in the node object.
      $result = db_query('SELECT note FROM {annotations} WHERE nid = %d AND uid = %d', $node->nid, $user->uid);
      $node->annotation = db_result($result);

      // Add our form as a content item.
      $node->content['annotation_form'] = array(
        '#value' => drupal_get_form('annotate_entry_form', $node),
        '#weight' => 10
      );
      break;
     
    case 'delete':
      db_query('DELETE FROM {annotations} WHERE nid = %d', $node->nid);
      break;
  }
}

/**
 * Define the form for entering an annotation.
 */
function annotate_entry_form($form_state, $node) {
  // Define a fieldset.
  $form['annotate'] = array(
    '#type' => 'fieldset',
    '#title' => t('Annotations')
  );
 
  // Define a textarea inside the fieldset.
  $form['annotate']['note'] = array(
    '#type' => 'textarea',
    '#title' => t('Notes'),
    '#default_value' => isset($node->annotation) ? $node->annotation : '',
    '#description' => t('Make your personal annotations about this content here. Only you (and the site administrator) will be able to see them.')
  );
 
  // For convenience, save the node id.
  $form['annotate']['nid'] = array(
    '#type' => 'value',
    '#value' => $node->nid
  );
 
  // Define a submit button.
  $form['annotate']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Update')
  );
  return $form;
}

/**
 * Handle submission of the annotation form and saving
 * of the data to the database.
 */
function annotate_entry_form_submit($form, $form_state) {
  global $user;
 
  $note = $form_state['values']['note'];
  $nid = $form_state['values']['nid'];
 
  db_query('DELETE FROM {annotations} WHERE nid = %d AND uid = %d', $nid, $user->uid);
  db_query("INSERT INTO {annotations} (nid, uid, note, created) VALUES (%d, %d, '%s', %d)", $nid, $user->uid, $note, time());
  drupal_set_message(t('Your annotation has been saved.'));
}

三、annotate.admin.inc 文件

代码如下
 

<?php
// $Id$

/**
 * @file
 * Administration page callbacks for the annotate module.
 */

/**
 * Form builder. Configure annotations.
 *
 * @ingroup forms
 * @see system_settings_form().
 */
function annotate_admin_settings() {
  // Get an array of node types with internal names as keys and
  // "friendly names" as values. E.g.,
  // array('page' => 'Page', 'story' => 'Story')
  $options = node_get_types('names');
 
  $form['annotate_node_types'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Users may annotate these content types'),
    '#options' => $options,
    '#default_value' => variable_get('annotate_node_types', array('page')),
    '#description' => t('A text field will be available on these content types to make user-specific notes.'),
  );

  return system_settings_form($form);

}

四、安装文件 annotate.install

代码如下
 

<?php
// $Id$

/**
 * Implementation of hook_install().
 */
function annotate_install() {
  // Use schema API to create database table.
  drupal_install_schema('annotate');
}

/**
 * Implementation of hook_uninstall().
 */
function annotate_uninstall() {
  // Use schema API to delete database table.
  drupal_uninstall_schema('annotate');
}

/**
 * Implementation of hook_schema().
 */
function annotate_schema() {
  $schema['annotations'] = array(
    'description' => t('Stores node annotations that users write.'),
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => t('The {node}.nid to which the annotation applies.'),
      ),
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => t('The {user}.uid of the user who created the annotation.')
      ),
      'note' => array(
        'description' => t('The text of the annotation.'),
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'big'
      ),
      'created' => array(
        'description' => t('A Unix timestamp indicating when the annotation was created.'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0
      ),
    ),
    'primary key' => array(
      'nid', 'uid'
    ),
  );
 
  return $schema;
}

建立好以上文件放在"sites/all/modules/annotate"文件夹下,在后台开启模块。此模块的作用是在已经发布的文章下面显示添加注解的功能。