1.
在sites/all/modules
下面創(chuàng)建一個(gè)annotate
文件夾
2. 創(chuàng)建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. 創(chuàng)建annotate module的實(shí)際的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. 這時(shí)候到Administer? Site building? Modules中就可以看到剛才添加的annotate模組.但這時(shí)候激活它在導(dǎo)航欄里面是看不到annotate設(shè)置菜單的.
5. 實(shí)現(xiàn)Hook(鉤子),添加一下代碼,重新激活annotate模組,這樣就可以看到在Administer? Site configuration下多了一個(gè)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'). 這里當(dāng)用戶(hù)通過(guò)http://www.example.com/?q=admin/settings/annotate訪問(wèn)的時(shí)候,將會(huì)調(diào)用drupal_get_form()函數(shù),并且通過(guò)它的form ID annotate_admin_settings來(lái)調(diào)用annotate_admin_settings()函數(shù).所以我們要自己定義這個(gè)方法.
/**
* 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類(lèi)型組成的數(shù)組
'#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. 實(shí)現(xiàn)hook_nodeapi(),當(dāng)Drupal對(duì)node做各種各樣的操作的時(shí)候?qū)φ{(diào)用此函數(shù).
/**
* 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,作為頁(yè)面現(xiàn)實(shí)內(nèi)容
/**
* 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. 到目前為止對(duì)于annotate的內(nèi)容我們還有做處理.從這里開(kāi)始,我們就要把annotate的數(shù)據(jù)存儲(chǔ)到數(shù)據(jù)庫(kù)里面,很多module里面都有.install文件,該文件就是創(chuàng)建數(shù)據(jù)庫(kù)表文件.我們要?jiǎng)?chuàng)建一個(gè)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. 這里要到數(shù)據(jù)庫(kù)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. 為了實(shí)現(xiàn)在現(xiàn)實(shí)annotate的時(shí)候讀取數(shù)據(jù)庫(kù)里面的數(shù)據(jù)現(xiàn)實(shí),這里要修改一下前面的hook_nodeapi函數(shù).修改以后的為:
/**
* 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
周銳 閱讀(316)
評(píng)論(0) 編輯 收藏 所屬分類(lèi):
PHP