使用drupal开发应用,系统默认已经提供了用户、角色和权限管理等功能,但是对于很多特定的应用,drupal默认的用户信息字段是不够的,所以就产生了添加用户信息字段的需求。对于刚刚接触drupal的开发者来说,这件事情也是比较困难。本文将阐述如何在drupal6上实现这个功能。
第一步,我们要创建一个新的模块demo_user。
首先,在sites/all/modules下创建目录demo_user。(文中所有路径都是相对于drupal的根目录)
然后在demo_user目录下创建文件:demo_user.info,文件内容如下:
name = Demo User
description = Demo User additional profiles
package = "Demo"
core = 6.x
version = "6.x-2.0"
project = "demo_user"
drupal6的模块列表页面就是通过这个文件来获取模块信息的。
下一步,我们将在demo_user目录下创建文件:demo_user.install,文件内容如下:
<?php
/**
* Implementation of hook_enable().
*/
function demo_user_enable() {
drupal_set_message(t('Demo User module successfully installed.'));
}
/**
* Implementation of hook_install().
*/
function demo_user_install() {
drupal_install_schema('demo_user');
}
/**
* Implementation of hook_uninstall().
*/
function demo_user_uninstall() {
drupal_uninstall_schema('demo_user');
}
/**
* Implementation of hook_schema()
*/
function demo_user_schema() {
return array(
'demo_user' => array(
'description' => t("Stores Demo User Mappings"),
'fields' => array(
'uid' => array('type' => 'int', 'not null' => TRUE),
'cn_name' => array('type' => 'varchar', 'length' => 128, 'not null' => FALSE),
),
'primary key' => array('uid'),
),
);
}
这个文件主要是用来为模块创建数据库表,同时会设置模块安装成功的信息。drupal6中,如果你已经安装过一次模块,当你在模块管理的List页面中uncheck这个模块保存后,并没有完全删除这个模块的信息,需要在Uninstall tab页面中在删除一遍,才能彻底删除模块的信息,包括drop该模块的数据库表。
最后一步就是创建demo_user模块的核心代码文件了,在demo_user目录下创建文件demo_user.module,内容如下:
<?php
/**
* Implementation of hook_user()
*/
function demo_user_user($op, &$edit, &$account, $category = NULL) {
// We only do updates if the user is not anonymous.
if ((!empty($account->uid) && $account->uid > 0) || user_access('administer users')) {
switch ($op) {
case 'delete':
demo_user_delete($edit, $account);
break;
case 'form':
return demo_user_form($edit, $account, $category);
break;
case 'register':
return demo_user_form($edit, $account, $category);
break;
case 'insert':
demo_user_insert($edit, $account, $category);
break;
case 'load':
demo_user_load($account);
break;
case 'update':
demo_user_update($edit, $account, $category);
break;
case 'validate':
demo_user_validate($edit, $account);
break;
}
}
}
/**
* Builds the form elements for use in the user administration screen
*/
function demo_user_form($edit, $account, $category = NULL) {
$form = array();
$form['demo_user'] = array('#type' => 'fieldset', '#title' => t('Demo User'), '#tree' => TRUE, '#weight' => 2);
$form['demo_user']['cn_name'] = array(
'#type' => 'textfield',
'#title' => t('Chinese name'),
'#default_value' => $account->demo_user['cn_name'],
'#description' => t('Input your chinese fullname'),
);
return $form;
}
/**
* Load the user information into the user variable
*/
function demo_user_load(&$account) {
if (!(db_result(db_query("SELECT COUNT(uid) FROM {demo_user} WHERE uid = %d", $account->uid)))) {
// if the user is not already in the table create the user then we can preload the object
db_query("INSERT INTO {demo_user} (uid) VALUES (%d)", $account->uid);
}
// populating the demo_user array of the user object
$account->demo_user = db_fetch_array(db_query("SELECT uid, cn_name FROM {demo_user} WHERE uid = %d", $account->uid));
}
/**
* Validates the form input from the administration screen elements
*/
function demo_user_validate($edit, $account) {
}
/**
* Update the user information in the system, if the account is updated
*/
function demo_user_update($edit, $account, $category = NULL) {
if ($category == 'account') {
$cn_name = $account->demo_user['cn_name'];
if (isset($edit['demo_user']['cn_name']) && !is_null($edit['demo_user']['cn_name']) && drupal_strlen($edit['demo_user']['cn_name'])) {
$cn_name = $edit['demo_user']['cn_name'];
}
db_query("UPDATE {demo_user} SET cn_name = '%s' WHERE uid = %d", $cn_name, $account->uid);
}
}
/**
* Add the user information to the system, if the account is created
*/
function demo_user_insert($edit, $account, $category = NULL) {
demo_user_load($account);
demo_user_update($edit, $account, $category);
}
/**
* Remove the user information from the system, if the account is deleted
*/
function demo_user_delete($edit, $account) {
// removing the user from the demo_user user table
db_query("DELETE FROM {demo_user} WHERE uid = %d", $account->uid);
}
?>
上面所有的3个文件写完之后,在drupal的模块安装界面,安装该模块,然后进入用户信息编辑页面,你就可以看到新字段cn_name被添加到了用户编辑页面。