开发者

drupal forum function hook to validate a list of words before posting

开发者 https://www.devze.com 2023-02-24 17:10 出处:网络
how can I implement a hook to validate the开发者_StackOverflow words that I\'m posting ? it seems that the forum lacks that feature: prohibited words

how can I implement a hook to validate the开发者_StackOverflow words that I'm posting ? it seems that the forum lacks that feature: prohibited words so I want to implement one even if I have to make my own module I just need to know what function to hook


Have you looked at the existing modules?

A quick search finds Wordfilter and Phonetic Wordfilter. I would suggest you try these out, even if they don't do exactly what you need their code will probably help point you in the right direction.

+++ EDIT +++

If you must do it when they post then use hook_nodeapi

If you want to remove word automatically then run the 'presave' operation to alter the body before saving. Something along the lines of;

function mymodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
    if($op == 'presave' && $node->type == 'forum'){
        $node->body = preg_replace('#\b(word1|word2|word3)\b#i', '*removed*', $node->body);
    }
}

Or if you wanted to prevent the user from posting until they had removed any banned words then you use the 'validate' operation. Something like;

function mymodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
    if($op == 'validate' && $node->type == 'forum'){
        if(preg_match('#\b(word1|word2|word3)\b#i', $node->body)){
            form_set_error('body', 'You have used restricted words');
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号