Webemania | Working with comment in Drupal

| Published Date: 24 Dec 2013 | Last Updated Date: 04 Sep 2017
Total Reads: 103,348 | Last Visit: 22 Sep 2017 04:15:48 AM

Adding comments to Drupal Content

Suppose, you want to add comment to Page content types. Now Go to content type edit section and then comment settings section and do the following

  1. Select Read/Writeradio in Default comment setting
  2. To hide subject field in comment form, then choose disabled in comment subject field.
  3. If you want to display name, email field in comment form, then choose “Anonymous posters must leave their contact information” in anonymous commenting
  4. Hit save content type to save these settings

Setting permission for anonymous user

Now go to permission page and search for comment module section and give permission to anonymous user

  • access comments
  • post comments
  • post comments without approval ( If you want to publish comments without admin approval )
  • Click save configuration

Setting up Restriction in user comment

For security reason, you must configure comment input format system. If you gave input format as full html permission, then user may enter untrusted data or harmful data to your sites. To do this go to : admin/settings/filters/defaults (drupal 6) and set “Filtered HTML” for anonymous user.

The anchor tag <a> is default in Filter HTML. If you think it is harmful then configure it and remove <a> tag from Filtered HTML. To do this first go to input format list page:admin/settings/filters (drupal 6) and click Filtered HTML configure link and then click configure tab . Under allowed HTML tags : remove <a>

Finally click save configuration button.

Adding captcha to block spam

If you wish you may include captcha in your comment form. Adding captacha is one of the best way to project spam comment. For this download Captcha module. Install captcha and image captcha module, go to configuration page and add comment form

Restrict on Comment body characters

Now some extra configuration. suppose you want to restrict comment body to 128 characters. To do this write a small module and write the folloing code or you can download this from here and enable.

  1. <?php
  2. // drupal 6
  3. // comment_limitation.info file
  4. ; $Id: comment_limitation hasan hafiz Exp $
  5. name = "comment limitation"
  6. description = "Set character limits in comment body"
  7. core = 6.x
  8. ?>
  9. <?php
  10. // comment_limitation.module file
  12. // implements hook_form_alter()
  13. function comment_limitation_form_alter(&$form, $form_state, $form_id) {
  14. if ($form_id == 'comment_form') {
  15. $form['comment_filter']['comment']['#maxlength'] = 128;
  16. $form['comment_filter']['comment']['#length'] = 128;
  17. $form['comment_filter']['comment']['#description'] = 'Max length 128 characters';
  18. // print_r($form); exit;
  19. }
  20. }
  21. ?>



I personally like your post, you have shared great article.

Post new comment

The content of this field is kept private and will not be shown publicly.
Max length 128 characters
Enter the characters shown in the image.

About This Mania

Hasan Hafiz, Blogger at web mania ltd (www.webemania.com). He has more than 5 years experience in web development platform. He worked as a Sr. Software Developer at Canadian based outsourcing software firm, Lead Developer at countries top most VOIPswitch development company and Sr. Software Engineer at USA based outsourcing software firm . His expertise areas are PHP, MySQL, CSS, Javascript, JQuery, HTML, XML and MVC Framework Codeigniter. He has been along with Drupal from more than 4 years. You may call him Drupal Addicted.