当你安装 Drupal 时,一些模块被启用,一些特定的配置被选择,但是这些默认的可能并不是你所需要的。Drupal 安装器使用了一个默认的安装过程 profile,用来决定所有的这些配置。通过创建你自己的安装过程 profile,你可以定制 Drupal 的初始安装,从而使你的站点带有你想要的模块和设置。可能你在为每一个高校工作,你想创建一个安装过程 profile,从而能够启用一个与你高校的单点登录系统相绑定的定制模块,能够为站点管理员创建一个新的角色,能够在安装完成时向你发送 e-mail。Drupal 的安装器系统,允许你通过创建一个安装过程 profile 来定制安装时的各种操作。在本章你将学到如何做到这一点。

Profile 的存放位置

你的 Drupal 站点已经包含了一个安装过程 profile。它是 Drupal 自带的默认的安装过程 profile,位于 profiles/default/default.profile。我们想创建一个新的名为“university”(大学)的 profile,所以我们将先在 profiles/university/university.profile 创建一个新的文件。先在,我们将向这个文件仅添加一个独立的函数:

<?php
// $Id$
/**
* Return a description of the profile for the initial installation screen.
*
* @return
* An array with keys ‘name’ and ‘description’ describing this profile.
*/
function university_profile_details() {
return array(
‘name’ => st(’Drupal (Customized for Iowa State University)’),
‘description’ => st(’Select this profile to enable settings typical for a departmental website.’)
);
}

注意,这里文件的名称与 profile 目录的名称相同,在文件名的后面使用了 .profile 后缀,文件 university.profile 中的所有函数都以前缀 university_ 开头。我们还是用了函数 st(),而不是通常用的 t(),这是因为在安装器运行这段代码时,Drupal 还没有运行完一个完整的引导指令,所以 t() 不可用。

安装过程 Profile 的工作原理

当 Drupal 的安装器启动时,它扫描 profiles 目录以查看有多少个可用的 profile。如果他发现多于一个时,它将向用户提供所有的 profile 以备用户选择。例如,创建了我们的 university.profile 文件并向其添加了 university_profile_details() 函数以后,访问 http://example.com/install.php,将会产生一个如图23-1所示的截图。

图23-1 Drupal 展示了一个有哪些 profile 可用的选项

Drupal 的安装器接下来将再度回到安装过程 profile 上。它将找出该 profile 想要启用哪些模块,在安装过程的最后,当安装器将执行交给安装过程 profile 时,它又回来了一次。在后面的之一阶段,更多的 Drupal 定制化完成了。流程的概貌如图23-2所示。

指示启用哪些模块

通过添加函数 university_profile_modules(),我们告诉 Drupal 我们的安装过程 profile 想启用哪些模块(还有,我们知道这个函数的名称应该由我们的 profile 的名称加上 _profile_modules 合成)。

/**
* Return an array of the modules to be enabled when this profile is installed.
*
* @return
* An array of modules to be enabled.
*/
function university_profile_modules() {
return array(
// Enable required core modules.
‘block’, ‘filter’, ‘help’, ‘node’, ’system’, ‘user’, ‘watchdog’,
// Enable optional core modules.
‘color’, ‘help’, ‘taxonomy’, ‘throttle’, ’search’, ’statistics’,
// Enable single signon by enabling a contributed module.
‘pubcookie’,
);
}

在启用这些模块以前,安装器会询问每一个模块,以查看正被安装的 Drupal 系统是否提供了该模块所有的必要条件。通过为每个模块调用 hook_requirements(’install’) 来完成检查。如果要求没有被满足,安装器将会失败,并报告缺少了哪些条件。

图23-2 安装器是如何与安装过程 profile 交互的

注意:必要条件钩子是一个可选的钩子,它允许模块在在进行安装以前测试所需环境是否准备好了。关于该钩子的更多信息,参看 http://api.drupal.org/api/5/function/hook_requirements

安装器在启用模块以前确保模块是存在的。它会在多个地方查找,这些位置在表23-1中列出来了。由于我们要启用 pubcookie 模块(不包含在 Drupal 内核中的模块),在运行我们的安装过程 profile 以前,我们需要确保能够在这些所列的目录中找到它。

Table 23-1 Drupal 模块可以存放的目录

