Drupal模組開發之建立自己的鉤子

來源:互聯網
上載者:User

Drupal的鉤子系統允許和模組互動並改變其他模組的邏輯,甚至是改變Drupal核心邏輯。這是一個非常簡單的系統,甚至可以讓第三方模組建立自己的鉤子。在通常的實踐中,有兩種類型的鉤子你可能想要建立,一種是內容修改類的鉤子,一種是攔截類的鉤子。修改類的鉤子提供了一個標準的方法來修改某個特定對象或變數的內容,典型的是使用 drupal_alter()函數。攔截類的鉤子可以讓第三方模組在模組執行過程中根據條件做出一些動作。


例1:簡單調用

 代碼如下 複製代碼
<?php
// will call all modules implementing hook_hook_name
module_invoke_all('hook_name');
?>



例2:彙總結果

 代碼如下 複製代碼
<?php
$result = array();
foreach (module_implements('hook_name') as $module) {
// will call all modules implementing hook_hook_name and
// push the results onto the $result array
$result[] = module_invoke($module, 'hook_name');
}
?>



例3:使用 drupal_alter() 改變內容

 代碼如下 複製代碼
<?php
$data = array(
'key1' => 'value1',
'key2' => 'value2',
);
// will call all modules implementing hook_my_data_alter
drupal_alter('my_data', $data);
?>



例4:引用傳參,不能使用 module_invoke

 代碼如下 複製代碼
<?php
// @see user_module_invoke()
foreach (module_implements('hook_name') as $module) {
$function = $module . '_hook_name';
// will call all modules implementing hook_hook_name
// and can pass each argument as reference determined
// by the function declaration
$function($arg1, $arg2);
}
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.