Search code examples
drupalmodulehookforum

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


how can I implement a hook to validate the 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


Solution

  • 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');
            }
        }
    }