Limit comment length

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 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,
    );   
  }
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.