跳转到主要内容
mark 提交于 9 August 2011

写这个Demo为了让同学们了解如何快速创建一个自定义HOOK,并且在其他的模块里面调用它.

主要是使用了module_invoke_all('hook_name'),参数hook_name就是我们要定义的Hook了.

下面贴上完整的测试代码,记得要清除performance里面的cache data,然后点击菜单上的'Test my hook'!

Enjoy! :-)

mygarden.info

name = mygarden 
description = test my hook 
package = custom 
version = VERSION 
core = 6.x 

------------------------------------------

mygarden.module

<?php
function mygarden_menu(){
  $items = array();
  $items['mypath/mygarden'] = array(
  'title' = t('Test my hook'), 
  'page callback' => 'myfunc', 
  'access arguments' => array('access contents'), ); 
  return $items; 
} 

function myfunc(){ // 定义一个 hook_sayHello,记得在performance里面清除cache!!!! 
   module_invoke_all('sayHello');
 } 

function mygarden_sayHello(){ 
  echo 'mygarden_sayHello'; 
  echo ' ->mygarden模块调用[hook_sayHello] was success!!';  
  echo ''; 
}

------------------------------------------

myflower.info

name = myflower 
description = test my hook 
package = custom 
version = VERSION 
core = 6.x 

------------------------------------------

myflower.module

<?php
function myflower_sayHello(){
  echo 'myflower_sayHello';
  echo 'myflower模块调用[hook_sayHello] was success!!'; 
  echo '';
} 

Drupal花园 Mark 原创教程 转载请保留 http://drupalgarden.cn/content/wo-de-drupal-wo-zuo-zhu-shi-zhan-zi-ding-yi-hook

标签
Drupal 版本