drupal module 开发的例子

来源:互联网 发布:node 删除模块 编辑:程序博客网 时间:2024/06/11 04:14

需求:
开发一个module,列出某一时间段内的所有node。module的名称为current_posts


开始
1 module的文件放的位置

在drupal中,$DRUPAL_ROOT/sites/all/modules 存放非core的module。我们在此目录下建立名为current_posts的目录, 所有的current_posts module的文件都放在此目录下。

2 module的描述文件current_posts.info
current_posts.info文件描述了module的基本信息。current_posts.info的内容为:
name = Current posts
description = A block module that lists links to rencent posts.
core = 7.x
files[] = current_posts.test

.info文件的格式
必填的字段:
name
description
core
可选的字段:
files
例:file[] = example.test
Drupal 支持动态加载代码。module要在files字段中包含定义class和interface的文件。在enable module的时候,Drupal会扫描files字段指定的文件,记录所有的class和interface。当访问class和interface, 他们会被自动php加载
dependencies
例:dependencies[] = taxonomy   
例:dependencies[] = comment
本module需要qitamodule的支持,如果依赖的module没有enable,则本enable本module会失败
package
例:package = "you arbitrary grouping string"
module的分组。如果没有package字段,module默认被分到others

3 实现hook
一般的把hook的实现函数写到module_name.module文件中,对于本例,对应的写到current_posts.module
3.1 实现hook_help
 
当访问admin/help/current_posts时,会看到help的内容
3.2 实现hook_block_info
 
3.3 获取数据

 
3.4 实现hook_block_view
 
3.5 配置module
3.5.1 准备配置module的表单
实现hook_menu

当访问admin/config/content/current_posts时会调用page callback指定的函数,并给函数传递page arguments指定的参数
type的值为MENU_NORMAL_ITEM时, 会在navigation菜单中
 

3.5.2 校验数据 实现hook_form_validate
 
3.5.3 权限设置 实现hook_permission
function current_posts_permission()
{
    return array(
        'access current_posts content' => array(
            'title' => t('Access content for the Current posts module'),
            )
        );
}
3.6 适配查询
在block和page中都要调用current_posts_contents($display)。
把current_posts_contents中的注释打开

3.7 主题化page


注意:在修改hook之后必须reenable module在可以, drupal会把实现的hook存到数据库中

 

完整current_posts.module文件请猛击此处