第二节 实现一个钩子

 

实现一个钩子
 
让我们再回忆一下:Drupal的建立是基于一个钩子系统的,有时我们也称之为回调系统。在Drupal运行的过程中,会询问模块是否有什么要做的。例如,当需要确定哪个模块该对当前请求做出回应的时候,所有模块都会被要求将自己所处理的路径提供出来。这个行为是如何实现的呢?Drupal会将所有模块进行列表,然后调用这些模块中以模块名加上“_menu”命名的函数。当进行到annotate模块(将会在早些时候按照字母表的顺序被默认定义在列表中)的时候,就会调用annotate_menu()函数——其返回值是一个由菜单项组成的数组。每一项(我们此时只有一项内容)都以路径为键,在这个例子中,路径是admin/settings/annotate。而我们的菜单项的内容是由键和值组成,并用来描述当这个路径被访问Drupal应该做些什么事情。如果想详细了解这些,请阅读第四章,在那里我们将会讨论Drupal的menu/callback系统。
 
下面是将要加入到我们的模块中的内容:
 
 /**
 *Implementation of hook_menu().
 */
 function annotate_menu(){
 $items[‘admin/settings/annotate’] = array(
    ‘title’ => ‘Annotation settings’,
    ‘description’ => ‘Change how annotations behave.’,
    ‘page callback’ => ‘drupal_get_form’,
    ‘page arguments’ => array(‘annotate_admin_settings’),
    ‘access arguments’ => array(‘administer site configuration’),
    ‘type’ => MENU_NORMAL_ITEM,
    ‘file’ => ‘annotate.admin.inc’,
);
 
return $items;
}
 
此时先不要过多的担心细节问题。这段代码的意思是说:“当用户访问http://example.com/?q=admin/settings/annotate,调用函数drupal_get_form(),并且传出它的表单ID annotate_admin_settings,以用来在annotate.admin.inc中搜寻描述这个表单的函数。而且,只有在administer site configuration中获得权限的用户才能访问这个菜单项。”当进行到显示这个表单的时候,Drupal会要求我们对此表单提供一个定义(如何定义稍后再说)。在Drupal查询完所有模块的菜单项时,就会为当前路径的生成一个菜单,用来选择恰当的函数以应对当前请求。
 
Note:如果你有兴趣了解一下hook函数的工作机制,去看看includes/module.inc文件中的module_invoke_all()函数吧。
 
现在你知道为什么叫它hook_menu()或者menu hook了吧?Drupal的钩子(hook)通常都是通过将钩子名附在你的模块名的后面建立起来的。
 
Tip:Drupal的钩子几乎使你可以修改这个软件的所有方面。关于hook及其功能的完整列表可以在DrupalAPI文件的网站上找到(http://api.drupal.org).
 

 

评论

Parse error: syntax error, unexpected T_STRING, expecting ')' in D:\web\htdocs\test1.com\www\drupal\sites\all\modules\custom\annotate\annotate.module on line 16

大哥,这里的代码格式也有问题.能不能换个编辑器( word )啊!我在 notepad++ 改好了.
/**
*Implementation of hook_menu().
*/
function annotate_menu(){
$items[ 'admin/settings/annotate' ] = array(
'title' => 'Annotation settings',
'description' => 'Change how annotations behave.',
'page callback' => 'drupal_get_form',
'page arguments' => array('annotate_admin_settings'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'file' => 'annotate.admin.inc',
);

return $items;
}

我们新手很菜的!!

其实就是word的问题啊
因为word里的格式和代码编辑器里的都不一样,尤其是空格,word里面两个空格基本就等于这个fckeditor的3个空格了
所以请大家理解代码,而不要照搬格式了。
还有就是
代码要注意缩进啊
你的代码没有缩进,读起来会很麻烦的

Note:如果你有兴趣了解一下hook函数的工作机制,去看看includes/module.inc文件中的module_incoke_all()函数吧。
是includes/module.inc文件中的module_invoke_all()函数吧

多谢指正
手抖了打到c上去了
呵呵

I will take dissertation writing or english dissertation. And to demonstrate that you are the professional, you should use thesis writing service.

Lots of custom written essays about could be created for some men which will like to know very interesting article just about terms.

I think that that doesn’t matter if opt for dissertation writing service or thesis writing services! Because simply the quality of the graduate thesis close to this post is counting.

Students have an opportunity buy the essays written and buy essay at the essay writing services just about this good topic.

You should know that I from time to time buy custom essay papersor buy research papers opting for essay writing service and students purchase essay writing just about this post . Thanks for the great text!

With the goal to prevent the mistakes in the thesis help, this can be good to purchase the superb article close to this good topic from the dissertation writing service in Internet. That’s easier to have the doctoral degree going that way.