1

I want to add a banned words list to the site. and if a user used one of the banned word in node form (title and body), comment form (title and body), user username in the registration form, then the user should get an error message that you are using a banned word in your {field-name}.

And the form should not be submitted if the banned words are present.

How can I do this?

I have checked some modules like wordfilter but it does not provide the functionality for usernames and also it replaces the strings not giving the error message.

  • I really like the edit done via revision 2 (as an alternative to putting it on hold because "Is there a module for it" is not allowed on this site). – Pierre.Vriens Apr 18 '16 at 8:43
3

Try the Profanity module, here is a quote about it from its project page:

An alternative to Wordfilter and Phonetic Word Filter, this module provides...

  • Filters any Entity!
  • The ability to create multiple word lists.
  • Features exportable.
  • An input format filter for either replacing matched words with a repeated character or phrase.
  • Super quick no regex processing (for simple matching).
  • A function to use for custom form item validation/detection.
  • Tokens for title/label properties per Entity allowing you to specify which lists to process with.
  • A rule condition for "Text contains profanity" so you can do clever things when bad words are detected (add to moderation queue).
  • Ability to filter Entity titles before view/render.
  • A view field for each Entity title, profanity filtered.
  • Validation to prevent users registering with a username containing profanity.
  • Works with Search API by providing a filtered entity title property to be indexed.
  • Integration with Search API Views to provide a field for profanity filtered Excerpt.

Note that it has Rules integration so you can send emails etc.

|improve this answer|||||
  • 1
    Nice (also!), never heard about this module. But I think you should (slightly) extend your answer to explain that the "yellow" part of it is a quote from "somewhere" (that makes it compliant to some of the Rules of this site. – Pierre.Vriens Apr 18 '16 at 8:35
  • This module has problems. When you add a list, then its edit, delete, disable urls are pointing to some page not found pages. And this module does not provide the functionality to display an error message on the form and restrict the user from submitting the form. It also replaces the banned words with some defined words. – Ahmad Apr 18 '16 at 8:44
  • @Pierre.Vriens I think it's obvious where the yellow part came from... – No Sssweat Apr 18 '16 at 8:47
  • 1
    @NoSssweat: true, but that's not what the SE-instructions (which I did not invent) say if you quote things from somewhere ... . – Pierre.Vriens Apr 18 '16 at 8:51
1

Try using Rules Module.

{ "block_user_on_banned_words" : {
    "LABEL" : "Ban user when used banned words",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "comment" ],
    "ON" : {
      "node_insert--page" : { "bundle" : "page" },
      "node_update--page" : { "bundle" : "page" },
      "comment_insert--comment_node_page" : { "bundle" : "comment_node_page" },
      "comment_update--comment_node_page" : { "bundle" : "comment_node_page" }
    },
    "IF" : [
      { "data_is" : {
          "data" : [ "node:body" ],
          "value" : { "value" : {
              "value" : "your_banned_words0\r\nyour_banned_words1\r\nyour_banned_words2\r\nyour_banned_words3",
              "format" : "filtered_html"
            }
          }
        }
      }
    ],
    "DO" : [
      { "drupal_message" : {
          "message" : "You are using one or more banned words(your banned words)",
          "type" : "error"
        }
      }
    ]
  }
}

Change the words and content type of node and comment

|improve this answer|||||
  • Haven't QA-tested it (yet), but it looks nice, chapeau (oeps: heads off!)! However I think you should remove your first rules action. Because the question is not (I think) to "ban the user". Consider adapting your rule like so (and let me know if you did so). PS: "if" this rule works (like it is now), the QA-tester trying to use some word that is banned ... gets banned right away ... I think ... No? – Pierre.Vriens Apr 18 '16 at 8:30
  • Yes, your right, I need to removed "ban the user" action. If i didn't he will be banned instantly and the user cant able to login. Edited the answer as per your comments. And it is just a prototype, so i didnt check properly – NTT Apr 18 '16 at 8:40
  • OK, great, merci for accepting my advice ... +1. But posting code, or code variations you didn't QA-test before sounds scary ... Do you mind further updating your answer some time in the future with some kind of "I tested it and it works as design" ...? – Pierre.Vriens Apr 18 '16 at 8:46
  • Think you should further enhance your Rules Action: the message is misleading (should rather be something like "You are using one or more banned words", and the type should be an actual "error" (not just a "warning"). – Pierre.Vriens Apr 18 '16 at 14:37
0

Why not just hook the forms:

  • my_module_form_node_form_alter(&$form, $form_state)
  • my_module_form_commt_form_alter(&$form, $form_state)

and add custom validators, like

function my_module_form_node_form_alter(&$form, &$form_state, $form_id) {
  // There will already be some validate handlers added so you need to add to the
  // array rather than overwrite it.
  $form['#validate'][] = 'mymodule_node_form_validate';

}

function mymodule_node_form_validate($form, &$form_state) {
  // Random example, if the title is 'test' throw an error
  if ($form_state['values']['title'] == 'test') {
    form_set_error('title', 'Title cannot be "test"');
  }
}

Your banned words could be a vocabulary, and you could check every word with taxonomy_get_term_by_name($name, $vocabulary = NULL)

|improve this answer|||||
0

try the dev version of moduleprofanity

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.