[翻译:陈市明 摘自:http://drupal.org/node/82926] 在5.x中,你模块的基本信息不再是通过函数hook_help提供给Drupal,而是在info文件定义name和description即可(具体格式参见info文件指南)。在我们的例子中,该文件为onthisdate.info
通常格式如下:
; $Id$
name = Module Name
description = "A description of what your module does."
如果没有这个文件,则drupal不能在模块安装的时候找到该模块。
在我们的例子中,onthisdate.info应该包含如下数据:
; $Id$
name = On this date
description = "A block module that lists links to content such as blog entries or forum discussions that were created one week ago."
将把这些代码写到onthisdate.info文件中,保存到sites/all/modules/onthisdate目录
下面3句是在info文件中是可选的
dependencies = module1 module2 module3
package = "Your arbitrary grouping string"
version = "$Name$"
在我们的例子中,将不使用这些代码。如果你的模块依赖其他模块,则Drupal在你的依赖模块没有被激活的情况下是不允许激活的。
Package:在模块列表页面中的显示分组,如果该值为空则默认为“Uncategorized”。
Version:通过cvs直接得到的模块的版本号
该文件使用的是ini格式,所以该文件可以包含;表示注释;
; $Id$则让cvs自动把该文件的ID信息自动替换掉
关于ini的格式,具体参见PHP.net parse_ini_file documentation
除了info文件,我们还可以通过实现help钩子来添加额外的帮助信息。不管怎么样,最好还是实现help钩子。onthisdate模块的help钩子叫做onthisdate_help:
<?php
function onthisdate_help($section='') {
}?>
$section变量:是该页面的结点路径。官方推荐,最好在模块中通过swtich case语句来判断是否是该模块的结点路径。你可以参照如下代码:
<?php
/**
* 显示帮助和模块信息
* @param 当前帮助结点的路径#模块名
* @return 显示的帮助信息
*/
function onthisdate_help($section='') {
$output = '';
switch ($section) {
case "admin/help#onthisdate":
$output = '<p>'. t("Displays links to nodes created on this date"). '</p>';
break;
}
return $output;
} // function onthisdate_help
?>
把这些代码写到onthisdate.module文件中,保存到目录sites/all/modules/onthisdate
原文:
In Drupal 5.x the basic information about your module, its name and description, is no longer provided by hook_help. Instead, all modules now need to have a modulename.info file, containing meta information about the module (for details see Writing .info files (Drupal 5.x)). For our example, "onthisdate.info'.
The general format is:
; $Id$
name = Module Name
description = "A description of what your module does."
Without this file, your module will not show up in the module listing!.
for our example, it could contain the following:
; $Id$
name = On this date
description = "A block module that lists links to content such as blog entries or forum discussions that were created one week ago."
Add the source above to a file named to onthisdate.info before saving in your module's directory at sites/all/modules/onthisdate.
There are also three optional lines that may appear in the .info file:
dependencies = module1 module2 module3
package = "Your arbitrary grouping string"
version = "$Name$"
For our example module, these don't apply and we will simply omit them. If you assign dependencies for your module, Drupal will not allow it to be activated until the required dependencies are met.
If you assign a package string for your module, on the admin/build/modules page it will be listed with other modules with the same category. If you do not assign one, it will simply be listed as 'Uncategorized'. Not assigning a package for your module is perfectly ok; in general packages are best used for modules that are distributed together or are meant to be used together. If you have any doubt, leave this field blank.
Suggested examples of appropriate items for the package field:
* Audio
* Bot
* CCK
* Chat
* E-Commerce
* Event
* Feed Parser
* Organic groups
* Station
* Video
* Views
* Voting (if it uses/requires VotingAPI)
The version line will provide the version string for users getting their modules directly from CVS rather than using the tarball package that is created with a release.
The files use the ini format and can include a ; $Id$ to have CVS insert the file ID information.
For more information on ini file formatting, see the PHP.net parse_ini_file documentation.
We can also provide help and additional information about our module. Because of the use of the .info file described above, this hook is now optional. However, it is a good idea to implement it. The hook name for this function is 'help', so start with the onthisdate_help function:
<?php
function onthisdate_help($section='') {
}
?>
The $section variable provides context for the help: where in Drupal or the module are we looking for help. The recommended way to process this variable is with a switch statement. You'll see this code pattern in other modules.
<?php
/**
* Display help and module information
* @param section which section of the site we're displaying help
* @return help text for section
*/
function onthisdate_help($section='') {
$output = '';
switch ($section) {
case "admin/help#onthisdate":
$output = '<p>'. t("Displays links to nodes created on this date"). '</p>';
break;
}
return $output;
} // function onthisdate_help
?>
The admin/help#modulename case is used by the Drupal core to linked from the main help page (/admin/help or ?q=admin/help). You will eventually want to add more text to provide a better help message to the user.
More information about the help hook:
Drupal HEAD
Add the source above to a file named to onthisdate.module before saving in your Drupal installation.