Limit comment length

Last modified: March 28, 2009 - 18:11

If you need to limit the maximum length of comments posted by site visitors/members, this mini module will do the job. Save as file 'limit_comments.module' without the ending ?> tag, and activate as usual from the /admin/modules page. The new setting for maximum comment length will be accessible on the existing comments configuration page at /admin/comment/configure.

Drupal 6.x

Comments are different in drupal 6, they are set on content type, so we need different code, here is my suggestion :

<?php
// Must work on Drupal 6
function limit_comments_help($path, $arg) {
  switch (
$path) {
    case
'admin/modules#description':
      return
t('Enforce a maximum comment length');
    case
'admin/help#limit_comments':
     
$output = t('This module enforces a maximum limit on the length of comments posted by non-administrators.  To change the maximum comment length, visit each <a href="%url">content type</a> and set comment parameters to your needs.', array('%url' => url('admin/content/types')));
      return
$output;
  }
}

function
limit_comments_validate($node, &$form) {
 
// we set up variable by content type, so we need to get content type
 
$node = node_load($node['nid']['#value']);
 
$max = variable_get('comment_max_length_'.$node->type, 1000);
  if (
strlen(trim($form['values']['comment'])) > $max) {
   
form_set_error('comment', t('Your comment is too long. Comments are limited to a maximum of %num characters.', array('%num' => $max)));
  }
}

/**
* Implementation of hook_form_alter().
*/
function limit_comments_form_alter(&$form, $form_state, $form_id) {
 
// comment form
 
if ($form_id == 'comment_form') {
   
$node = node_load($form['nid']['#value']); // we load node to get content type
   
$form['#validate'] = array('limit_comments_validate');
   
$form['comment_filter']['comment']['#description'] = t('Comments are limited to a maximum of %num characters.', array('%num' => variable_get('comment_max_length_'.$node->type, 0)));
  }
 
// content type form
 
elseif ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
   
$form['comment']['comment_max_length'] = array(
     
'#type' => 'select',
     
'#title' => t('Maximum comment length'),
     
'#default_value' => variable_get('comment_max_length_'. $form['#node_type']->type, 1000), // we set variable for each content type
     
'#options' => drupal_map_assoc(array(300, 500, 700, 1000, 1500, 2000, 2500, 3000, 5000)),
     
'#description' => t('Maximum permitted length of a comment (in characters) for non-administrators.'),
     
'#weight' => -1,
    );
  }
}
?>

Drupal 5.x

Note: for D5 you also need to create a modules/limit_comments/limit_comments.info file per http://drupal.org/node/126743

<?php
// Tested with Drupal 5.2
function limit_comments_help($section) {
  switch (
$section) {
    case
'admin/modules#description':
      return
t('Enforce a maximum comment length');
    case
'admin/help#limit_comments':
     
$output = t('This module enforces a maximum limit on the length of comments posted by non-administrators.  To change the maximum comment length, visit the <a href="%url">comments configuration page</a>.', array('%url' => url('admin/comment/configure')));
      return
$output;
  }
}

function
limit_comments_validate($form_id, $form_values) {
 
$max = variable_get('comment_max_length', 1000);
  if (
strlen(trim($form_values['comment'])) > $max) {
   
form_set_error('comment', t('Your comment is too long. Comments are limited to a maximum of %num characters.', array('%num' => $max)));
  }
}

function
limit_comments_form_alter($form_id, &$form) {

  if (
$form_id == 'comment_form' && !user_access('administer comments')) {
   
$form['#validate']['limit_comments_validate'] = array();
   
$form['comment_filter']['comment']['#description'] = t('Comments are limited to a maximum of %num characters.', array('%num' => variable_get('comment_max_length', 1000)));
  }
  elseif (
$form_id == 'comment_admin_settings') {

   
$form['comment_max_length'] = array(
     
'#type' => 'select',
     
'#title' => t('Maximum comment length'),
     
'#default_value' => variable_get('comment_max_length', 1000),
     
'#options' => drupal_map_assoc(array(300, 500, 700, 1000, 1500, 2000, 2500, 3000, 5000)),
     
'#description' => t('Maximum permitted length of a comment (in characters) for non-administrators.'),
     
'#weight' => -1,
    ); 
  }
}
?>

Drupal 4.7

<?php
// Tested with Drupal 4.7.4
function limit_comments_help($section) {
  switch (
$section) {
    case
'admin/modules#description':
      return
t('Enforce a maximum comment length');
    case
'admin/help#limit_comments':
     
$output = t('This module enforces a maximum limit on the length of comments posted by non-administrators.  To change the maximum comment length, visit the <a href="%url">comments configuration page</a>.', array('%url' => url('admin/comment/configure')));
      return
$output;
  }
}

function
limit_comments_validate($form_id, $form_values) {
 
$max = variable_get('comment_max_length', 1000);
  if (
strlen(trim($form_values['comment'])) > $max) {
   
form_set_error('comment', t('Your comment is too long. Comments are limited to a maximum of %num characters.', array('%num' => $max)));
  }
}

function
limit_comments_form_alter($form_id, &$form) {

  if (
$form_id == 'comment_form' && !user_access('administer comments')) {
   
$form['#validate']['limit_comments_validate'] = array();
   
$form['comment_filter']['comment']['#description'] = t('Comments are limited to a maximum of %num characters.', array('%num' => variable_get('comment_max_length', 1000)));
  }
  elseif (
$form_id == 'comment_settings_form') {
   
$form['comment_max_length'] = array(
     
'#type' => 'select',
     
'#title' => t('Maximum comment length'),
     
'#default_value' => variable_get('comment_max_length', 1000),
     
'#options' => drupal_map_assoc(array(300, 500, 700, 1000, 1500, 2000, 2500, 3000, 5000)),
     
'#description' => t('Maximum permitted length of a comment (in characters) for non-administrators.'),
     
'#weight' => -1,
    );   
  }
}
?>

