我认为在这里采用使用安装配置文件 API 的策略是一个不错的选择。通过使用安装配置文件 API,您可以完成大部分使用 Drupal 管理工具所能做到的事情。大多数核心表单只是在变量表中设置变量。为了能够合理地对非内容数据库内容进行版本控制,即配置,最好使用更新函数。
在我的站点上,我们有一个名为“ec”的模块,除了包含其 ec.install 文件中的更新函数(例如 ec_update_6001())外,几乎没有其他作用。
您的主安装函数可以负责在任何新安装上运行更新,以使您的模块保持最新。
function ec_install() {
$ret = array();
$num = 0;
while (1) {
$version = 6000 + $num;
$funcname = 'ec_update_' . $version;
if (function_exists($funcname)) {
$ret[] = $funcname();
$num++;
} else {
break;
}
}
return $ret;
}
我们现在提供实际文件中的一些示例更新函数。
function ec_update_6000() {
install_include(array('user'));
$editor_rid = install_add_role('editor');
install_add_permissions(DRUPAL_ANONYMOUS_RID, array('access comments'));
install_add_permissions(DRUPAL_AUTHENTICATED_RID, array('access comments', 'post comments', 'post comments without approval'));
install_add_permissions($editor_rid, array('administer comments', 'administer nodes'));
return array();
}
function ec_update_6001() {
install_include(array('system'));
install_enable_theme('pirc');
return array();
}
function ec_update_6002() {
install_include(array('node'));
$props = array(
'description' => 'Historical Movable Type blog entries',
);
install_create_content_type('mtblog', 'MT Blog entry', $props);
$props = array(
'description' => 'Article',
);
install_create_content_type('article', 'Article', $props);
return array();
}
实际上,这大多解决了数据库和Drupal代码的版本问题。我们广泛使用它。它使我们能够推广新的更改数据库配置的代码,而无需重新导入数据库或进行实时更改。这也意味着我们可以正确地测试发布版本,而不必担心隐藏的数据库更改。
最后,CCK和Views支持这种方法。请参见此代码片段。
function ec_update_6023() {
$ret = array();
drupal_install_modules(array('content', 'content_copy', 'text', 'number', 'optionwidgets'));
install_include(array('content', 'content_copy'));
install_content_copy_import_from_file(drupal_get_path('module', 'ec') . '/' . 'article.type', 'article');
$sql = "UPDATE {node_type} SET body_label='Body', has_body=1
WHERE type = 'article'";
$ret[] = update_sql($sql);
return $ret;
}