You are here

Drupal专业开发指南 第14章 使用hook_taxonomy()来获悉词汇表的变更

g089h515r806 的头像
Submitted by g089h515r806 on 星期五, 2009-08-21 15:49

老葛的Drupal培训班 Think in Drupal

如果你为你自己的模块保留了一个词汇表,当使用标准的分类用户界面修改你的词汇表时,那么你将想收到词汇表的相关修改信息。对于由taxonomy.module维护的已有的一个词汇表,当它被修改时,你可能也想收到相关的修改信息。在任何一种情况下,通过实现hook_taxonomy(),当词汇表变更时你都会收到相应的通知。下面的模块实现了hook_taxonomy(),当词汇表变更时,你将会收到电子邮件通知。下面是taxonomymonitor.info文件:
 
; $Id$
name = Taxonomy Monitor
description = Sends email to notify of changes to taxonomy vocabularies.
package = Pro Drupal Development
dependencies[] = taxonomy
core = 6.x
 
    下面是taxonomymonitor.module:
 
<?php
// $Id$
 
/**
 * Implementation of hook_taxonomy().
 *
 * Sends email when changes to vocabularies or terms occur.
 */
function taxonomymonitor_taxonomy($op, $type, $array = array()) {
    $to = 'me@example.com';
    $name = check_plain($array['name']);
 
    // $type is either 'vocabulary' or 'term'.
    switch ($type) {
        case 'vocabulary':
            switch($op) {
                case 'insert':
                    $subject = t('Vocabulary @voc was added.', array('@voc' => $name));
                    break;
                case 'update':
                    $subject = t('Vocabulary @voc was changed.', array('@voc' => $name));
                    break;
                case 'delete':
                    $subject = t('Vocabulary @voc was deleted.', array('@voc' => $name));
                    break;
            }
            break;
        case 'term':
            switch($op) {
                case 'insert':
                    $subject = t('Term @term was added.', array('@term' => $name));
                    break;
                case 'update':
                    $subject = t('Term @term was changed.', array('@term' => $name));
                    break;
                case 'delete':
                    $subject = t('Term @term was deleted.', array('@term' => $name));
                    break;
            }
    }
 
    // Dump the vocabulary or term information out and send it along.
    $body = print_r($array, TRUE);
 
    // Send the email.
    watchdog('taxonomymonitor', 'Sending email for @type @op',
        array('@type' => $type, '@op' => $op));
    drupal_mail('taxonomymonitor-notify', $to, $subject, $body);
}
 
    对于一些额外的功能,比如将变更者的名字也包含进来,你可以修改这一模块来实现自己的需求。

Drupal版本:

评论

thomasfan 的头像

drupal_mail('taxonomymonitor,‘notify', $to, $subject, $body);