目录 存放的模块
modules Drupal 核心模块
sites/all/modules 第3方模块(适用于所有站点)
profiles/profilename/modules 位于安装过程 profile 中的模块
sites/*/modules 与你的 settings.php 文件位于同一目录的模块

安装器还会在放置你站点 settings.php 文件的目录下查找模块。如果 settings.php 位于 sites/default,那么 Drupal 会在 sites/default/modules 下面找。类似的,如果 settings.php 位于 sites/example.com,那么 Drupal 将在 sites/example.com/modules 下面寻找模块。

最后设置

安装系统将按照我们列出模块的次序来启用我们所需要的模块,然后再调用安装过程 profile。这一次,安装系统将查找一个名为 university_install() 的函数。在我们的例子中,我们没有实现这个函数,这是因为我们想在函数 university_profile_final() 中完成所有的事情。但是在引导指令结束以前调用 install() 钩子,如果需要的话,使你能够在最后时刻修改所要设置的东西。

注意:在 Drupal 的下一个版本中,安装过程 profile 中的 install() 函数将被删除,如果兼容性对你很重要的话,推荐你完全彻底 profile_final() 钩子。

最后,安装器调用 university_profile_final()。

/**
* Perform final installation tasks for this installation profile.
*/
function university_profile_final() {
// Define a node type, ‘page’.
$node_type = array(
‘type’ => ‘page’,
‘name’ => st(’Page’),
‘module’ => ‘node’,
‘description’ => st(’A standard web page.’),
‘custom’ => TRUE,
‘modified’ => TRUE,
‘locked’ => FALSE,
‘has_title’ => TRUE,
‘has_body’ => TRUE,
‘orig_type’ => ‘page’,
‘is_new’ => TRUE,
);
node_type_save((object) $node_type);
// Page node types should be published and create new revisions by default.
variable_set(’node_options_page’, array(’status’, ‘revision’));
// If the administrator enables the comment module, we want
// to have comments disabled for pages.
variable_set(’comment_page’, COMMENT_NODE_DISABLED);
// Define a node type, ‘newsitem’.
$node_type = array(
‘type’ => ‘news’,
‘name’ => st(’News Item’),
‘module’ => ‘node’,
‘description’ => st(’A news item for the front page.’),
‘custom’ => TRUE,
‘modified’ => TRUE,
‘locked’ => FALSE,
‘has_title’ => TRUE,
‘has_body’ => TRUE,
‘orig_type’ => ‘news’,
‘is_new’ => TRUE,
);
node_type_save((object) $node_type);
// News items should be published and promoted to front page by default.
// News items should create new revisions by default.
variable_set(’node_options_news’, array(’status’, ‘revision’, ‘promote’));
// If the administrator enables the comment module, we want
// to have comments enabled for news items.
variable_set(’comment_news’, COMMENT_NODE_READ_WRITE);
// Create a taxonomy so news can be classified.
$vocabulary = array(
‘name’ => t(’News Categories’),
‘description’ => st(’Select the appropriate audience for your news item.’),
‘help’ => st(’You may select multiple audiences.’),
‘nodes’ => array(’news’ => st(’News Item’)),
‘hierarchy’ => 0,
‘relations’ => 0,
‘tags’ => 0,
‘multiple’ => 1,
‘required’ => 0,
);
taxonomy_save_vocabulary($vocabulary);
// Define some terms to categorize news items.
$terms = array(
st(’Departmental News’),
st(’Faculty News’),
st(’Staff News’),
st(’Student News’),
);
// Submit the “Add term” form programmatically for each term.
foreach ($terms as $name) {
drupal_execute(’taxonomy_form_term’, array(’name’ => $name), $vid);
}
// Add a role.
db_query(”INSERT INTO {role} (name) VALUES (’%s’)”, ’site administrator’);
// Configure the pubcookie module.
variable_set(’pubcookie_login_dir’, ‘login’);
variable_set(’pubcookie_id_is_email’, 1);
// …other settings go here
// Report by email that a new Drupal site has been installed.
$to = ‘administrator@example.com’;
$from = ini_get(’sendmail_from’);
$subject = st(’New Drupal site created!’);
$body = st(’A new Drupal site was created: @site’, array(’@site’ => base_path()));
drupal_mail(’university-profile’, $to, $subject, $body, $from);
}

如前面的例子所示,安装过程 profile 需要完成多个常用任务。安装器在调用 profile_final() 钩子以前,先完成一个完整的 Drupal 引导指令,所以在该钩子中可以使用所有的 Drupal 函数了。

注意:在安装过程 profile 中,我们使用 st() 来代替 t(),从而使得整个安装过程 profile 可被翻译,如果有任何翻译的话,将被存放在一个安装过程 profile 的翻译文件中。这是一个与安装过程 profile 位于同一个目录下的 .po 文件。更多关于 .po 文件的信息,参看第18章。

设置 Drupal 变量

通过简单的调用 variable_set() 来设置 Drupal 变量:

variable_set(’pubcookie_login_dir’, ‘login’);

创建初始节点类型

如果你需要使用 Drupal 内置的内容类型系统创建新的节点类型的话,你只需要创建一个节点类型定义对象并并将其传递给 node_type_save() 就可以了。在前面的 profile 例子中,我们创建了两个节点类型:“page”用于普通网页,而“news”用于新闻项。我们接着使用了 variable_set() 来设置节点的默认选项,这样发布的新闻项将会出现在首页,而“page”则不。

如果你启用了提供节点类型的模块,根据这些模块中的 node_info() 钩子,Drupal 就可以调用里面的节点类型了。

将信息保存到数据库中

一个安装过程 profile 可能想修改一些数据库设置。由于现在数据库连接已经可用,可以使用 db_query() 来修改数据库。在我们的例子中,我们为该 Drupal 站点添加了一个角色。在你的 profile 中,你可做更多修改,比如向 permission 表中插入一些权限。

一个简单的获取合适的查询的方法是,先按照默认设置安装好 Drupal,接着完全按照你想要的方式来配置它。这甚至可以包括创建一些节点,完成 URL 别名的设置。本章例子中的安装过程 profile 可能想要一个 About 页面,一个 Courses Taught 页面等等。当完成了这一配置过程以后,你可以使用你的数据库工具来将你站点的数据库完全导入到一个 SQL 脚本中。然后在 SQL 脚本中挑出你想要的 INSERT SQL 命令,并将它们包含到你的安装过程 profile 中。

提交表单

由于 Drupal 支持通过程序的方式提交表单,如果你要与网站交互的话,你可以使用 drupal_execute() 来提交表单。在前面的例子中,我们使用这种方式来为站点添加分类词语。关于 drupal_execute() 的更多信息,参看第10章。

总结

在本章,你学到了以下几点:

  • 什么是安装过程 profile
  • 安装过程 profile 的存放位置
  • 如何创建一个基本的安装过程 profile
  • 在最后安装阶段如何操纵 Drupal

相关文章


没有评论

(*)
(不会公布)