1.
         在sites/all/modules
下面创建一个annotate
文件夹
2.         创建annotate module的信息文件(annotate.info)
; $Id: annotate.info v 1.1.2.3 2007/06/18 23:06:32 dww Exp $
name = Annotate
description = Allows users to annotate nodes.
package = Example
version = 5.5
//dependencies = node blog
project = "annotate"
datestamp = "1193367002"
3.         创建annotate module的实际的module功能文件(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.
*/
4.         这时候到Administer➤ Site building➤ Modules中就可以看到刚才添加的annotate模组.但这时候激活它在导航栏里面是看不到annotate设置菜单的.
5.         实现Hook(钩子),添加一下代码,重新激活annotate模组,这样就可以看到在Administer➤ Site configuration下多了一个Annotation settings菜单
/**
* Implementation of hook_menu().
*/
function annotate_menu($may_cache) {
    $items = array();
    if ($may_cache) {
              $items[] = array(
              'path' => 'admin/settings/annotate',
              'title' => t('Annotation settings'),
              'description' => t('Change how annotations behave.'),
              'callback' => 'drupal_get_form',
              'callback arguments' => array('annotate_admin_settings'),
              'access' => user_access('administer site configuration')
              );
       }
       return $items;
}
6.         上面有行'callback' => 'drupal_get_form'代码,还有一行 'callback arguments' => array('annotate_admin_settings'). 这里当用户通过http://www.example.com/?q=admin/settings/annotate访问的时候,将会调用drupal_get_form()函数,并且通过它的form ID annotate_admin_settings来调用annotate_admin_settings()函数.所以我们要自己定义这个方法.
/**
* Define the settings form.
*/
function annotate_admin_settings() {
       $form['annotate_nodetypes'] = array(
              '#type' => 'checkboxes',
              '#title' => t('Users may annotate these node types'),
              '#options' => node_get_types('names'), //返回所有node类型组成的数组
              '#default_value' => variable_get('annotate_nodetypes', array('story')),
              '#description' => t('A text field will be available on these node types to make
              user-specific notes.'),
       );
       $form['array_filter'] = array('#type' => 'hidden');
       return system_settings_form($form);
}
7.         实现hook_nodeapi(),当Drupal对node做各种各样的操作的时候对调用此函数.
/**
* Implementation of hook_nodeapi().
*/
function annotate_nodeapi(&$node, $op, $teaser, $page) {
       switch ($op) {
              case 'view':
                     global $user;
                     // If only the node summary is being displayed, or if the
                     // user is an anonymous user (not logged in), abort.
                     if ($teaser || $user->uid == 0) {
                            break;
                     }
                     $types_to_annotate = variable_get('annotate_nodetypes', array('story'));
                     if (!in_array($node->type, $types_to_annotate)) {
                            break;
                     }
                     // Add our form as a content item.
                     $node->content['annotation_form'] = array(
                            '#value' => drupal_get_form('annotate_entry_form', $node),
                            '#weight' => 10
                     );
       }
}
8.         下面我们要定义annotate form,作为页面现实内容
/**
* Define the form for entering an annotation.
*/
function annotate_entry_form($node) {
       $form['annotate'] = array(
              '#type' => 'fieldset',
              '#title' => t('Annotations')
       );
       $form['annotate']['nid'] = array( 
              '#type' => 'value', 
              '#value' => $node->nid 
    ); 
       $form['annotate']['note'] = array(
              '#type' => 'textarea',
              '#title' => t('Node'),
              '#default_value' => $node->annotation,
              '#description' => t('Make your personal annotations about this content
here. Only you (and the site administrator) will be able to see them.')
       );
       $form['annotate']['submit'] = array(
              '#type' => 'submit',
              '#value' => t('Update')
       );
       return $form;
}
9.         到目前为止对于annotate的内容我们还有做处理.从这里开始,我们就要把annotate的数据存储到数据库里面,很多module里面都有.install文件,该文件就是创建数据库表文件.我们要创建一个annotate.install文件
<?php
// $Id$
function annotate_install() {
       drupal_set_message(t('Beginning installation of annotate module.'));
       switch ($GLOBALS['db_type']) {
              case 'mysql':
              case 'mysqli':
                     db_query("CREATE TABLE annotations (
                            uid int NOT NULL default 0,
                            nid int NOT NULL default 0,
                            note longtext NOT NULL,
                            timestamp int NOT NULL default 0,
                            PRIMARY KEY (uid, nid)
                            ) /*!40100 DEFAULT CHARACTER SET utf8 */;"
                     );
                     $success = TRUE;
                     break;
              case 'pgsql':
                     db_query("CREATE TABLE annotations (
                            uid int NOT NULL DEFAULT 0,
                            nid int NOT NULL DEFAULT 0,
                            note text NOT NULL,
                            timestamp int NOT NULL DEFAULT 0,
                            PRIMARY KEY (uid, nid)
                            );"
                     );
                     $success = TRUE;
                     break;
              default:
                     drupal_set_message(t('Unsupported database.'));
       }
       if ($success) {
              drupal_set_message(t('The module installed tables successfully.'));
       } else {
              drupal_set_message(t('The installation of the annotate module was unsuccessful.'),'error');
       }
}
10.     这里要到数据库system表里把annotate给删了,然后重新激活annotate模组.添加提交事件.
/*
* Save the annotation to the database.
*/
function annotate_entry_form_submit($form_id, $form_values) {
       global $user;
       $nid = $form_values['nid'];
       $note = $form_values['note'];
       db_query("DELETE FROM {annotations} WHERE uid = %d and nid = %d", $user->uid, $nid);
       db_query("INSERT INTO {annotations} (uid, nid, note, timestamp) VALUES (%d, %d, '%s', %d)", $user->uid, $nid, $note, time());
       drupal_set_message(t('Your annotation was saved.'));
}
11.     为了实现在现实annotate的时候读取数据库里面的数据现实,这里要修改一下前面的hook_nodeapi函数.修改以后的为:
/**
* Implementation of hook_nodeapi().
*/
function annotate_nodeapi(&$node, $op, $teaser, $page) {
       switch ($op) {
              case 'view':
                     global $user;
                     // If only the node summary is being displayed, or if the
                     // user is an anonymous user (not logged in), abort.
                     if ($teaser || $user->uid == 0) {
                            break;
                     }
                     $types_to_annotate = variable_get('annotate_nodetypes', array('story'));
                     if (!in_array($node->type, $types_to_annotate)) {
                            break;
                     }
                     // Get previously saved note, if any.
                     $result = db_query("SELECT note FROM {annotations} WHERE uid = %d AND nid = %d", $user->uid, $node->nid);
                     $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
                     );
       }
}
这样在重新激活使用一下就可以了!
	posted on 2007-11-29 15:41 
周锐 阅读(332) 
评论(0)  编辑  收藏  所属分类: 
PHP