模組開發 - 自訂 Rules 事件(Event)

要自訂Ruels事件,主要會應用到兩個函數- hook_rules_event_info() 及 rules_invoke_event()

以下以download_count中的下載事件為例:

hook_rules_event_info() :註冊一個Event

function download_count_rules_event_info() {
  return array(
    'download_count_file_download' => array(
      'group' => t('Download Count'),
      'label' => t('A file has been downloaded'),
      'module' => 'download_count',
      'variables' => array(
        'file' => array(
          'type' => 'file',
          'label' => t('Downloaded file'),
        ),
        'user' => array(
          'type' => 'user',
          'label' => t('Downloading user'),
        ),
      ),
    ),
  );
}

rules_invoke_event() :告知Rules事件發生

if (module_exists('rules')) {
  rules_invoke_event('download_count_file_download', (object) $file, $user);
  }