demonstration website ?

wwwoliondorcom - April 7, 2009 - 13:03

Hi, do you have a demonstration website where you use this module ?

Thanks.

how to enforce minimum comment length?

giorgio79 - May 3, 2009 - 11:29

Hello,

I would like to do the oposite, namely enforce a minimum lenght.

Cheers,
G

I would like to know this too

nmridul - May 4, 2009 - 10:06

Here is a thread for this exact requirement. I hope someone can respond on how to set a minimum number of characters for the comments.

http://drupal.org/node/452166

Length restriction on comment's title

ekrispin - July 30, 2009 - 22:08

How could I do the same Max/min restrictions but on the title of the comment?

---

http://www.openify.com - Drupal Services in Israel

Revised for D6

As If - September 9, 2009 - 19:42

The form_alter() function above didn't work for me under D6. Here is a modified version that did...

function limit_comments_form_alter($form_id, &$form) {
  // comment form
  if ($form_id['#id'] == 'comment-form') {
    $form_id['preview']['#type']='hidden';
    $node = node_load($form['nid']['#value']);
    $form_id['#validate'][] = 'limit_comments_validate';
    $form_id['comment_filter']['comment']['#description'] = t('Comments are limited to a maximum of %num characters.', array('%num' => variable_get('comment_max_length_'.$node->type, 1000)));
  }
  // content type form
  if ($form_id['#id'] == 'node-type-form' && $form['post']['op'] != 'Save content type') {
    $form_id['comment']['comment_max_length'] = array(
      '#type' => 'select',
      '#title' => t('Maximum comment length'),
      '#default_value' => variable_get('comment_max_length_' . $form_id['#node_type']->type, 1000),
      '#options' => drupal_map_assoc(array(300, 500, 700, 1000, 1500, 2000, 2500, 3000, 5000)),
      '#description' => t('Maximum permitted length of a comment (in characters) for non-administrators.'),
      '#weight' => -1,
    );
  }
}

NOTE: The above "module" does not update the value once it is set, because variable_set() is never used. IOW, once the content type is created with a given comment limit value, that's the value it will remain unless changed directly in the variables table (via PHPMyAdmin or whatever).

-------------------------------------------
Interactive Worlds and Immersive Obsessions
http://www.asifproductions.com

Variables?

djudd - September 11, 2009 - 13:08

I've put this module in place, but I'm having one problem.

I can't even set a variable. When I go in through PHPMyAdmin, I don't see any variables that match up correctly. There is no comment_max_length_story variable, for example.

So if I set a length of 2,000 characters for a story comment, and save the content type, when I go back in, it's back to 1,000.

I tried creating the variable manually and setting it, but that did nothing either.

try just inserting it

As If - September 12, 2009 - 06:11

Don't know if your content type was created already or not, but using PHPMyAdmin you can just try inserting the variable directly into the variables table. The command variable_get('comment_max_length_'.$node->type, 1000)) is saying "go get this variable, and if you don't find it, return 1000". That's probably why you're getting 1000 again (if I understand you correctly).

-------------------------------------------
Interactive Worlds and Immersive Obsessions
http://www.asifproductions.com

@As If: your revision for D6

marcvangend - October 9, 2009 - 11:59

@As If: your revision for D6 might happen to work, but it's definately not the correct way to do it because hook_form_alter takes three arguments in D6 (&$form, $form_state, $form_id). See http://api.drupal.org/api/function/hook_form_alter/6.

you're right

As If - October 9, 2009 - 22:11

You're right, Marc. It's a relic of some D5 code that got mixed in with D6 code. Frankly I'm actually a little amazed that it works ;-) but it does. On a D6 production site with massive traffic. Guess I should rewrite it though, just to stick with the program.

EDIT FOR CLOSURE: The original code (with some trivial variations) now works for me too. My problem was the arguments to the function, which I seem to have inherited from the D5 version. Anyway, for those following along... My version of the function managed to get around the missing third argument by using $form['#id'] instead of $form_id (which otherwise would have come from the third variable). I figured it out by putting this at the top of the function and examining the output:

function zzz_form_alter(&$form, $form_state, $form_id) {
// DEBUGGING
echo $form_id . '<br>';
echo "<textarea cols=100 rows=30>";
print_r($form);
echo '</textarea>';
// END DEBUGGING
...

Maybe that can help someone else while they're debugging forms. We now return you to your regularly-scheduled programming!

-------------------------------------------
Interactive Worlds and Immersive Obsessions
http://www.asifproductions.com

Thanks for the explanation, I

marcvangend - October 9, 2009 - 22:37

Thanks for the explanation, I already guessed something like that would have happened. In case anyone likes to know: the code in the original post works fine for me.

Some bonus javascript:
I wanted a way to inform the user about the number of characters used while typing, so I put this javascript in my page (see the Javascript Startup Guide if you don't know how to do that).

var description = $('#comment-form .description').html() + ' ';
if (description) {
  $('#edit-comment').keyup( function() {
      var charCount = $(this).val().length;
      $('#comment-form .description').html(description + Drupal.t('You have used !count characters.', { '!count': charCount }));
  });
}

Thanks!

abs13 - September 1, 2009 - 01:40

Thank you very much for this mini module, it works fine! I am using it to create something like a shoutbox using Drupals build-in comments. The only problem i have is that when form validation fails I get redirected to "comment/reply/" but I want Drupal to stay on the current page and show the error message there. Any suggestions?

Check maxlength module

shenzhuxi - September 9, 2009 - 09:21

check http://drupal.org/node/572398
I added comment limit to maxlength module.

 
 

Drupal is a registered trademark of Dries Buytaert.